001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.editor.ext;
043:
044: import java.awt.Color;
045: import java.io.IOException;
046: import java.io.BufferedReader;
047: import java.io.StringReader;
048: import java.awt.Insets;
049: import java.awt.Rectangle;
050: import java.io.Reader;
051:
052: import javax.swing.JEditorPane;
053: import javax.swing.SwingUtilities;
054: import javax.swing.text.html.HTMLEditorKit;
055: import javax.swing.text.BadLocationException;
056: import javax.swing.text.Document;
057: import javax.swing.text.EditorKit;
058:
059: /**
060: * HTML javadoc view.
061: * Javadoc content is displayed in JEditorPane pane using HTMLEditorKit.
062: *
063: * @author Martin Roskanin
064: * @since 03/2002
065: */
066: public class HTMLJavaDocView extends JEditorPane implements JavaDocView {
067:
068: private HTMLEditorKit htmlKit;
069:
070: /** Creates a new instance of HTMLJavaDocView */
071: public HTMLJavaDocView(Color bgColor) {
072: setEditable(false);
073: setBGColor(bgColor);
074: setMargin(new Insets(0, 3, 3, 3));
075: }
076:
077: /** Sets the javadoc content as HTML document */
078: public void setContent(final String content) {
079: SwingUtilities.invokeLater(new Runnable() {
080: public void run() {
081: Reader in = new StringReader("<HTML><BODY>" + content
082: + "</BODY></HTML>");//NOI18N
083: try {
084: Document doc = getDocument();
085: doc.remove(0, doc.getLength());
086: getEditorKit().read(in, getDocument(), 0); //!!! still too expensive to be called from AWT
087: setCaretPosition(0);
088: scrollRectToVisible(new Rectangle(0, 0, 0, 0));
089: } catch (IOException ioe) {
090: ioe.printStackTrace();
091: } catch (BadLocationException ble) {
092: ble.printStackTrace();
093: }
094: }
095: });
096: }
097:
098: /** Sets javadoc background color */
099: public void setBGColor(Color bgColor) {
100: setBackground(bgColor);
101: }
102:
103: protected EditorKit createDefaultEditorKit() {
104: // it is extremelly slow to init it
105: // new RuntimeException("new HTMLEditorKit").printStackTrace();
106: if (htmlKit == null) {
107: htmlKit = new HTMLEditorKit();
108: setEditorKit(htmlKit);
109:
110: // override the Swing default CSS to make the HTMLEditorKit use the
111: // same font as the rest of the UI.
112:
113: // XXX the style sheet is shared by all HTMLEditorKits. We must
114: // detect if it has been tweaked by ourselves or someone else
115: // (template description for example) and avoid doing the same
116: // thing again
117:
118: if (htmlKit.getStyleSheet().getStyleSheets() != null)
119: return htmlKit;
120:
121: javax.swing.text.html.StyleSheet css = new javax.swing.text.html.StyleSheet();
122: java.awt.Font f = /*new javax.swing.JTextArea().*/getFont();
123: css.addRule(new StringBuffer("body { font-size: ").append(
124: f.getSize())
125: // NOI18N
126: .append("; font-family: ").append(f.getName())
127: .append("; }").toString()); // NOI18N
128: css.addStyleSheet(htmlKit.getStyleSheet());
129: htmlKit.setStyleSheet(css);
130: }
131: return htmlKit;
132: }
133: }
|