JAL-1432 updated copyright notices
[jalview.git] / src / jalview / util / AWTConsole.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
10  *  
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.
15  * 
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.
18  */
19 package jalview.util;
20
21 //
22 // A simple Java Console for your application (Swing version)
23 // Requires Java 1.1.5 or higher
24 //
25 // Disclaimer the use of this source is at your own risk. 
26 //
27 // Permision to use and distribute into your own applications
28 //
29 // RJHM van den Bergh , rvdb@comweb.nl
30
31 import java.io.*;
32 import java.awt.*;
33 import java.awt.event.*;
34
35 public class AWTConsole extends WindowAdapter implements WindowListener,
36         ActionListener, Runnable
37 {
38   private Frame frame;
39
40   private TextArea textArea;
41
42   private Thread reader;
43
44   private Thread reader2;
45
46   private boolean quit;
47
48   private final PipedInputStream pin = new PipedInputStream();
49
50   private final PipedInputStream pin2 = new PipedInputStream();
51
52   Thread errorThrower; // just for testing (Throws an Exception at this Console
53
54   public AWTConsole()
55   {
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);
64
65     textArea = new TextArea();
66     textArea.setEditable(false);
67     Button button = new Button("clear");
68
69     Panel panel = new Panel();
70     panel.setLayout(new BorderLayout());
71     panel.add(textArea, BorderLayout.CENTER);
72     panel.add(button, BorderLayout.SOUTH);
73     frame.add(panel);
74
75     frame.setVisible(true);
76
77     frame.addWindowListener(this);
78     button.addActionListener(this);
79
80     try
81     {
82       PipedOutputStream pout = new PipedOutputStream(this.pin);
83       System.setOut(new PrintStream(pout, true));
84     } catch (java.io.IOException io)
85     {
86       textArea.append("Couldn't redirect STDOUT to this console\n"
87               + io.getMessage());
88     } catch (SecurityException se)
89     {
90       textArea.append("Couldn't redirect STDOUT to this console\n"
91               + se.getMessage());
92     }
93
94     try
95     {
96       PipedOutputStream pout2 = new PipedOutputStream(this.pin2);
97       System.setErr(new PrintStream(pout2, true));
98     } catch (java.io.IOException io)
99     {
100       textArea.append("Couldn't redirect STDERR to this console\n"
101               + io.getMessage());
102     } catch (SecurityException se)
103     {
104       textArea.append("Couldn't redirect STDERR to this console\n"
105               + se.getMessage());
106     }
107
108     quit = false; // signals the Threads that they should exit
109
110     // Starting two seperate threads to read from the PipedInputStreams
111     //
112     reader = new Thread(this);
113     reader.setDaemon(true);
114     reader.start();
115     //
116     reader2 = new Thread(this);
117     reader2.setDaemon(true);
118     reader2.start();
119
120     // testing part
121     // you may omit this part for your application
122     //
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
131     // on the Console
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();
138   }
139
140   public synchronized void windowClosed(WindowEvent evt)
141   {
142     quit = true;
143     this.notifyAll(); // stop all threads
144     try
145     {
146       reader.join(1000);
147       pin.close();
148     } catch (Exception e)
149     {
150     }
151     try
152     {
153       reader2.join(1000);
154       pin2.close();
155     } catch (Exception e)
156     {
157     }
158     System.exit(0);
159   }
160
161   public synchronized void windowClosing(WindowEvent evt)
162   {
163     frame.setVisible(false); // default behaviour of JFrame
164     frame.dispose();
165   }
166
167   public synchronized void actionPerformed(ActionEvent evt)
168   {
169     textArea.setText("");
170   }
171
172   public synchronized void run()
173   {
174     try
175     {
176       while (Thread.currentThread() == reader)
177       {
178         try
179         {
180           this.wait(100);
181         } catch (InterruptedException ie)
182         {
183         }
184         if (pin.available() != 0)
185         {
186           String input = this.readLine(pin);
187           textArea.append(input);
188         }
189         if (quit)
190           return;
191       }
192
193       while (Thread.currentThread() == reader2)
194       {
195         try
196         {
197           this.wait(100);
198         } catch (InterruptedException ie)
199         {
200         }
201         if (pin2.available() != 0)
202         {
203           String input = this.readLine(pin2);
204           textArea.append(input);
205         }
206         if (quit)
207           return;
208       }
209     } catch (Exception e)
210     {
211       textArea.append("\nConsole reports an Internal error.");
212       textArea.append("The error is: " + e);
213     }
214
215     // just for testing (Throw a Nullpointer after 1 second)
216     if (Thread.currentThread() == errorThrower)
217     {
218       try
219       {
220         this.wait(1000);
221       } catch (InterruptedException ie)
222       {
223       }
224       throw new NullPointerException(
225               "Application test: throwing an NullPointerException It should arrive at the console");
226     }
227
228   }
229
230   public synchronized String readLine(PipedInputStream in)
231           throws IOException
232   {
233     String input = "";
234     do
235     {
236       int available = in.available();
237       if (available == 0)
238         break;
239       byte b[] = new byte[available];
240       in.read(b);
241       input = input + new String(b, 0, b.length);
242     } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
243     return input;
244   }
245
246   public static void main(String[] arg)
247   {
248     new AWTConsole(); // create console with not reference
249   }
250 }