1 19 20 21 package org.netbeans.editor.ext; 22 23 import java.awt.Color ; 24 import java.io.IOException ; 25 import java.io.BufferedReader ; 26 import java.io.StringReader ; 27 import java.awt.Insets ; 28 import java.awt.Rectangle ; 29 import java.io.Reader ; 30 31 import javax.swing.JEditorPane ; 32 import javax.swing.SwingUtilities ; 33 import javax.swing.text.html.HTMLEditorKit ; 34 import javax.swing.text.BadLocationException ; 35 import javax.swing.text.Document ; 36 import javax.swing.text.EditorKit ; 37 38 45 public class HTMLJavaDocView extends JEditorPane implements JavaDocView { 46 47 private HTMLEditorKit htmlKit; 48 49 50 public HTMLJavaDocView(Color bgColor) { 51 setEditable(false); 52 setBGColor(bgColor); 53 setMargin(new Insets (0,3,3,3)); 54 } 55 56 57 public void setContent(final String content) { 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 scrollRectToVisible(new Rectangle (0,0,0,0)); 67 }catch(IOException ioe){ 68 ioe.printStackTrace(); 69 }catch(BadLocationException ble){ 70 ble.printStackTrace(); 71 } 72 } 73 }); 74 } 75 76 77 public void setBGColor(Color bgColor) { 78 setBackground(bgColor); 79 } 80 81 protected EditorKit createDefaultEditorKit() { 82 if (htmlKit == null){ 85 htmlKit= new HTMLEditorKit (); 86 setEditorKit(htmlKit); 87 88 91 96 if (htmlKit.getStyleSheet().getStyleSheets() != null) 97 return htmlKit; 98 99 javax.swing.text.html.StyleSheet css = new javax.swing.text.html.StyleSheet (); 100 java.awt.Font f = getFont(); 101 css.addRule(new StringBuffer ("body { font-size: ").append(f.getSize()) .append("; font-family: ").append(f.getName()).append("; }").toString()); css.addStyleSheet(htmlKit.getStyleSheet()); 104 htmlKit.setStyleSheet(css); 105 } 106 return htmlKit; 107 } 108 } 109 | Popular Tags |