gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.swing / org.gvsig.scripting.swing.impl / src / main / java / org / gvsig / scripting / swing / impl / DefaultJScriptingConsole.java @ 478
History | View | Annotate | Download (4.32 KB)
1 |
package org.gvsig.scripting.swing.impl; |
---|---|
2 |
|
3 |
import org.gvsig.scripting.swing.api.JScriptingConsole; |
4 |
import java.awt.BorderLayout; |
5 |
import java.awt.Color; |
6 |
import java.awt.Font; |
7 |
import java.awt.event.ComponentEvent; |
8 |
import java.awt.event.ComponentListener; |
9 |
import java.io.IOException; |
10 |
import java.io.OutputStream; |
11 |
import java.io.PrintStream; |
12 |
import javax.swing.JComponent; |
13 |
import javax.swing.JPanel; |
14 |
import javax.swing.JScrollPane; |
15 |
import javax.swing.JTextPane; |
16 |
import javax.swing.text.AttributeSet; |
17 |
import javax.swing.text.SimpleAttributeSet; |
18 |
import javax.swing.text.StyleConstants; |
19 |
import javax.swing.text.StyleContext; |
20 |
|
21 |
|
22 |
public class DefaultJScriptingConsole extends JPanel implements JScriptingConsole { |
23 |
|
24 |
private JTextAreaOutputStream stdout = null; |
25 |
private JTextAreaOutputStream stderr = null; |
26 |
private JTextPane console; |
27 |
|
28 |
public DefaultJScriptingConsole() {
|
29 |
|
30 |
this.initComponents();
|
31 |
|
32 |
this.stdout = new JTextAreaOutputStream(System.out, Color.BLACK); |
33 |
this.stderr = new JTextAreaOutputStream(System.err, Color.RED.darker()); |
34 |
|
35 |
|
36 |
this.addComponentListener(new ComponentListener() { |
37 |
|
38 |
@Override
|
39 |
public void componentResized(ComponentEvent e) { |
40 |
} |
41 |
|
42 |
@Override
|
43 |
public void componentMoved(ComponentEvent e) { |
44 |
} |
45 |
|
46 |
@Override
|
47 |
public void componentShown(ComponentEvent e) { |
48 |
if( System.out != stdout.getPrintStream() ) { |
49 |
System.setOut(stdout.getPrintStream(System.out)); |
50 |
} |
51 |
if( System.err != stderr.getPrintStream() ) { |
52 |
System.setErr(stderr.getPrintStream(System.err)); |
53 |
} |
54 |
} |
55 |
|
56 |
@Override
|
57 |
public void componentHidden(ComponentEvent e) { |
58 |
System.setOut(stdout.getOriginalPS());
|
59 |
System.setErr(stderr.getOriginalPS());
|
60 |
} |
61 |
}); |
62 |
} |
63 |
|
64 |
private void initComponents(){ |
65 |
this.setLayout(new BorderLayout()); |
66 |
console = new JTextPane(); |
67 |
console.setFont(new Font("monospaced", Font.PLAIN, 13)); |
68 |
JScrollPane scrollConsole = new JScrollPane(console); |
69 |
this.add(scrollConsole, BorderLayout.CENTER); |
70 |
} |
71 |
|
72 |
@Override
|
73 |
public void clear() { |
74 |
this.console.setText(""); |
75 |
} |
76 |
|
77 |
@Override
|
78 |
public void append(String s) { |
79 |
append(s, stdout.color); |
80 |
} |
81 |
|
82 |
@Override
|
83 |
public void append(String msg, Color color) { |
84 |
StyleContext sc = StyleContext.getDefaultStyleContext(); |
85 |
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, color); |
86 |
|
87 |
int len = console.getDocument().getLength();
|
88 |
console.setCaretPosition(len); |
89 |
console.setCharacterAttributes(aset, false);
|
90 |
console.replaceSelection(msg); |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public PrintStream getStdout() { |
95 |
return this.stdout.getPrintStream(); |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public PrintStream getStderr() { |
100 |
return this.stderr.getPrintStream(); |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public JComponent asJComponent() { |
105 |
return this; |
106 |
} |
107 |
|
108 |
private class JTextAreaOutputStream extends OutputStream { |
109 |
|
110 |
PrintStream printStream = null; |
111 |
PrintStream originalPS;
|
112 |
private final Color color; |
113 |
|
114 |
public JTextAreaOutputStream(PrintStream originalPS, Color color) { |
115 |
super();
|
116 |
this.originalPS = originalPS;
|
117 |
this.color = color;
|
118 |
} |
119 |
|
120 |
@Override
|
121 |
public void write(int i) throws IOException { |
122 |
append(Character.toString((char) i), color); |
123 |
originalPS.write(i); |
124 |
} |
125 |
|
126 |
@Override
|
127 |
public void write(byte[] buf, int off, int len) throws IOException { |
128 |
String s = new String(buf, off, len); |
129 |
append(s, color); |
130 |
originalPS.write(buf, off, len); |
131 |
} |
132 |
|
133 |
public PrintStream getPrintStream() { |
134 |
if( this.printStream == null ) { |
135 |
this.printStream = new PrintStream(this); |
136 |
} |
137 |
return this.printStream; |
138 |
} |
139 |
|
140 |
public PrintStream getPrintStream(PrintStream originalPS) { |
141 |
this.originalPS = originalPS;
|
142 |
return this.getPrintStream(); |
143 |
} |
144 |
|
145 |
public PrintStream getOriginalPS() { |
146 |
return this.originalPS; |
147 |
} |
148 |
} |
149 |
|
150 |
|
151 |
} |