KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > completion > HTMLDocView


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.editor.completion;
22
23 import java.awt.Color JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.Rectangle JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.Reader JavaDoc;
28 import java.io.StringReader JavaDoc;
29
30 import javax.swing.JEditorPane JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
32 import javax.swing.text.BadLocationException JavaDoc;
33 import javax.swing.text.Document JavaDoc;
34 import javax.swing.text.EditorKit JavaDoc;
35 import javax.swing.text.html.HTMLEditorKit JavaDoc;
36
37 /**
38  * HTML documentation view.
39  * Javadoc content is displayed in JEditorPane pane using HTMLEditorKit.
40  *
41  * @author Martin Roskanin
42  * @since 03/2002
43  */

44 public class HTMLDocView extends JEditorPane JavaDoc {
45     
46     private HTMLEditorKit JavaDoc htmlKit;
47     
48     /** Creates a new instance of HTMLJavaDocView */
49     public HTMLDocView(Color JavaDoc bgColor) {
50         setEditable(false);
51         setFocusable(false);
52         setBackground(bgColor);
53         setMargin(new Insets JavaDoc(0,3,3,3));
54     }
55
56     /** Sets the javadoc content as HTML document */
57     public void setContent(final String JavaDoc content, final String JavaDoc reference) {
58         SwingUtilities.invokeLater(new Runnable JavaDoc(){
59             public void run(){
60                 Reader JavaDoc in = new StringReader JavaDoc("<HTML><BODY>"+content+"</BODY></HTML>");//NOI18N
61
try{
62                     Document JavaDoc doc = getDocument();
63                     doc.remove(0, doc.getLength());
64                     getEditorKit().read(in, getDocument(), 0); //!!! still too expensive to be called from AWT
65
setCaretPosition(0);
66                     if (reference != null) {
67                         SwingUtilities.invokeLater(new Runnable JavaDoc(){
68                             public void run(){
69                                 scrollToReference(reference);
70                             }
71                         });
72                     } else {
73                         scrollRectToVisible(new Rectangle JavaDoc(0,0,0,0));
74                     }
75                 }catch(IOException JavaDoc ioe){
76                     ioe.printStackTrace();
77                 }catch(BadLocationException JavaDoc ble){
78                     ble.printStackTrace();
79                 }
80             }
81         });
82     }
83     
84     protected EditorKit JavaDoc createDefaultEditorKit() {
85         // it is extremelly slow to init it
86
if (htmlKit == null){
87             htmlKit= new HTMLEditorKit JavaDoc ();
88             setEditorKit(htmlKit);
89
90             // override the Swing default CSS to make the HTMLEditorKit use the
91
// same font as the rest of the UI.
92

93             // XXX the style sheet is shared by all HTMLEditorKits. We must
94
// detect if it has been tweaked by ourselves or someone else
95
// (template description for example) and avoid doing the same
96
// thing again
97

98             if (htmlKit.getStyleSheet().getStyleSheets() != null)
99                 return htmlKit;
100             
101             javax.swing.text.html.StyleSheet JavaDoc css = new javax.swing.text.html.StyleSheet JavaDoc();
102             java.awt.Font JavaDoc f = getFont();
103             css.addRule(new StringBuffer JavaDoc("body { font-size: ").append(f.getSize()) // NOI18N
104
.append("; font-family: ").append(f.getName()).append("; }").toString()); // NOI18N
105
css.addStyleSheet(htmlKit.getStyleSheet());
106             htmlKit.setStyleSheet(css);
107         }
108         return htmlKit;
109     }
110 }
111
Popular Tags