2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
21 // A simple Java Console for your application (Swing version)
22 // Requires Java 1.1.5 or higher
24 // Disclaimer the use of this source is at your own risk.
26 // Permision to use and distribute into your own applications
28 // RJHM van den Bergh , rvdb@comweb.nl
32 import java.awt.event.*;
34 public class AWTConsole extends WindowAdapter implements WindowListener,
35 ActionListener, Runnable
39 private TextArea textArea;
41 private Thread reader;
43 private Thread reader2;
47 private final PipedInputStream pin = new PipedInputStream();
49 private final PipedInputStream pin2 = new PipedInputStream();
51 Thread errorThrower; // just for testing (Throws an Exception at this Console
55 // create all components and add them
56 frame = new Frame("Java Console");
57 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
58 Dimension frameSize = new Dimension((int) (screenSize.width / 2),
59 (int) (screenSize.height / 2));
60 int x = (int) (frameSize.width / 2);
61 int y = (int) (frameSize.height / 2);
62 frame.setBounds(x, y, frameSize.width, frameSize.height);
64 textArea = new TextArea();
65 textArea.setEditable(false);
66 Button button = new Button("clear");
68 Panel panel = new Panel();
69 panel.setLayout(new BorderLayout());
70 panel.add(textArea, BorderLayout.CENTER);
71 panel.add(button, BorderLayout.SOUTH);
74 frame.setVisible(true);
76 frame.addWindowListener(this);
77 button.addActionListener(this);
81 PipedOutputStream pout = new PipedOutputStream(this.pin);
82 System.setOut(new PrintStream(pout, true));
83 } catch (java.io.IOException io)
85 textArea.append("Couldn't redirect STDOUT to this console\n"
87 } catch (SecurityException se)
89 textArea.append("Couldn't redirect STDOUT to this console\n"
95 PipedOutputStream pout2 = new PipedOutputStream(this.pin2);
96 System.setErr(new PrintStream(pout2, true));
97 } catch (java.io.IOException io)
99 textArea.append("Couldn't redirect STDERR to this console\n"
101 } catch (SecurityException se)
103 textArea.append("Couldn't redirect STDERR to this console\n"
107 quit = false; // signals the Threads that they should exit
109 // Starting two seperate threads to read from the PipedInputStreams
111 reader = new Thread(this);
112 reader.setDaemon(true);
115 reader2 = new Thread(this);
116 reader2.setDaemon(true);
120 // you may omit this part for your application
122 System.out.println("Hello World 2");
123 System.out.println("All fonts available to Graphic2D:\n");
124 GraphicsEnvironment ge = GraphicsEnvironment
125 .getLocalGraphicsEnvironment();
126 String[] fontNames = ge.getAvailableFontFamilyNames();
127 for (int n = 0; n < fontNames.length; n++)
128 System.out.println(fontNames[n]);
129 // Testing part: simple an error thrown anywhere in this JVM will be printed
131 // We do it with a seperate Thread becasue we don't wan't to break a Thread
132 // used by the Console.
133 System.out.println("\nLets throw an error on this console");
134 errorThrower = new Thread(this);
135 errorThrower.setDaemon(true);
136 errorThrower.start();
139 public synchronized void windowClosed(WindowEvent evt)
142 this.notifyAll(); // stop all threads
147 } catch (Exception e)
154 } catch (Exception e)
160 public synchronized void windowClosing(WindowEvent evt)
162 frame.setVisible(false); // default behaviour of JFrame
166 public synchronized void actionPerformed(ActionEvent evt)
168 textArea.setText("");
171 public synchronized void run()
175 while (Thread.currentThread() == reader)
180 } catch (InterruptedException ie)
183 if (pin.available() != 0)
185 String input = this.readLine(pin);
186 textArea.append(input);
192 while (Thread.currentThread() == reader2)
197 } catch (InterruptedException ie)
200 if (pin2.available() != 0)
202 String input = this.readLine(pin2);
203 textArea.append(input);
208 } catch (Exception e)
210 textArea.append("\nConsole reports an Internal error.");
211 textArea.append("The error is: " + e);
214 // just for testing (Throw a Nullpointer after 1 second)
215 if (Thread.currentThread() == errorThrower)
220 } catch (InterruptedException ie)
223 throw new NullPointerException(
224 "Application test: throwing an NullPointerException It should arrive at the console");
229 public synchronized String readLine(PipedInputStream in)
235 int available = in.available();
238 byte b[] = new byte[available];
240 input = input + new String(b, 0, b.length);
241 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
245 public static void main(String[] arg)
247 new AWTConsole(); // create console with not reference