KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > HTMLJavaDocView


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.editor.ext;
22
23 import java.awt.Color JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.BufferedReader JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.io.Reader JavaDoc;
30
31 import javax.swing.JEditorPane JavaDoc;
32 import javax.swing.SwingUtilities JavaDoc;
33 import javax.swing.text.html.HTMLEditorKit JavaDoc;
34 import javax.swing.text.BadLocationException JavaDoc;
35 import javax.swing.text.Document JavaDoc;
36 import javax.swing.text.EditorKit JavaDoc;
37
38 /**
39  * HTML javadoc view.
40  * Javadoc content is displayed in JEditorPane pane using HTMLEditorKit.
41  *
42  * @author Martin Roskanin
43  * @since 03/2002
44  */

45 public class HTMLJavaDocView extends JEditorPane JavaDoc implements JavaDocView {
46     
47     private HTMLEditorKit JavaDoc htmlKit;
48     
49     /** Creates a new instance of HTMLJavaDocView */
50     public HTMLJavaDocView(Color JavaDoc bgColor) {
51         setEditable(false);
52         setBGColor(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) {
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                     scrollRectToVisible(new Rectangle JavaDoc(0,0,0,0));
67                 }catch(IOException JavaDoc ioe){
68                     ioe.printStackTrace();
69                 }catch(BadLocationException JavaDoc ble){
70                     ble.printStackTrace();
71                 }
72             }
73         });
74     }
75     
76     /** Sets javadoc background color */
77     public void setBGColor(Color JavaDoc bgColor) {
78         setBackground(bgColor);
79     }
80     
81     protected EditorKit JavaDoc createDefaultEditorKit() {
82         // it is extremelly slow to init it
83
// new RuntimeException("new HTMLEditorKit").printStackTrace();
84
if (htmlKit == null){
85             htmlKit= new HTMLEditorKit JavaDoc ();
86             setEditorKit(htmlKit);
87
88             // override the Swing default CSS to make the HTMLEditorKit use the
89
// same font as the rest of the UI.
90

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

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