View Javadoc

1   package org.xmlhammer.gui.util;
2   
3   import java.awt.Color;
4   import java.awt.Font;
5   import java.io.IOException;
6   import java.io.OutputStream;
7   import java.io.PrintStream;
8   import java.util.HashMap;
9   
10  import javax.swing.JTextPane;
11  import javax.swing.text.BadLocationException;
12  import javax.swing.text.Document;
13  import javax.swing.text.SimpleAttributeSet;
14  import javax.swing.text.StyleConstants;
15  import javax.swing.text.StyledDocument;
16  
17  public class ConsolePane extends JTextPane {
18  
19      private static final long serialVersionUID = 1513033943583921042L;
20      
21      private static final PrintStream ORIGINAL_OUT = System.out;
22      private static final PrintStream ORIGINAL_ERR = System.err;
23  
24      private static OutputStreamProxy errOutputStreamProxy = null;
25      private static OutputStreamProxy outOutputStreamProxy = null;
26  
27      public ConsolePane() {
28          super();
29  
30          init();
31      }
32  
33      public ConsolePane(StyledDocument arg0) {
34          super(arg0);
35          
36          init();
37      }
38      
39      private void init() {
40          Font f = getFont();
41          setFont(new Font("Monospaced", f.getStyle(), f.getSize()));
42      }
43      
44      public void start(Thread thread) {
45          getErrStreamProxy().put(thread, createErrStream());
46          getOutStreamProxy().put(thread, createOutStream());
47      }
48  
49      public void stop(Thread thread) {
50          getErrStreamProxy().remove(thread);
51          getOutStreamProxy().remove(thread);
52      }
53      
54      
55      private static OutputStreamProxy getErrStreamProxy() {
56          if (errOutputStreamProxy == null) {
57              errOutputStreamProxy = new OutputStreamProxy(ORIGINAL_ERR);
58              System.setErr(new PrintStream(errOutputStreamProxy));
59          }
60          
61          return errOutputStreamProxy;
62      }
63  
64      private static OutputStreamProxy getOutStreamProxy() {
65          if (outOutputStreamProxy == null) {
66              outOutputStreamProxy = new OutputStreamProxy(ORIGINAL_OUT);
67              System.setOut(new PrintStream(outOutputStreamProxy));
68          }
69          
70          return outOutputStreamProxy;
71      }
72  
73      private PrintStream createErrStream() {
74          SimpleAttributeSet errAttributes = new SimpleAttributeSet();
75          StyleConstants.setForeground(errAttributes, Color.red);
76          
77          return new PrintStream(new OutputStreamAdapter(this.getDocument(), errAttributes));
78      }
79      
80      private PrintStream createOutStream() {
81          SimpleAttributeSet attributes = new SimpleAttributeSet();
82          StyleConstants.setForeground(attributes, Color.blue);
83          
84          return new PrintStream(new OutputStreamAdapter(this.getDocument(), attributes));
85      }
86      
87      public class OutputStreamAdapter extends OutputStream {
88          private Document document = null;
89          private SimpleAttributeSet attributes = null;
90          
91          public OutputStreamAdapter(Document document, SimpleAttributeSet attributes) {
92              this.document = document;
93              this.attributes = attributes;
94          }
95  
96          public void write(byte bytes[], int off, int len) {
97              ORIGINAL_OUT.println("ID = "+Thread.currentThread().getId());
98              try {
99                  document.insertString(document.getLength(), new String(bytes, off, len), attributes);
100             } catch (BadLocationException e) {
101                 // Should never happen
102             }
103         }
104 
105         public void write(int c) {
106             ORIGINAL_OUT.println("ID = "+Thread.currentThread().getId());
107             try {
108                 document.insertString(document.getLength(), new String(new byte[] {(byte)c}), attributes);
109             } catch (BadLocationException e) {
110                 // Should never happen
111             }
112         }
113     }
114     
115     public static class OutputStreamProxy extends OutputStream {
116         private OutputStream original = null;
117         private HashMap<Long, OutputStream> map = null; 
118 
119         public OutputStreamProxy(OutputStream original) {
120             map = new HashMap<Long, OutputStream>();
121             this.original = original;
122         }
123 
124         public void write(byte bytes[], int off, int len) throws IOException {
125             OutputStream stream = map.get(Thread.currentThread().getId());
126             
127             if (stream == null) {
128                 original.write(bytes, off, len);
129             } else {
130                 stream.write(bytes, off, len);
131             }
132         }
133 
134         public void write(int c) throws IOException {
135             OutputStream stream = map.get(Thread.currentThread().getId());
136             
137             if (stream == null) {
138                 original.write(c);
139             } else {
140                 stream.write(c);
141             }
142         }
143 
144         public void put(Thread thread, OutputStream stream) {
145             map.put(thread.getId(), stream);
146         }
147 
148         public void remove(Thread thread) {
149             map.remove(thread.getId());
150         }
151     }
152 }