KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > htmlviewer > JavaHTMLViewerPlugin


1 package org.columba.core.gui.htmlviewer;
2
3 import java.io.IOException JavaDoc;
4 import java.io.Reader JavaDoc;
5 import java.io.StringReader JavaDoc;
6 import java.net.URL JavaDoc;
7
8 import javax.swing.BorderFactory JavaDoc;
9 import javax.swing.JComponent JavaDoc;
10 import javax.swing.JScrollPane JavaDoc;
11 import javax.swing.JTextPane JavaDoc;
12 import javax.swing.text.BadLocationException JavaDoc;
13 import javax.swing.text.Document JavaDoc;
14 import javax.swing.text.Element JavaDoc;
15 import javax.swing.text.ElementIterator JavaDoc;
16 import javax.swing.text.MutableAttributeSet JavaDoc;
17 import javax.swing.text.StyleContext JavaDoc;
18 import javax.swing.text.html.HTML JavaDoc;
19 import javax.swing.text.html.HTMLDocument JavaDoc;
20 import javax.swing.text.html.HTMLEditorKit JavaDoc;
21 import javax.swing.text.html.HTMLDocument.HTMLReader;
22
23 import org.columba.core.gui.htmlviewer.api.IHTMLViewerPlugin;
24 import org.columba.core.io.DiskIO;
25
26 public class JavaHTMLViewerPlugin extends JScrollPane JavaDoc implements
27         IHTMLViewerPlugin {
28
29     private HTMLEditorKit JavaDoc htmlEditorKit;
30
31     private AsynchronousHTMLDocument doc;
32
33     private JTextPane JavaDoc textPane = new JTextPane JavaDoc();
34
35     public JavaHTMLViewerPlugin() {
36         super();
37
38         
39         
40         setViewportView(textPane);
41         // textPane.setMargin(new Insets(5, 5, 5, 5));
42
textPane.setEditable(false);
43
44         htmlEditorKit = new HTMLEditorKit JavaDoc();
45         textPane.setEditorKit(htmlEditorKit);
46
47         textPane.setContentType("text/html");
48
49         setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
50     }
51
52     /*
53      * public void view(String htmlSource) {
54      *
55      * setText(htmlSource);
56      *
57      * postView(); }
58      */

59
60     private void postView() {
61         // setup base url in order to be able to display images
62
// in html-component
63
URL JavaDoc baseUrl = DiskIO.getResourceURL("org/columba/core/icons/MISC/");
64
65         ((HTMLDocument JavaDoc) textPane.getDocument()).setBase(baseUrl);
66
67         // scroll window to the beginning
68
textPane.setCaretPosition(0);
69     }
70
71     public void view(String JavaDoc text) {
72         if (text == null)
73             return;
74
75         doc = new AsynchronousHTMLDocument();
76
77         Reader JavaDoc rd = new StringReader JavaDoc(text);
78         try {
79             htmlEditorKit.read(rd, doc, 0);
80         } catch (BadLocationException JavaDoc e) {
81             // TODO Auto-generated catch block
82
e.printStackTrace();
83         } catch (IOException JavaDoc e) {
84             // TODO Auto-generated catch block
85
e.printStackTrace();
86         }
87         textPane.setDocument(doc);
88
89         postView();
90     }
91
92     public JComponent JavaDoc getComponent() {
93         return textPane;
94     }
95
96     /**
97      * Setting HTMLDocument to be an asynchronize model.
98      * <p>
99      * JTextPane therefore uses a background thread to display the message. This
100      * dramatically improves the performance of displaying a message.
101      * <p>
102      * Trick is to overwrite the getAsynchronousLoadPriority() to return a
103      * decent value.
104      *
105      * @author fdietz
106      */

107     
108     public class AsynchronousHTMLDocument extends HTMLDocument JavaDoc {
109
110         /**
111          *
112          */

113         public AsynchronousHTMLDocument() {
114             super();
115             putProperty("IgnoreCharsetDirective", new Boolean JavaDoc(true));
116         }
117         
118         /*Overwrite the method to maintain line breaks when copying
119          * messages form the MessageViewer.
120          * @author aoki-y
121          * @see javax.swing.text.html.HTMLDocument#getReader(int)
122          */

123         public HTMLEditorKit.ParserCallback JavaDoc getReader(int pos) {
124             Object JavaDoc desc = getProperty(Document.StreamDescriptionProperty);
125             if (desc instanceof URL JavaDoc) {
126                 setBase((URL JavaDoc)desc);
127             }
128             HTMLReader reader = new MyReader(pos);
129             return reader;
130             }
131         
132         public class MyReader extends HTMLDocument.HTMLReader JavaDoc{
133             public MyReader(int pos){super(pos);}
134             
135             protected void addSpecialElement(HTML.Tag JavaDoc t, MutableAttributeSet JavaDoc a) {
136                 super.addSpecialElement(t,a);
137                 if(t==HTML.Tag.BR){
138                     int size=parseBuffer.size();
139                     parseBuffer.removeElementAt(size-1);
140                     char[] c={'\n'};
141                     parseBuffer.addElement(new ElementSpec(
142                     a.copyAttributes(), ElementSpec.ContentType,c,0,c.length));
143                 }
144             }
145         }
146
147         /**
148          * From the JDK1.4 reference:
149          * <p>
150          * This may load either synchronously or asynchronously depending upon
151          * the document returned by the EditorKit. If the Document is of type
152          * AbstractDocument and has a value returned by
153          * AbstractDocument.getAsynchronousLoadPriority that is greater than or
154          * equal to zero, the page will be loaded on a separate thread using
155          * that priority.
156          *
157          * @see javax.swing.text.AbstractDocument#getAsynchronousLoadPriority()
158          */

159         public int getAsynchronousLoadPriority() {
160             return 10;
161         }
162
163         public String JavaDoc getTextWithLineBreaks(int start, int end)
164                 throws BadLocationException JavaDoc {
165             StringBuffer JavaDoc result = new StringBuffer JavaDoc(end - start);
166             ElementIterator JavaDoc iter = new ElementIterator JavaDoc(this);
167
168             // First find the beginning element
169
for (iter.next(); iter.current() != null; iter.next()) {
170                 Element JavaDoc e = iter.current();
171                 if (e.isLeaf()
172                         && (e.getStartOffset() >= start || e.getEndOffset() >= start)
173                         && e.getStartOffset() <= end) {
174                     Object JavaDoc a = e.getAttributes().getAttribute(
175                             StyleContext.NamedStyle.NameAttribute);
176                     if (a == HTML.Tag.CONTENT) {
177                         int as = Math.max(e.getStartOffset(), start);
178                         int ae = Math.min(e.getEndOffset(), end);
179                         result.append(super.getText(as, ae - as));
180                     }
181                     if (a == HTML.Tag.BR) {
182                         result.append("\n");
183                     }
184                 }
185             }
186
187             return result.toString();
188         }
189     }
190
191     public boolean initialized() {
192         return true;
193     }
194
195     /**
196      * @see javax.swing.text.JTextComponent#getSelectedText()
197      */

198     public String JavaDoc getSelectedText() {
199         try {
200             return doc.getTextWithLineBreaks(textPane.getSelectionStart(),
201                     textPane.getSelectionEnd());
202         } catch (BadLocationException JavaDoc e) {
203             return "";
204         }
205     }
206
207     public JComponent JavaDoc getContainer() {
208         return this;
209     }
210
211     public String JavaDoc getText() {
212         try {
213             return doc.getTextWithLineBreaks(0,doc.getLength());
214         } catch (BadLocationException JavaDoc e) {
215             return "";
216         }
217     }
218
219     /**
220      * @see org.columba.core.gui.htmlviewer.api.IHTMLViewerPlugin#setCaretPosition(int)
221      */

222     public void setCaretPosition(int position) {
223         textPane.setCaretPosition(position);
224     }
225
226     /**
227      * @see org.columba.core.gui.htmlviewer.api.IHTMLViewerPlugin#moveCaretPosition(int)
228      */

229     public void moveCaretPosition(int position) {
230         textPane.moveCaretPosition(position);
231     }
232
233 }
234
Popular Tags