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;
50 private final PrintStream stdout = System.out;
51 private final PrintStream stderr = System.err;
52 private final PipedInputStream pin = new PipedInputStream();
54 private final PipedInputStream pin2 = new PipedInputStream();
57 Thread errorThrower; // just for testing (Throws an Exception at this Console
58 // are we attached to some parent Desktop
59 Desktop parent = null;
62 // create all components and add them
63 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
64 frame = initFrame("Java Console", screenSize.width / 2,
65 screenSize.height / 2, -1, -1);
69 private void initConsole(boolean visible)
71 // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
72 //textArea = cpt.getTextArea();
73 textArea = new JTextArea();
74 textArea.setEditable(false);
75 JButton button = new JButton("clear");
78 frame.getContentPane().setLayout(new BorderLayout());
79 frame.getContentPane().add(new JScrollPane(textArea),
81 frame.getContentPane().add(button, BorderLayout.SOUTH);
82 frame.setVisible(visible);
84 frame.addWindowListener(this);
85 button.addActionListener(this);
89 PipedOutputStream pout = new PipedOutputStream(this.pin);
90 System.setOut(new PrintStream(pout, true));
91 } catch (java.io.IOException io)
93 textArea.append("Couldn't redirect STDOUT to this console\n"
95 } catch (SecurityException se)
97 textArea.append("Couldn't redirect STDOUT to this console\n"
103 PipedOutputStream pout2 = new PipedOutputStream(this.pin2);
104 System.setErr(new PrintStream(pout2, true));
105 } catch (java.io.IOException io)
107 textArea.append("Couldn't redirect STDERR to this console\n"
109 } catch (SecurityException se)
111 textArea.append("Couldn't redirect STDERR to this console\n"
115 quit = false; // signals the Threads that they should exit
117 // Starting two seperate threads to read from the PipedInputStreams
119 reader = new Thread(this);
120 reader.setDaemon(true);
123 reader2 = new Thread(this);
124 reader2.setDaemon(true);
129 // you may omit this part for your application
132 System.out.println("Hello World 2");
133 System.out.println("All fonts available to Graphic2D:\n");
134 GraphicsEnvironment ge = GraphicsEnvironment
135 .getLocalGraphicsEnvironment();
136 String[] fontNames = ge.getAvailableFontFamilyNames();
137 for (int n = 0; n < fontNames.length; n++)
138 System.out.println(fontNames[n]);
139 // Testing part: simple an error thrown anywhere in this JVM will be printed
141 // We do it with a seperate Thread becasue we don't wan't to break a Thread
142 // used by the Console.
143 System.out.println("\nLets throw an error on this console");
144 errorThrower = new Thread(this);
145 errorThrower.setDaemon(true);
146 errorThrower.start();
149 private JFrame initFrame(String string, int i, int j, int x, int y)
151 JFrame frame = new JFrame(string);
152 frame.setName(string);
157 frame.setBounds(x, y, i, j);
162 * attach a console to the desktop - the desktop will open it if requested.
166 public Console(Desktop desktop)
169 // window name - get x,y,width, height possibly scaled
170 Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
173 frame = initFrame("Jalview Java Console", desktop.getWidth() / 2,
174 desktop.getHeight() / 4,desktop.getX(),desktop.getY());
176 frame = initFrame("Jalview Java Console", bounds.width, bounds.height, bounds.x, bounds.y);
178 // desktop.add(frame);
180 JalviewAppender jappender = new JalviewAppender();
181 jappender.setLayout(new SimpleLayout());
182 JalviewAppender.setTextArea(textArea);
183 org.apache.log4j.Logger.getRootLogger().addAppender(jappender);
186 public synchronized void stopConsole()
189 this.notifyAll(); // stop all threads
194 } catch (Exception e)
201 } catch (Exception e)
207 public synchronized void windowClosed(WindowEvent evt)
209 frame.setVisible(false);
215 parent.showConsole(false);
219 public synchronized void windowClosing(WindowEvent evt)
221 frame.setVisible(false); // default behaviour of JFrame
225 public synchronized void actionPerformed(ActionEvent evt)
227 textArea.setText("");
229 int lines=0,lim=1000;
230 public synchronized void run()
234 while (Thread.currentThread() == reader)
239 } catch (InterruptedException ie)
242 if (pin.available() != 0)
244 String input = this.readLine(pin);
246 textArea.append(input);
253 while (Thread.currentThread() == reader2)
258 } catch (InterruptedException ie)
261 if (pin2.available() != 0)
263 String input = this.readLine(pin2);
265 textArea.append(input);
271 } catch (Exception e)
273 textArea.append("\nConsole reports an Internal error.");
274 textArea.append("The error is: " + e);
278 // just for testing (Throw a Nullpointer after 1 second)
279 if (Thread.currentThread() == errorThrower)
284 } catch (InterruptedException ie)
287 throw new NullPointerException(
288 "Application test: throwing an NullPointerException It should arrive at the console");
294 String header = textArea.getText(0,textArea.getLineEndOffset(5))+"\n..Truncated..\n"; // keep first 5 lines for startup info
295 int truncate = textArea.getLineEndOffset(lim-7-lines);
296 textArea.setText(header+textArea.getText(truncate,textArea.getText().length()-truncate)); } catch (Exception e)
300 lines = textArea.getLineCount();
305 public synchronized String readLine(PipedInputStream in)
311 int available = in.available();
314 byte b[] = new byte[available];
316 input = input + new String(b, 0, b.length);
317 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
321 public static void main(String[] arg)
323 new Console().test(); // create console with not reference
327 public void setVisible(boolean selected)
329 frame.setVisible(selected);
330 if (selected == true)
336 public Rectangle getBounds()
340 return frame.getBounds();