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