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-2007 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.modules.java.navigation;
043:
044: import java.awt.Color;
045: import java.awt.Insets;
046: import java.awt.Rectangle;
047: import java.io.IOException;
048: import java.io.Reader;
049: import java.io.StringReader;
050:
051: import javax.swing.JEditorPane;
052: import javax.swing.SwingUtilities;
053: import javax.swing.text.BadLocationException;
054: import javax.swing.text.Document;
055: import javax.swing.text.EditorKit;
056: import javax.swing.text.html.HTMLEditorKit;
057:
058: /**
059: * HTML documentation view.
060: * Javadoc content is displayed in JEditorPane pane using HTMLEditorKit.
061: *
062: * @author Martin Roskanin
063: * @since 03/2002
064: */
065: class HTMLDocView extends JEditorPane {
066:
067: private HTMLEditorKit htmlKit;
068:
069: /** Creates a new instance of HTMLJavaDocView */
070: public HTMLDocView(Color bgColor) {
071: setEditable(false);
072: setFocusable(true);
073: setBackground(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, final String reference) {
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: if (reference != null) {
089: SwingUtilities.invokeLater(new Runnable() {
090: public void run() {
091: scrollToReference(reference);
092: }
093: });
094: } else {
095: scrollRectToVisible(new Rectangle(0, 0, 0, 0));
096: }
097: } catch (IOException ioe) {
098: ioe.printStackTrace();
099: } catch (BadLocationException ble) {
100: ble.printStackTrace();
101: }
102: }
103: });
104: }
105:
106: protected EditorKit createDefaultEditorKit() {
107: // it is extremelly slow to init it
108: if (htmlKit == null) {
109: htmlKit = new HTMLEditorKit();
110: setEditorKit(htmlKit);
111:
112: // override the Swing default CSS to make the HTMLEditorKit use the
113: // same font as the rest of the UI.
114:
115: // XXX the style sheet is shared by all HTMLEditorKits. We must
116: // detect if it has been tweaked by ourselves or someone else
117: // (template description for example) and avoid doing the same
118: // thing again
119:
120: if (htmlKit.getStyleSheet().getStyleSheets() != null)
121: return htmlKit;
122:
123: javax.swing.text.html.StyleSheet css = new javax.swing.text.html.StyleSheet();
124: java.awt.Font f = getFont();
125: css.addRule(new StringBuffer("body { font-size: ").append(
126: f.getSize())
127: // NOI18N
128: .append("; font-family: ").append(f.getName())
129: .append("; }").toString()); // NOI18N
130: css.addStyleSheet(htmlKit.getStyleSheet());
131: htmlKit.setStyleSheet(css);
132: }
133: return htmlKit;
134: }
135: }
|