Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 15 | arune | 1 | /** |
| 2 | * File created by Jimmy |
||
| 3 | * at 2004-jan-04 20:22:22 |
||
| 4 | */ |
||
| 5 | package Macbeth.Utilities; |
||
| 6 | |||
| 7 | import javax.swing.*; |
||
| 8 | import javax.swing.text.Document; |
||
| 9 | import javax.swing.text.JTextComponent; |
||
| 10 | import javax.swing.text.BadLocationException; |
||
| 11 | import javax.swing.text.Element; |
||
| 12 | import javax.swing.text.html.HTMLDocument; |
||
| 13 | import javax.swing.text.html.HTMLEditorKit; |
||
| 14 | import javax.swing.text.html.HTML; |
||
| 15 | import java.io.PrintStream; |
||
| 16 | import java.io.PipedOutputStream; |
||
| 17 | import java.io.IOException; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * A stream that writes the text to a JEditorPane. |
||
| 21 | * @author Jimmy |
||
| 22 | */ |
||
| 23 | public class EditorPaneStream extends PrintStream { |
||
| 24 | |||
| 25 | //the JEditorPane to which we will write all text |
||
| 26 | private JEditorPane editorPane; |
||
| 27 | private HTMLEditorKit editorKit; |
||
| 28 | private HTMLDocument htmlDocument; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Creates a new instance of EditorPaneStream. |
||
| 32 | * @param editorPane The JEditorPane to which all text |
||
| 33 | * should be written. |
||
| 34 | */ |
||
| 35 | public EditorPaneStream(JEditorPane editorPane) { |
||
| 36 | //construct PrintWriter with non-used PipedOutputStream |
||
| 37 | super(new PipedOutputStream()); |
||
| 38 | //keep reference to our JTextComponent |
||
| 39 | this.editorPane = editorPane; |
||
| 40 | editorPane.setContentType("text/html"); |
||
| 41 | //get editor kit |
||
| 42 | editorKit = (HTMLEditorKit)editorPane.getEditorKit(); |
||
| 43 | //get html document |
||
| 44 | htmlDocument = (HTMLDocument)editorPane.getDocument(); |
||
| 45 | } |
||
| 46 | |||
| 47 | private void insertString(String string) { |
||
| 48 | try { |
||
| 49 | //editorKit.insertHTML(htmlDocument,htmlDocument.getLength(),string,2,2,null); |
||
| 50 | //htmlDocument.insertBeforeEnd(htmlDocument.getDefaultRootElement().getElement(0),string); |
||
| 51 | editorKit.insertHTML(htmlDocument, htmlDocument.getLength(), string, 0, 0, null); |
||
| 52 | } catch (Exception e) { |
||
| 53 | e.printStackTrace(); |
||
| 54 | } |
||
| 55 | editorPane.setCaretPosition(htmlDocument.getLength()); |
||
| 56 | } |
||
| 57 | |||
| 58 | /* Override all standard print-methods: */ |
||
| 59 | |||
| 60 | public void println(char c[]) { |
||
| 61 | if(c!=null) insertString(new String(c,0,c.length) + MySystem.lineBreak); |
||
| 62 | } |
||
| 63 | |||
| 64 | public void print(char c[]) { |
||
| 65 | if(c!=null) insertString(new String(c,0,c.length)); |
||
| 66 | } |
||
| 67 | |||
| 68 | public void println(double d) { |
||
| 69 | insertString(d + "" + MySystem.lineBreak); |
||
| 70 | } |
||
| 71 | |||
| 72 | public void print(double d) { |
||
| 73 | insertString(d + ""); |
||
| 74 | } |
||
| 75 | |||
| 76 | public void println(float f) { |
||
| 77 | insertString(f + "" + MySystem.lineBreak); |
||
| 78 | } |
||
| 79 | |||
| 80 | public void print(float f) { |
||
| 81 | insertString(f + ""); |
||
| 82 | } |
||
| 83 | |||
| 84 | public void println(char c) { |
||
| 85 | insertString(c + "" + MySystem.lineBreak); |
||
| 86 | } |
||
| 87 | |||
| 88 | public void print(char c) { |
||
| 89 | insertString(c + ""); |
||
| 90 | } |
||
| 91 | |||
| 92 | public void println(boolean b) { |
||
| 93 | insertString(b + "" + MySystem.lineBreak); |
||
| 94 | } |
||
| 95 | |||
| 96 | public void print(boolean b) { |
||
| 97 | insertString(b + ""); |
||
| 98 | } |
||
| 99 | |||
| 100 | public void println(int i) { |
||
| 101 | insertString(i + "" + MySystem.lineBreak); |
||
| 102 | } |
||
| 103 | |||
| 104 | public void print(int i) { |
||
| 105 | insertString(i + ""); |
||
| 106 | } |
||
| 107 | |||
| 108 | public void println(String str) { |
||
| 109 | if(str!=null) insertString(str + MySystem.lineBreak); |
||
| 110 | else insertString("null" + MySystem.lineBreak); |
||
| 111 | } |
||
| 112 | |||
| 113 | public void print(String str) { |
||
| 114 | if(str!=null) insertString(str); |
||
| 115 | else insertString("null"); |
||
| 116 | } |
||
| 117 | |||
| 118 | public void println(Object obj) { |
||
| 119 | if(obj!=null) insertString(obj.toString() + MySystem.lineBreak); |
||
| 120 | else insertString("null" + MySystem.lineBreak); |
||
| 121 | } |
||
| 122 | |||
| 123 | public void print(Object obj) { |
||
| 124 | if(obj!=null) insertString(obj.toString()); |
||
| 125 | else insertString("null"); |
||
| 126 | } |
||
| 127 | |||
| 128 | } |