JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / jalview / util / AWTConsole.js
1 Clazz.declarePackage ("jalview.util");\r
2 Clazz.load (["java.awt.event.ActionListener", "$.WindowAdapter", "$.WindowListener", "java.io.PipedInputStream"], "jalview.util.AWTConsole", ["jalview.util.MessageManager", "java.awt.BorderLayout", "$.Button", "$.Dimension", "$.Frame", "$.GraphicsEnvironment", "$.Panel", "$.TextArea", "$.Toolkit", "java.io.PipedOutputStream", "java.lang.NullPointerException", "$.Thread"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.frame = null;\r
5 this.textArea = null;\r
6 this.reader = null;\r
7 this.reader2 = null;\r
8 this.quit = false;\r
9 this.pin = null;\r
10 this.pin2 = null;\r
11 this.errorThrower = null;\r
12 Clazz.instantialize (this, arguments);\r
13 }, jalview.util, "AWTConsole", java.awt.event.WindowAdapter, [java.awt.event.WindowListener, java.awt.event.ActionListener, Runnable]);\r
14 Clazz.prepareFields (c$, function () {\r
15 this.pin =  new java.io.PipedInputStream ();\r
16 this.pin2 =  new java.io.PipedInputStream ();\r
17 });\r
18 Clazz.makeConstructor (c$, \r
19 function () {\r
20 Clazz.superConstructor (this, jalview.util.AWTConsole, []);\r
21 this.frame =  new java.awt.Frame ("Java Console");\r
22 var screenSize = java.awt.Toolkit.getDefaultToolkit ().getScreenSize ();\r
23 var frameSize =  new java.awt.Dimension ((Clazz.doubleToInt (screenSize.width / 2)), (Clazz.doubleToInt (screenSize.height / 2)));\r
24 var x = (Clazz.doubleToInt (frameSize.width / 2));\r
25 var y = (Clazz.doubleToInt (frameSize.height / 2));\r
26 this.frame.setBounds (x, y, frameSize.width, frameSize.height);\r
27 this.textArea =  new java.awt.TextArea ();\r
28 this.textArea.setEditable (false);\r
29 var button =  new java.awt.Button ("clear");\r
30 var panel =  new java.awt.Panel ();\r
31 panel.setLayout ( new java.awt.BorderLayout ());\r
32 panel.add (this.textArea, "Center");\r
33 panel.add (button, "South");\r
34 this.frame.add (panel);\r
35 this.frame.setVisible (true);\r
36 this.frame.addWindowListener (this);\r
37 button.addActionListener (this);\r
38 try {\r
39 var pout =  new java.io.PipedOutputStream (this.pin);\r
40 System.setOut ( new java.io.PrintStream (pout, true));\r
41 } catch (e$$) {\r
42 if (Clazz.exceptionOf (e$$, java.io.IOException)) {\r
43 var io = e$$;\r
44 {\r
45 this.textArea.append ("Couldn't redirect STDOUT to this console\n" + io.getMessage ());\r
46 }\r
47 } else if (Clazz.exceptionOf (e$$, SecurityException)) {\r
48 var se = e$$;\r
49 {\r
50 this.textArea.append ("Couldn't redirect STDOUT to this console\n" + se.getMessage ());\r
51 }\r
52 } else {\r
53 throw e$$;\r
54 }\r
55 }\r
56 try {\r
57 var pout2 =  new java.io.PipedOutputStream (this.pin2);\r
58 System.setErr ( new java.io.PrintStream (pout2, true));\r
59 } catch (e$$) {\r
60 if (Clazz.exceptionOf (e$$, java.io.IOException)) {\r
61 var io = e$$;\r
62 {\r
63 this.textArea.append ("Couldn't redirect STDERR to this console\n" + io.getMessage ());\r
64 }\r
65 } else if (Clazz.exceptionOf (e$$, SecurityException)) {\r
66 var se = e$$;\r
67 {\r
68 this.textArea.append ("Couldn't redirect STDERR to this console\n" + se.getMessage ());\r
69 }\r
70 } else {\r
71 throw e$$;\r
72 }\r
73 }\r
74 this.quit = false;\r
75 this.reader =  new Thread (this);\r
76 this.reader.setDaemon (true);\r
77 this.reader.start ();\r
78 this.reader2 =  new Thread (this);\r
79 this.reader2.setDaemon (true);\r
80 this.reader2.start ();\r
81 System.out.println ("Hello World 2");\r
82 System.out.println ("All fonts available to Graphic2D:\n");\r
83 var ge = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment ();\r
84 var fontNames = ge.getAvailableFontFamilyNames ();\r
85 for (var n = 0; n < fontNames.length; n++) System.out.println (fontNames[n]);\r
86 \r
87 System.out.println ("\nLets throw an error on this console");\r
88 this.errorThrower =  new Thread (this);\r
89 this.errorThrower.setDaemon (true);\r
90 this.errorThrower.start ();\r
91 });\r
92 Clazz.overrideMethod (c$, "windowClosed", \r
93 function (evt) {\r
94 this.quit = true;\r
95 this.notifyAll ();\r
96 try {\r
97 this.reader.join (1000);\r
98 this.pin.close ();\r
99 } catch (e) {\r
100 if (Clazz.exceptionOf (e, Exception)) {\r
101 } else {\r
102 throw e;\r
103 }\r
104 }\r
105 try {\r
106 this.reader2.join (1000);\r
107 this.pin2.close ();\r
108 } catch (e) {\r
109 if (Clazz.exceptionOf (e, Exception)) {\r
110 } else {\r
111 throw e;\r
112 }\r
113 }\r
114 System.exit (0);\r
115 }, "java.awt.event.WindowEvent");\r
116 Clazz.overrideMethod (c$, "windowClosing", \r
117 function (evt) {\r
118 this.frame.setVisible (false);\r
119 this.frame.dispose ();\r
120 }, "java.awt.event.WindowEvent");\r
121 Clazz.overrideMethod (c$, "actionPerformed", \r
122 function (evt) {\r
123 this.textArea.setText ("");\r
124 }, "java.awt.event.ActionEvent");\r
125 Clazz.overrideMethod (c$, "run", \r
126 function () {\r
127 try {\r
128 while (Thread.currentThread () === this.reader) {\r
129 try {\r
130 this.wait (100);\r
131 } catch (ie) {\r
132 if (Clazz.exceptionOf (ie, InterruptedException)) {\r
133 } else {\r
134 throw ie;\r
135 }\r
136 }\r
137 if (this.pin.available () != 0) {\r
138 var input = this.readLine (this.pin);\r
139 this.textArea.append (input);\r
140 }if (this.quit) return;\r
141 }\r
142 while (Thread.currentThread () === this.reader2) {\r
143 try {\r
144 this.wait (100);\r
145 } catch (ie) {\r
146 if (Clazz.exceptionOf (ie, InterruptedException)) {\r
147 } else {\r
148 throw ie;\r
149 }\r
150 }\r
151 if (this.pin2.available () != 0) {\r
152 var input = this.readLine (this.pin2);\r
153 this.textArea.append (input);\r
154 }if (this.quit) return;\r
155 }\r
156 } catch (e) {\r
157 if (Clazz.exceptionOf (e, Exception)) {\r
158 this.textArea.append ("\nConsole reports an Internal error.");\r
159 this.textArea.append ("The error is: " + e);\r
160 } else {\r
161 throw e;\r
162 }\r
163 }\r
164 if (Thread.currentThread () === this.errorThrower) {\r
165 try {\r
166 this.wait (1000);\r
167 } catch (ie) {\r
168 if (Clazz.exceptionOf (ie, InterruptedException)) {\r
169 } else {\r
170 throw ie;\r
171 }\r
172 }\r
173 throw  new NullPointerException (jalview.util.MessageManager.getString ("exception.application_test_npe"));\r
174 }});\r
175 Clazz.defineMethod (c$, "readLine", \r
176 function ($in) {\r
177 var input = "";\r
178 do {\r
179 var available = $in.available ();\r
180 if (available == 0) break;\r
181 var b =  Clazz.newByteArray (available, 0);\r
182 $in.read (b);\r
183 input = input +  String.instantialize (b, 0, b.length);\r
184 } while (!input.endsWith ("\n") && !input.endsWith ("\r\n") && !this.quit);\r
185 return input;\r
186 }, "java.io.PipedInputStream");\r
187 });\r