2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 import java.awt.event.*;
26 import org.apache.log4j.SimpleLayout;
29 * Simple Jalview Java Console. Version 1 - allows viewing of console output after desktop is created.
30 * Acquired with thanks from RJHM's site http://www.comweb.nl/java/Console/Console.html
31 * A simple Java Console for your application (Swing version)
32 * Requires Java 1.1.5 or higher
33 * Disclaimer the use of this source is at your own risk.
34 * Permision to use and distribute into your own applications
35 * RJHM van den Bergh , rvdb@comweb.nl
38 public class Console extends WindowAdapter implements WindowListener,
39 ActionListener, Runnable
43 private JTextArea textArea;
45 private Thread reader;
47 private Thread reader2;
51 private final PipedInputStream pin = new PipedInputStream();
53 private final PipedInputStream pin2 = new PipedInputStream();
56 Thread errorThrower; // just for testing (Throws an Exception at this Console
57 // are we attached to some parent Desktop
58 Desktop parent = null;
61 // create all components and add them
62 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
63 frame = initFrame("Java Console", screenSize.width / 2,
64 screenSize.height / 2, -1, -1);
68 private void initConsole(boolean visible)
70 // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
71 //textArea = cpt.getTextArea();
72 textArea = new JTextArea();
73 textArea.setEditable(false);
74 JButton button = new JButton("clear");
77 frame.getContentPane().setLayout(new BorderLayout());
78 frame.getContentPane().add(new JScrollPane(textArea),
80 frame.getContentPane().add(button, BorderLayout.SOUTH);
81 frame.setVisible(visible);
83 frame.addWindowListener(this);
84 button.addActionListener(this);
88 PipedOutputStream pout = new PipedOutputStream(this.pin);
89 System.setOut(new PrintStream(pout, true));
90 } catch (java.io.IOException io)
92 textArea.append("Couldn't redirect STDOUT to this console\n"
94 } catch (SecurityException se)
96 textArea.append("Couldn't redirect STDOUT to this console\n"
102 PipedOutputStream pout2 = new PipedOutputStream(this.pin2);
103 System.setErr(new PrintStream(pout2, true));
104 } catch (java.io.IOException io)
106 textArea.append("Couldn't redirect STDERR to this console\n"
108 } catch (SecurityException se)
110 textArea.append("Couldn't redirect STDERR to this console\n"
114 quit = false; // signals the Threads that they should exit
116 // Starting two seperate threads to read from the PipedInputStreams
118 reader = new Thread(this);
119 reader.setDaemon(true);
122 reader2 = new Thread(this);
123 reader2.setDaemon(true);
128 // you may omit this part for your application
131 System.out.println("Hello World 2");
132 System.out.println("All fonts available to Graphic2D:\n");
133 GraphicsEnvironment ge = GraphicsEnvironment
134 .getLocalGraphicsEnvironment();
135 String[] fontNames = ge.getAvailableFontFamilyNames();
136 for (int n = 0; n < fontNames.length; n++)
137 System.out.println(fontNames[n]);
138 // Testing part: simple an error thrown anywhere in this JVM will be printed
140 // We do it with a seperate Thread becasue we don't wan't to break a Thread
141 // used by the Console.
142 System.out.println("\nLets throw an error on this console");
143 errorThrower = new Thread(this);
144 errorThrower.setDaemon(true);
145 errorThrower.start();
148 private JFrame initFrame(String string, int i, int j, int x, int y)
150 JFrame frame = new JFrame(string);
151 frame.setName(string);
156 frame.setBounds(x, y, i, j);
161 * attach a console to the desktop - the desktop will open it if requested.
165 public Console(Desktop desktop)
168 // window name - get x,y,width, height possibly scaled
169 Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
172 frame = initFrame("Jalview Java Console", desktop.getWidth() / 2,
173 desktop.getHeight() / 4,desktop.getX(),desktop.getY());
175 frame = initFrame("Jalview Java Console", bounds.width, bounds.height, bounds.x, bounds.y);
177 // desktop.add(frame);
179 JalviewAppender jappender = new JalviewAppender();
180 jappender.setLayout(new SimpleLayout());
181 jappender.setTextArea(textArea);
182 org.apache.log4j.Logger.getRootLogger().addAppender(jappender);
185 public synchronized void stopConsole()
188 this.notifyAll(); // stop all threads
193 } catch (Exception e)
200 } catch (Exception e)
206 public synchronized void windowClosed(WindowEvent evt)
208 frame.setVisible(false);
214 parent.showConsole(false);
218 public synchronized void windowClosing(WindowEvent evt)
220 frame.setVisible(false); // default behaviour of JFrame
224 public synchronized void actionPerformed(ActionEvent evt)
226 textArea.setText("");
229 public synchronized void run()
233 while (Thread.currentThread() == reader)
238 } catch (InterruptedException ie)
241 if (pin.available() != 0)
243 String input = this.readLine(pin);
244 textArea.append(input);
250 while (Thread.currentThread() == reader2)
255 } catch (InterruptedException ie)
258 if (pin2.available() != 0)
260 String input = this.readLine(pin2);
261 textArea.append(input);
266 } catch (Exception e)
268 textArea.append("\nConsole reports an Internal error.");
269 textArea.append("The error is: " + e);
272 // just for testing (Throw a Nullpointer after 1 second)
273 if (Thread.currentThread() == errorThrower)
278 } catch (InterruptedException ie)
281 throw new NullPointerException(
282 "Application test: throwing an NullPointerException It should arrive at the console");
287 public synchronized String readLine(PipedInputStream in)
293 int available = in.available();
296 byte b[] = new byte[available];
298 input = input + new String(b, 0, b.length);
299 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
303 public static void main(String[] arg)
305 new Console().test(); // create console with not reference
309 public void setVisible(boolean selected)
311 frame.setVisible(selected);
312 if (selected == true)
318 public Rectangle getBounds()
322 return frame.getBounds();