2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 // A simple Java Console for your application (Swing version)
25 // Requires Java 1.1.5 or higher
27 // Disclaimer the use of this source is at your own risk.
29 // Permision to use and distribute into your own applications
31 // RJHM van den Bergh , rvdb@comweb.nl
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;
51 import jalview.bin.Jalview;
53 public class AWTConsole extends WindowAdapter
54 implements WindowListener, ActionListener, Runnable
58 private TextArea textArea;
60 private Thread reader;
62 private Thread reader2;
66 private final PipedInputStream pin = new PipedInputStream();
68 private final PipedInputStream pin2 = new PipedInputStream();
70 Thread errorThrower; // just for testing (Throws an Exception at this Console
74 // create all components and add them
75 frame = new Frame("Java Console");
76 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
77 Dimension frameSize = new Dimension(screenSize.width / 2,
78 screenSize.height / 2);
79 int x = frameSize.width / 2;
80 int y = frameSize.height / 2;
81 frame.setBounds(x, y, frameSize.width, frameSize.height);
83 textArea = new TextArea();
84 textArea.setEditable(false);
85 Button button = new Button("clear");
87 Panel panel = new Panel();
88 panel.setLayout(new BorderLayout());
89 panel.add(textArea, BorderLayout.CENTER);
90 panel.add(button, BorderLayout.SOUTH);
93 frame.setVisible(true);
95 frame.addWindowListener(this);
96 button.addActionListener(this);
100 PipedOutputStream pout = new PipedOutputStream(this.pin);
101 System.setOut(new PrintStream(pout, true));
102 } catch (java.io.IOException io)
104 textArea.append("Couldn't redirect STDOUT to this console\n"
106 } catch (SecurityException se)
108 textArea.append("Couldn't redirect STDOUT to this console\n"
114 PipedOutputStream pout2 = new PipedOutputStream(this.pin2);
115 System.setErr(new PrintStream(pout2, true));
116 } catch (java.io.IOException io)
118 textArea.append("Couldn't redirect STDERR to this console\n"
120 } catch (SecurityException se)
122 textArea.append("Couldn't redirect STDERR to this console\n"
126 quit = false; // signals the Threads that they should exit
128 // Starting two seperate threads to read from the PipedInputStreams
130 reader = new Thread(this);
131 reader.setDaemon(true);
134 reader2 = new Thread(this);
135 reader2.setDaemon(true);
139 // you may omit this part for your application
141 System.out.println("Hello World 2");
142 System.out.println("All fonts available to Graphic2D:\n");
143 GraphicsEnvironment ge = GraphicsEnvironment
144 .getLocalGraphicsEnvironment();
145 String[] fontNames = ge.getAvailableFontFamilyNames();
146 for (int n = 0; n < fontNames.length; n++)
148 System.out.println(fontNames[n]);
150 // Testing part: simple an error thrown anywhere in this JVM will be printed
152 // We do it with a seperate Thread becasue we don't wan't to break a Thread
153 // used by the Console.
154 System.out.println("\nLets throw an error on this console");
155 errorThrower = new Thread(this);
156 errorThrower.setDaemon(true);
157 errorThrower.start();
161 public synchronized void windowClosed(WindowEvent evt)
164 this.notifyAll(); // stop all threads
169 } catch (Exception e)
176 } catch (Exception e)
179 Jalview.exit("Window closing. Bye!", 0);
183 public synchronized void windowClosing(WindowEvent evt)
185 frame.setVisible(false); // default behaviour of JFrame
190 public synchronized void actionPerformed(ActionEvent evt)
192 textArea.setText("");
196 public synchronized void run()
200 while (Thread.currentThread() == reader)
205 } catch (InterruptedException ie)
208 if (pin.available() != 0)
210 String input = this.readLine(pin);
211 textArea.append(input);
219 while (Thread.currentThread() == reader2)
224 } catch (InterruptedException ie)
227 if (pin2.available() != 0)
229 String input = this.readLine(pin2);
230 textArea.append(input);
237 } catch (Exception e)
239 textArea.append("\nConsole reports an Internal error.");
240 textArea.append("The error is: " + e);
243 // just for testing (Throw a Nullpointer after 1 second)
244 if (Thread.currentThread() == errorThrower)
249 } catch (InterruptedException ie)
252 throw new NullPointerException(
253 MessageManager.getString("exception.application_test_npe"));
258 public synchronized String readLine(PipedInputStream in)
264 int available = in.available();
269 byte b[] = new byte[available];
271 input = input + new String(b, 0, b.length);
272 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
281 public static void main(String[] arg)
283 new AWTConsole(); // create console with not reference