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