1 package org.columba.core.gui.htmlviewer; 2 3 import java.io.IOException ; 4 import java.io.Reader ; 5 import java.io.StringReader ; 6 import java.net.URL ; 7 8 import javax.swing.BorderFactory ; 9 import javax.swing.JComponent ; 10 import javax.swing.JScrollPane ; 11 import javax.swing.JTextPane ; 12 import javax.swing.text.BadLocationException ; 13 import javax.swing.text.Document ; 14 import javax.swing.text.Element ; 15 import javax.swing.text.ElementIterator ; 16 import javax.swing.text.MutableAttributeSet ; 17 import javax.swing.text.StyleContext ; 18 import javax.swing.text.html.HTML ; 19 import javax.swing.text.html.HTMLDocument ; 20 import javax.swing.text.html.HTMLEditorKit ; 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 implements 27 IHTMLViewerPlugin { 28 29 private HTMLEditorKit htmlEditorKit; 30 31 private AsynchronousHTMLDocument doc; 32 33 private JTextPane textPane = new JTextPane (); 34 35 public JavaHTMLViewerPlugin() { 36 super(); 37 38 39 40 setViewportView(textPane); 41 textPane.setEditable(false); 43 44 htmlEditorKit = new HTMLEditorKit (); 45 textPane.setEditorKit(htmlEditorKit); 46 47 textPane.setContentType("text/html"); 48 49 setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 50 } 51 52 59 60 private void postView() { 61 URL baseUrl = DiskIO.getResourceURL("org/columba/core/icons/MISC/"); 64 65 ((HTMLDocument ) textPane.getDocument()).setBase(baseUrl); 66 67 textPane.setCaretPosition(0); 69 } 70 71 public void view(String text) { 72 if (text == null) 73 return; 74 75 doc = new AsynchronousHTMLDocument(); 76 77 Reader rd = new StringReader (text); 78 try { 79 htmlEditorKit.read(rd, doc, 0); 80 } catch (BadLocationException e) { 81 e.printStackTrace(); 83 } catch (IOException e) { 84 e.printStackTrace(); 86 } 87 textPane.setDocument(doc); 88 89 postView(); 90 } 91 92 public JComponent getComponent() { 93 return textPane; 94 } 95 96 107 108 public class AsynchronousHTMLDocument extends HTMLDocument { 109 110 113 public AsynchronousHTMLDocument() { 114 super(); 115 putProperty("IgnoreCharsetDirective", new Boolean (true)); 116 } 117 118 123 public HTMLEditorKit.ParserCallback getReader(int pos) { 124 Object desc = getProperty(Document.StreamDescriptionProperty); 125 if (desc instanceof URL ) { 126 setBase((URL )desc); 127 } 128 HTMLReader reader = new MyReader(pos); 129 return reader; 130 } 131 132 public class MyReader extends HTMLDocument.HTMLReader { 133 public MyReader(int pos){super(pos);} 134 135 protected void addSpecialElement(HTML.Tag t, MutableAttributeSet 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 159 public int getAsynchronousLoadPriority() { 160 return 10; 161 } 162 163 public String getTextWithLineBreaks(int start, int end) 164 throws BadLocationException { 165 StringBuffer result = new StringBuffer (end - start); 166 ElementIterator iter = new ElementIterator (this); 167 168 for (iter.next(); iter.current() != null; iter.next()) { 170 Element e = iter.current(); 171 if (e.isLeaf() 172 && (e.getStartOffset() >= start || e.getEndOffset() >= start) 173 && e.getStartOffset() <= end) { 174 Object 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 198 public String getSelectedText() { 199 try { 200 return doc.getTextWithLineBreaks(textPane.getSelectionStart(), 201 textPane.getSelectionEnd()); 202 } catch (BadLocationException e) { 203 return ""; 204 } 205 } 206 207 public JComponent getContainer() { 208 return this; 209 } 210 211 public String getText() { 212 try { 213 return doc.getTextWithLineBreaks(0,doc.getLength()); 214 } catch (BadLocationException e) { 215 return ""; 216 } 217 } 218 219 222 public void setCaretPosition(int position) { 223 textPane.setCaretPosition(position); 224 } 225 226 229 public void moveCaretPosition(int position) { 230 textPane.moveCaretPosition(position); 231 } 232 233 } 234 | Popular Tags |