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 @ 650
History | View | Annotate | Download (5.39 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.Component; |
7 |
import java.awt.Font; |
8 |
import java.awt.event.ComponentEvent; |
9 |
import java.awt.event.ComponentListener; |
10 |
import java.io.IOException; |
11 |
import java.io.OutputStream; |
12 |
import java.io.PrintStream; |
13 |
import javax.swing.JComponent; |
14 |
import javax.swing.JPanel; |
15 |
import javax.swing.JScrollPane; |
16 |
import javax.swing.JTextPane; |
17 |
import javax.swing.plaf.ComponentUI; |
18 |
import javax.swing.text.AttributeSet; |
19 |
import javax.swing.text.SimpleAttributeSet; |
20 |
import javax.swing.text.StyleConstants; |
21 |
import javax.swing.text.StyleContext; |
22 |
|
23 |
public class DefaultJScriptingConsole extends JPanel implements JScriptingConsole { |
24 |
private static final long serialVersionUID = -1181983617272717357L; |
25 |
|
26 |
private JTextAreaOutputStream stdout = null; |
27 |
private JTextAreaOutputStream stderr = null; |
28 |
private JTextPane console; |
29 |
private boolean captureOutput; |
30 |
|
31 |
public DefaultJScriptingConsole(boolean captureOutput) { |
32 |
this.captureOutput = captureOutput;
|
33 |
|
34 |
this.initComponents();
|
35 |
|
36 |
this.stdout = new JTextAreaOutputStream(System.out, Color.BLACK); |
37 |
this.stderr = new JTextAreaOutputStream(System.err, Color.RED.darker()); |
38 |
|
39 |
this.addComponentListener(new ComponentListener() { |
40 |
|
41 |
@Override
|
42 |
public void componentResized(ComponentEvent e) { |
43 |
} |
44 |
|
45 |
@Override
|
46 |
public void componentMoved(ComponentEvent e) { |
47 |
} |
48 |
|
49 |
@Override
|
50 |
public void componentShown(ComponentEvent e) { |
51 |
if( canCaptureOutput() ) {
|
52 |
if ( System.out != stdout.getPrintStream()) { |
53 |
System.setOut(stdout.getPrintStream(System.out)); |
54 |
} |
55 |
if ( System.err != stderr.getPrintStream()) { |
56 |
System.setErr(stderr.getPrintStream(System.err)); |
57 |
} |
58 |
} |
59 |
} |
60 |
|
61 |
@Override
|
62 |
public void componentHidden(ComponentEvent e) { |
63 |
if( canCaptureOutput() ) {
|
64 |
if ( System.out != stdout.getPrintStream()) { |
65 |
System.setOut(stdout.getOriginalPS());
|
66 |
} |
67 |
if ( System.err != stderr.getPrintStream()) { |
68 |
System.setErr(stderr.getOriginalPS());
|
69 |
} |
70 |
} |
71 |
} |
72 |
}); |
73 |
} |
74 |
|
75 |
private boolean canCaptureOutput() { |
76 |
return this.captureOutput; |
77 |
} |
78 |
|
79 |
private void initComponents() { |
80 |
this.setLayout(new BorderLayout()); |
81 |
console = new JTextPane() { |
82 |
private static final long serialVersionUID = -1309476113717086242L; |
83 |
|
84 |
// https://tips4java.wordpress.com/2009/01/25/no-wrap-text-pane/
|
85 |
|
86 |
@Override
|
87 |
public boolean getScrollableTracksViewportWidth() { |
88 |
Component parent = getParent();
|
89 |
ComponentUI ui = getUI();
|
90 |
|
91 |
return parent != null ? (ui.getPreferredSize(this).width <= parent |
92 |
.getSize().width) : true;
|
93 |
} |
94 |
}; |
95 |
console.setFont(new Font("monospaced", Font.PLAIN, 13)); |
96 |
JScrollPane scrollConsole = new JScrollPane(console); |
97 |
this.add(scrollConsole, BorderLayout.CENTER); |
98 |
} |
99 |
|
100 |
@Override
|
101 |
public void clear() { |
102 |
this.console.setText(""); |
103 |
} |
104 |
|
105 |
@Override
|
106 |
public void append(String s) { |
107 |
append(s, stdout.color); |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public void append(String msg, Color color) { |
112 |
StyleContext sc = StyleContext.getDefaultStyleContext(); |
113 |
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, color); |
114 |
|
115 |
int len = console.getDocument().getLength();
|
116 |
console.setCaretPosition(len); |
117 |
console.setCharacterAttributes(aset, false);
|
118 |
console.replaceSelection(msg); |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public PrintStream getStdout() { |
123 |
return this.stdout.getPrintStream(); |
124 |
} |
125 |
|
126 |
@Override
|
127 |
public PrintStream getStderr() { |
128 |
return this.stderr.getPrintStream(); |
129 |
} |
130 |
|
131 |
@Override
|
132 |
public JComponent asJComponent() { |
133 |
return this; |
134 |
} |
135 |
|
136 |
private class JTextAreaOutputStream extends OutputStream { |
137 |
|
138 |
PrintStream printStream = null; |
139 |
PrintStream originalPS;
|
140 |
private final Color color; |
141 |
|
142 |
public JTextAreaOutputStream(PrintStream originalPS, Color color) { |
143 |
super();
|
144 |
this.originalPS = originalPS;
|
145 |
this.color = color;
|
146 |
} |
147 |
|
148 |
@Override
|
149 |
public void write(int i) throws IOException { |
150 |
append(Character.toString((char) i), color); |
151 |
originalPS.write(i); |
152 |
} |
153 |
|
154 |
@Override
|
155 |
public void write(byte[] buf, int off, int len) throws IOException { |
156 |
String s = new String(buf, off, len); |
157 |
append(s, color); |
158 |
originalPS.write(buf, off, len); |
159 |
} |
160 |
|
161 |
public PrintStream getPrintStream() { |
162 |
if (this.printStream == null) { |
163 |
this.printStream = new PrintStream(this); |
164 |
} |
165 |
return this.printStream; |
166 |
} |
167 |
|
168 |
public PrintStream getPrintStream(PrintStream originalPS) { |
169 |
this.originalPS = originalPS;
|
170 |
return this.getPrintStream(); |
171 |
} |
172 |
|
173 |
public PrintStream getOriginalPS() { |
174 |
return this.originalPS; |
175 |
} |
176 |
} |
177 |
|
178 |
} |