1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.util; |
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
import java.awt.BorderLayout; |
34 |
|
import java.awt.Button; |
35 |
|
import java.awt.Dimension; |
36 |
|
import java.awt.Frame; |
37 |
|
import java.awt.GraphicsEnvironment; |
38 |
|
import java.awt.Panel; |
39 |
|
import java.awt.TextArea; |
40 |
|
import java.awt.Toolkit; |
41 |
|
import java.awt.event.ActionEvent; |
42 |
|
import java.awt.event.ActionListener; |
43 |
|
import java.awt.event.WindowAdapter; |
44 |
|
import java.awt.event.WindowEvent; |
45 |
|
import java.awt.event.WindowListener; |
46 |
|
import java.io.IOException; |
47 |
|
import java.io.PipedInputStream; |
48 |
|
import java.io.PipedOutputStream; |
49 |
|
import java.io.PrintStream; |
50 |
|
|
51 |
|
import jalview.bin.Jalview; |
52 |
|
import jalview.bin.Jalview.ExitCode; |
53 |
|
|
|
|
| 0% |
Uncovered Elements: 116 (116) |
Complexity: 29 |
Complexity Density: 0.33 |
|
54 |
|
public class AWTConsole extends WindowAdapter |
55 |
|
implements WindowListener, ActionListener, Runnable |
56 |
|
{ |
57 |
|
private Frame frame; |
58 |
|
|
59 |
|
private TextArea textArea; |
60 |
|
|
61 |
|
private Thread reader; |
62 |
|
|
63 |
|
private Thread reader2; |
64 |
|
|
65 |
|
private boolean quit; |
66 |
|
|
67 |
|
private final PipedInputStream pin = new PipedInputStream(); |
68 |
|
|
69 |
|
private final PipedInputStream pin2 = new PipedInputStream(); |
70 |
|
|
71 |
|
Thread errorThrower; |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 46 (46) |
Complexity: 6 |
Complexity Density: 0.14 |
|
73 |
0 |
public AWTConsole()... |
74 |
|
{ |
75 |
|
|
76 |
0 |
frame = new Frame("Java Console"); |
77 |
0 |
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
78 |
0 |
Dimension frameSize = new Dimension(screenSize.width / 2, |
79 |
|
screenSize.height / 2); |
80 |
0 |
int x = frameSize.width / 2; |
81 |
0 |
int y = frameSize.height / 2; |
82 |
0 |
frame.setBounds(x, y, frameSize.width, frameSize.height); |
83 |
|
|
84 |
0 |
textArea = new TextArea(); |
85 |
0 |
textArea.setEditable(false); |
86 |
0 |
Button button = new Button("clear"); |
87 |
|
|
88 |
0 |
Panel panel = new Panel(); |
89 |
0 |
panel.setLayout(new BorderLayout()); |
90 |
0 |
panel.add(textArea, BorderLayout.CENTER); |
91 |
0 |
panel.add(button, BorderLayout.SOUTH); |
92 |
0 |
frame.add(panel); |
93 |
|
|
94 |
0 |
frame.setVisible(true); |
95 |
|
|
96 |
0 |
frame.addWindowListener(this); |
97 |
0 |
button.addActionListener(this); |
98 |
|
|
99 |
0 |
try |
100 |
|
{ |
101 |
0 |
PipedOutputStream pout = new PipedOutputStream(this.pin); |
102 |
0 |
System.setOut(new PrintStream(pout, true)); |
103 |
|
} catch (java.io.IOException io) |
104 |
|
{ |
105 |
0 |
textArea.append("Couldn't redirect STDOUT to this console\n" |
106 |
|
+ io.getMessage()); |
107 |
|
} catch (SecurityException se) |
108 |
|
{ |
109 |
0 |
textArea.append("Couldn't redirect STDOUT to this console\n" |
110 |
|
+ se.getMessage()); |
111 |
|
} |
112 |
|
|
113 |
0 |
try |
114 |
|
{ |
115 |
0 |
PipedOutputStream pout2 = new PipedOutputStream(this.pin2); |
116 |
0 |
System.setErr(new PrintStream(pout2, true)); |
117 |
|
} catch (java.io.IOException io) |
118 |
|
{ |
119 |
0 |
textArea.append("Couldn't redirect STDERR to this console\n" |
120 |
|
+ io.getMessage()); |
121 |
|
} catch (SecurityException se) |
122 |
|
{ |
123 |
0 |
textArea.append("Couldn't redirect STDERR to this console\n" |
124 |
|
+ se.getMessage()); |
125 |
|
} |
126 |
|
|
127 |
0 |
quit = false; |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
0 |
reader = new Thread(this); |
132 |
0 |
reader.setDaemon(true); |
133 |
0 |
reader.start(); |
134 |
|
|
135 |
0 |
reader2 = new Thread(this); |
136 |
0 |
reader2.setDaemon(true); |
137 |
0 |
reader2.start(); |
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
0 |
jalview.bin.Console.outPrintln("Hello World 2"); |
143 |
0 |
jalview.bin.Console.outPrintln("All fonts available to Graphic2D:\n"); |
144 |
0 |
GraphicsEnvironment ge = GraphicsEnvironment |
145 |
|
.getLocalGraphicsEnvironment(); |
146 |
0 |
String[] fontNames = ge.getAvailableFontFamilyNames(); |
147 |
0 |
for (int n = 0; n < fontNames.length; n++) |
148 |
|
{ |
149 |
0 |
jalview.bin.Console.outPrintln(fontNames[n]); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
0 |
jalview.bin.Console.outPrintln("\nLets throw an error on this console"); |
156 |
0 |
errorThrower = new Thread(this); |
157 |
0 |
errorThrower.setDaemon(true); |
158 |
0 |
errorThrower.start(); |
159 |
|
} |
160 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.33 |
|
161 |
0 |
@Override... |
162 |
|
public synchronized void windowClosed(WindowEvent evt) |
163 |
|
{ |
164 |
0 |
quit = true; |
165 |
0 |
this.notifyAll(); |
166 |
0 |
try |
167 |
|
{ |
168 |
0 |
reader.join(1000); |
169 |
0 |
pin.close(); |
170 |
|
} catch (Exception e) |
171 |
|
{ |
172 |
|
} |
173 |
0 |
try |
174 |
|
{ |
175 |
0 |
reader2.join(1000); |
176 |
0 |
pin2.close(); |
177 |
|
} catch (Exception e) |
178 |
|
{ |
179 |
|
} |
180 |
0 |
Jalview.exit("Window closing. Bye!", ExitCode.OK); |
181 |
|
} |
182 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
183 |
0 |
@Override... |
184 |
|
public synchronized void windowClosing(WindowEvent evt) |
185 |
|
{ |
186 |
0 |
frame.setVisible(false); |
187 |
0 |
frame.dispose(); |
188 |
|
} |
189 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
190 |
0 |
@Override... |
191 |
|
public synchronized void actionPerformed(ActionEvent evt) |
192 |
|
{ |
193 |
0 |
textArea.setText(""); |
194 |
|
} |
195 |
|
|
|
|
| 0% |
Uncovered Elements: 37 (37) |
Complexity: 12 |
Complexity Density: 0.52 |
|
196 |
0 |
@Override... |
197 |
|
public synchronized void run() |
198 |
|
{ |
199 |
0 |
try |
200 |
|
{ |
201 |
0 |
while (Thread.currentThread() == reader) |
202 |
|
{ |
203 |
0 |
try |
204 |
|
{ |
205 |
0 |
this.wait(100); |
206 |
|
} catch (InterruptedException ie) |
207 |
|
{ |
208 |
|
} |
209 |
0 |
if (pin.available() != 0) |
210 |
|
{ |
211 |
0 |
String input = this.readLine(pin); |
212 |
0 |
textArea.append(input); |
213 |
|
} |
214 |
0 |
if (quit) |
215 |
|
{ |
216 |
0 |
return; |
217 |
|
} |
218 |
|
} |
219 |
|
|
220 |
0 |
while (Thread.currentThread() == reader2) |
221 |
|
{ |
222 |
0 |
try |
223 |
|
{ |
224 |
0 |
this.wait(100); |
225 |
|
} catch (InterruptedException ie) |
226 |
|
{ |
227 |
|
} |
228 |
0 |
if (pin2.available() != 0) |
229 |
|
{ |
230 |
0 |
String input = this.readLine(pin2); |
231 |
0 |
textArea.append(input); |
232 |
|
} |
233 |
0 |
if (quit) |
234 |
|
{ |
235 |
0 |
return; |
236 |
|
} |
237 |
|
} |
238 |
|
} catch (Exception e) |
239 |
|
{ |
240 |
0 |
textArea.append("\nConsole reports an Internal error."); |
241 |
0 |
textArea.append("The error is: " + e); |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
0 |
if (Thread.currentThread() == errorThrower) |
246 |
|
{ |
247 |
0 |
try |
248 |
|
{ |
249 |
0 |
this.wait(1000); |
250 |
|
} catch (InterruptedException ie) |
251 |
|
{ |
252 |
|
} |
253 |
0 |
throw new NullPointerException( |
254 |
|
MessageManager.getString("exception.application_test_npe")); |
255 |
|
} |
256 |
|
|
257 |
|
} |
258 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 5 |
Complexity Density: 0.56 |
|
259 |
0 |
public synchronized String readLine(PipedInputStream in)... |
260 |
|
throws IOException |
261 |
|
{ |
262 |
0 |
String input = ""; |
263 |
0 |
do |
264 |
|
{ |
265 |
0 |
int available = in.available(); |
266 |
0 |
if (available == 0) |
267 |
|
{ |
268 |
0 |
break; |
269 |
|
} |
270 |
0 |
byte b[] = new byte[available]; |
271 |
0 |
in.read(b); |
272 |
0 |
input = input + new String(b, 0, b.length); |
273 |
0 |
} while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit); |
274 |
0 |
return input; |
275 |
|
} |
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
@param |
280 |
|
|
281 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
282 |
0 |
public static void main(String[] arg)... |
283 |
|
{ |
284 |
0 |
new AWTConsole(); |
285 |
|
} |
286 |
|
} |