1 19 20 21 package org.netbeans.modules.editor.completion; 22 23 import java.awt.Color ; 24 import java.awt.Insets ; 25 import java.awt.Rectangle ; 26 import java.io.IOException ; 27 import java.io.Reader ; 28 import java.io.StringReader ; 29 30 import javax.swing.JEditorPane ; 31 import javax.swing.SwingUtilities ; 32 import javax.swing.text.BadLocationException ; 33 import javax.swing.text.Document ; 34 import javax.swing.text.EditorKit ; 35 import javax.swing.text.html.HTMLEditorKit ; 36 37 44 public class HTMLDocView extends JEditorPane { 45 46 private HTMLEditorKit htmlKit; 47 48 49 public HTMLDocView(Color bgColor) { 50 setEditable(false); 51 setFocusable(false); 52 setBackground(bgColor); 53 setMargin(new Insets (0,3,3,3)); 54 } 55 56 57 public void setContent(final String content, final String reference) { 58 SwingUtilities.invokeLater(new Runnable (){ 59 public void run(){ 60 Reader in = new StringReader ("<HTML><BODY>"+content+"</BODY></HTML>"); try{ 62 Document doc = getDocument(); 63 doc.remove(0, doc.getLength()); 64 getEditorKit().read(in, getDocument(), 0); setCaretPosition(0); 66 if (reference != null) { 67 SwingUtilities.invokeLater(new Runnable (){ 68 public void run(){ 69 scrollToReference(reference); 70 } 71 }); 72 } else { 73 scrollRectToVisible(new Rectangle (0,0,0,0)); 74 } 75 }catch(IOException ioe){ 76 ioe.printStackTrace(); 77 }catch(BadLocationException ble){ 78 ble.printStackTrace(); 79 } 80 } 81 }); 82 } 83 84 protected EditorKit createDefaultEditorKit() { 85 if (htmlKit == null){ 87 htmlKit= new HTMLEditorKit (); 88 setEditorKit(htmlKit); 89 90 93 98 if (htmlKit.getStyleSheet().getStyleSheets() != null) 99 return htmlKit; 100 101 javax.swing.text.html.StyleSheet css = new javax.swing.text.html.StyleSheet (); 102 java.awt.Font f = getFont(); 103 css.addRule(new StringBuffer ("body { font-size: ").append(f.getSize()) .append("; font-family: ").append(f.getName()).append("; }").toString()); css.addStyleSheet(htmlKit.getStyleSheet()); 106 htmlKit.setStyleSheet(css); 107 } 108 return htmlKit; 109 } 110 } 111 | Popular Tags |