Revision 650 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
DefaultJScriptingConsole.java | ||
---|---|---|
3 | 3 |
import org.gvsig.scripting.swing.api.JScriptingConsole; |
4 | 4 |
import java.awt.BorderLayout; |
5 | 5 |
import java.awt.Color; |
6 |
import java.awt.Component; |
|
6 | 7 |
import java.awt.Font; |
7 | 8 |
import java.awt.event.ComponentEvent; |
8 | 9 |
import java.awt.event.ComponentListener; |
... | ... | |
13 | 14 |
import javax.swing.JPanel; |
14 | 15 |
import javax.swing.JScrollPane; |
15 | 16 |
import javax.swing.JTextPane; |
17 |
import javax.swing.plaf.ComponentUI; |
|
16 | 18 |
import javax.swing.text.AttributeSet; |
17 | 19 |
import javax.swing.text.SimpleAttributeSet; |
18 | 20 |
import javax.swing.text.StyleConstants; |
19 | 21 |
import javax.swing.text.StyleContext; |
20 | 22 |
|
21 |
|
|
22 | 23 |
public class DefaultJScriptingConsole extends JPanel implements JScriptingConsole { |
24 |
private static final long serialVersionUID = -1181983617272717357L; |
|
23 | 25 |
|
24 | 26 |
private JTextAreaOutputStream stdout = null; |
25 | 27 |
private JTextAreaOutputStream stderr = null; |
26 | 28 |
private JTextPane console; |
29 |
private boolean captureOutput; |
|
27 | 30 |
|
28 |
public DefaultJScriptingConsole() { |
|
31 |
public DefaultJScriptingConsole(boolean captureOutput) { |
|
32 |
this.captureOutput = captureOutput; |
|
33 |
|
|
34 |
this.initComponents(); |
|
29 | 35 |
|
30 |
this.initComponents(); |
|
31 |
|
|
32 | 36 |
this.stdout = new JTextAreaOutputStream(System.out, Color.BLACK); |
33 | 37 |
this.stderr = new JTextAreaOutputStream(System.err, Color.RED.darker()); |
34 |
|
|
35 |
|
|
38 |
|
|
36 | 39 |
this.addComponentListener(new ComponentListener() { |
37 | 40 |
|
38 | 41 |
@Override |
... | ... | |
45 | 48 |
|
46 | 49 |
@Override |
47 | 50 |
public void componentShown(ComponentEvent e) { |
48 |
if( System.out != stdout.getPrintStream() ) { |
|
49 |
System.setOut(stdout.getPrintStream(System.out)); |
|
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 |
} |
|
50 | 58 |
} |
51 |
if( System.err != stderr.getPrintStream() ) { |
|
52 |
System.setErr(stderr.getPrintStream(System.err)); |
|
53 |
} |
|
54 | 59 |
} |
55 | 60 |
|
56 | 61 |
@Override |
57 | 62 |
public void componentHidden(ComponentEvent e) { |
58 |
System.setOut(stdout.getOriginalPS()); |
|
59 |
System.setErr(stderr.getOriginalPS()); |
|
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 |
} |
|
60 | 71 |
} |
61 | 72 |
}); |
62 | 73 |
} |
74 |
|
|
75 |
private boolean canCaptureOutput() { |
|
76 |
return this.captureOutput; |
|
77 |
} |
|
63 | 78 |
|
64 |
private void initComponents(){ |
|
79 |
private void initComponents() {
|
|
65 | 80 |
this.setLayout(new BorderLayout()); |
66 |
console = new JTextPane(); |
|
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 |
}; |
|
67 | 95 |
console.setFont(new Font("monospaced", Font.PLAIN, 13)); |
68 | 96 |
JScrollPane scrollConsole = new JScrollPane(console); |
69 | 97 |
this.add(scrollConsole, BorderLayout.CENTER); |
70 | 98 |
} |
71 |
|
|
99 |
|
|
72 | 100 |
@Override |
73 | 101 |
public void clear() { |
74 | 102 |
this.console.setText(""); |
... | ... | |
78 | 106 |
public void append(String s) { |
79 | 107 |
append(s, stdout.color); |
80 | 108 |
} |
81 |
|
|
109 |
|
|
82 | 110 |
@Override |
83 | 111 |
public void append(String msg, Color color) { |
84 | 112 |
StyleContext sc = StyleContext.getDefaultStyleContext(); |
... | ... | |
87 | 115 |
int len = console.getDocument().getLength(); |
88 | 116 |
console.setCaretPosition(len); |
89 | 117 |
console.setCharacterAttributes(aset, false); |
90 |
console.replaceSelection(msg);
|
|
118 |
console.replaceSelection(msg); |
|
91 | 119 |
} |
92 |
|
|
120 |
|
|
93 | 121 |
@Override |
94 | 122 |
public PrintStream getStdout() { |
95 | 123 |
return this.stdout.getPrintStream(); |
96 | 124 |
} |
97 |
|
|
125 |
|
|
98 | 126 |
@Override |
99 | 127 |
public PrintStream getStderr() { |
100 | 128 |
return this.stderr.getPrintStream(); |
... | ... | |
104 | 132 |
public JComponent asJComponent() { |
105 | 133 |
return this; |
106 | 134 |
} |
107 |
|
|
135 |
|
|
108 | 136 |
private class JTextAreaOutputStream extends OutputStream { |
109 | 137 |
|
110 | 138 |
PrintStream printStream = null; |
... | ... | |
129 | 157 |
append(s, color); |
130 | 158 |
originalPS.write(buf, off, len); |
131 | 159 |
} |
132 |
|
|
160 |
|
|
133 | 161 |
public PrintStream getPrintStream() { |
134 |
if( this.printStream == null ) {
|
|
162 |
if (this.printStream == null) {
|
|
135 | 163 |
this.printStream = new PrintStream(this); |
136 | 164 |
} |
137 | 165 |
return this.printStream; |
... | ... | |
147 | 175 |
} |
148 | 176 |
} |
149 | 177 |
|
150 |
|
|
151 | 178 |
} |
Also available in: Unified diff