2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
22 // A simple Java Console for your application (Swing version)
23 // Requires Java 1.1.5 or higher
25 // Disclaimer the use of this source is at your own risk.
27 // Permision to use and distribute into your own applications
29 // RJHM van den Bergh , rvdb@comweb.nl
33 import java.awt.event.*;
35 public class AWTConsole extends WindowAdapter implements WindowListener,
36 ActionListener, Runnable
40 private TextArea textArea;
42 private Thread reader;
44 private Thread reader2;
48 private final PipedInputStream pin = new PipedInputStream();
50 private final PipedInputStream pin2 = new PipedInputStream();
52 Thread errorThrower; // just for testing (Throws an Exception at this Console
56 // create all components and add them
57 frame = new Frame("Java Console");
58 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
59 Dimension frameSize = new Dimension((int) (screenSize.width / 2),
60 (int) (screenSize.height / 2));
61 int x = (int) (frameSize.width / 2);
62 int y = (int) (frameSize.height / 2);
63 frame.setBounds(x, y, frameSize.width, frameSize.height);
65 textArea = new TextArea();
66 textArea.setEditable(false);
67 Button button = new Button("clear");
69 Panel panel = new Panel();
70 panel.setLayout(new BorderLayout());
71 panel.add(textArea, BorderLayout.CENTER);
72 panel.add(button, BorderLayout.SOUTH);
75 frame.setVisible(true);
77 frame.addWindowListener(this);
78 button.addActionListener(this);
82 PipedOutputStream pout = new PipedOutputStream(this.pin);
83 System.setOut(new PrintStream(pout, true));
84 } catch (java.io.IOException io)
86 textArea.append("Couldn't redirect STDOUT to this console\n"
88 } catch (SecurityException se)
90 textArea.append("Couldn't redirect STDOUT to this console\n"
96 PipedOutputStream pout2 = new PipedOutputStream(this.pin2);
97 System.setErr(new PrintStream(pout2, true));
98 } catch (java.io.IOException io)
100 textArea.append("Couldn't redirect STDERR to this console\n"
102 } catch (SecurityException se)
104 textArea.append("Couldn't redirect STDERR to this console\n"
108 quit = false; // signals the Threads that they should exit
110 // Starting two seperate threads to read from the PipedInputStreams
112 reader = new Thread(this);
113 reader.setDaemon(true);
116 reader2 = new Thread(this);
117 reader2.setDaemon(true);
121 // you may omit this part for your application
123 System.out.println("Hello World 2");
124 System.out.println("All fonts available to Graphic2D:\n");
125 GraphicsEnvironment ge = GraphicsEnvironment
126 .getLocalGraphicsEnvironment();
127 String[] fontNames = ge.getAvailableFontFamilyNames();
128 for (int n = 0; n < fontNames.length; n++)
129 System.out.println(fontNames[n]);
130 // Testing part: simple an error thrown anywhere in this JVM will be printed
132 // We do it with a seperate Thread becasue we don't wan't to break a Thread
133 // used by the Console.
134 System.out.println("\nLets throw an error on this console");
135 errorThrower = new Thread(this);
136 errorThrower.setDaemon(true);
137 errorThrower.start();
140 public synchronized void windowClosed(WindowEvent evt)
143 this.notifyAll(); // stop all threads
148 } catch (Exception e)
155 } catch (Exception e)
161 public synchronized void windowClosing(WindowEvent evt)
163 frame.setVisible(false); // default behaviour of JFrame
167 public synchronized void actionPerformed(ActionEvent evt)
169 textArea.setText("");
172 public synchronized void run()
176 while (Thread.currentThread() == reader)
181 } catch (InterruptedException ie)
184 if (pin.available() != 0)
186 String input = this.readLine(pin);
187 textArea.append(input);
193 while (Thread.currentThread() == reader2)
198 } catch (InterruptedException ie)
201 if (pin2.available() != 0)
203 String input = this.readLine(pin2);
204 textArea.append(input);
209 } catch (Exception e)
211 textArea.append("\nConsole reports an Internal error.");
212 textArea.append("The error is: " + e);
215 // just for testing (Throw a Nullpointer after 1 second)
216 if (Thread.currentThread() == errorThrower)
221 } catch (InterruptedException ie)
224 throw new NullPointerException(
225 "Application test: throwing an NullPointerException It should arrive at the console");
230 public synchronized String readLine(PipedInputStream in)
236 int available = in.available();
239 byte b[] = new byte[available];
241 input = input + new String(b, 0, b.length);
242 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
246 public static void main(String[] arg)
248 new AWTConsole(); // create console with not reference