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
30 * after desktop is created. Acquired with thanks from RJHM's site
31 * http://www.comweb.nl/java/Console/Console.html A simple Java Console for your
32 * application (Swing version) Requires Java 1.1.5 or higher Disclaimer the use
33 * of this source is at your own risk. Permision to use and distribute into your
34 * own applications RJHM van den Bergh , rvdb@comweb.nl
37 public class Console extends WindowAdapter implements WindowListener,
38 ActionListener, Runnable
42 private JTextArea textArea;
48 private Thread reader;
50 private Thread reader2;
54 private final PrintStream stdout = System.out;
56 private final PrintStream stderr = System.err;
58 private final PipedInputStream pin = new PipedInputStream();
60 private final PipedInputStream pin2 = new PipedInputStream();
62 Thread errorThrower; // just for testing (Throws an Exception at this Console
64 // are we attached to some parent Desktop
65 Desktop parent = null;
69 // create all components and add them
70 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
71 frame = initFrame("Java Console", screenSize.width / 2,
72 screenSize.height / 2, -1, -1);
76 private void initConsole(boolean visible)
78 // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
79 // textArea = cpt.getTextArea();
80 textArea = new JTextArea();
81 textArea.setEditable(false);
82 JButton button = new JButton("clear");
85 frame.getContentPane().setLayout(new BorderLayout());
86 frame.getContentPane().add(new JScrollPane(textArea),
88 frame.getContentPane().add(button, BorderLayout.SOUTH);
89 frame.setVisible(visible);
91 frame.addWindowListener(this);
92 button.addActionListener(this);
96 PipedOutputStream pout = new PipedOutputStream(this.pin);
97 System.setOut(new PrintStream(pout, true));
98 } catch (java.io.IOException io)
100 textArea.append("Couldn't redirect STDOUT to this console\n"
102 } catch (SecurityException se)
104 textArea.append("Couldn't redirect STDOUT to this console\n"
110 PipedOutputStream pout2 = new PipedOutputStream(this.pin2);
111 System.setErr(new PrintStream(pout2, true));
112 } catch (java.io.IOException io)
114 textArea.append("Couldn't redirect STDERR to this console\n"
116 } catch (SecurityException se)
118 textArea.append("Couldn't redirect STDERR to this console\n"
122 quit = false; // signals the Threads that they should exit
124 // Starting two seperate threads to read from the PipedInputStreams
126 reader = new Thread(this);
127 reader.setDaemon(true);
130 reader2 = new Thread(this);
131 reader2.setDaemon(true);
138 // you may omit this part for your application
141 System.out.println("Hello World 2");
142 System.out.println("All fonts available to Graphic2D:\n");
143 GraphicsEnvironment ge = GraphicsEnvironment
144 .getLocalGraphicsEnvironment();
145 String[] fontNames = ge.getAvailableFontFamilyNames();
146 for (int n = 0; n < fontNames.length; n++)
147 System.out.println(fontNames[n]);
148 // Testing part: simple an error thrown anywhere in this JVM will be printed
150 // We do it with a seperate Thread becasue we don't wan't to break a Thread
151 // used by the Console.
152 System.out.println("\nLets throw an error on this console");
153 errorThrower = new Thread(this);
154 errorThrower.setDaemon(true);
155 errorThrower.start();
158 private JFrame initFrame(String string, int i, int j, int x, int y)
160 JFrame frame = new JFrame(string);
161 frame.setName(string);
166 frame.setBounds(x, y, i, j);
171 * attach a console to the desktop - the desktop will open it if requested.
175 public Console(Desktop desktop)
178 // window name - get x,y,width, height possibly scaled
179 Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
182 frame = initFrame("Jalview Java Console", desktop.getWidth() / 2,
183 desktop.getHeight() / 4, desktop.getX(), desktop.getY());
187 frame = initFrame("Jalview Java Console", bounds.width,
188 bounds.height, bounds.x, bounds.y);
190 // desktop.add(frame);
192 JalviewAppender jappender = new JalviewAppender();
193 jappender.setLayout(new SimpleLayout());
194 JalviewAppender.setTextArea(textArea);
195 org.apache.log4j.Logger.getRootLogger().addAppender(jappender);
198 public synchronized void stopConsole()
201 this.notifyAll(); // stop all threads
206 } catch (Exception e)
213 } catch (Exception e)
219 public synchronized void windowClosed(WindowEvent evt)
221 frame.setVisible(false);
229 parent.showConsole(false);
233 public synchronized void windowClosing(WindowEvent evt)
235 frame.setVisible(false); // default behaviour of JFrame
239 public synchronized void actionPerformed(ActionEvent evt)
242 // textArea.setText("");
245 public synchronized void run()
249 while (Thread.currentThread() == reader)
254 } catch (InterruptedException ie)
257 while (pin.available() != 0)
259 String input = this.readLine(pin);
261 textArea.append(input);
269 while (Thread.currentThread() == reader2)
274 } catch (InterruptedException ie)
277 while (pin2.available() != 0)
279 String input = this.readLine(pin2);
281 textArea.append(input);
288 } catch (Exception e)
290 textArea.append("\nConsole reports an Internal error.");
291 textArea.append("The error is: " + e.getMessage());
293 stderr.println("Console reports an Internal error.\nThe error is: "
297 // just for testing (Throw a Nullpointer after 1 second)
298 if (Thread.currentThread() == errorThrower)
303 } catch (InterruptedException ie)
306 throw new NullPointerException(
307 "Application test: throwing an NullPointerException It should arrive at the console");
311 private synchronized void trimBuffer(boolean clear)
314 if (clear || lines > lim)
320 // minimum length for truncation/clearing
321 String header = textArea.getText(0, textArea.getLineEndOffset(5))
322 + "\n..Truncated..\n"; // keep first 5 lines for startup info
326 truncate = textArea.getLineEndOffset(lines - lim - 7);
327 textArea.setText(header
328 + textArea.getText(truncate, textArea.getText()
334 textArea.setText(header);
338 } catch (Exception e)
342 lines = textArea.getLineCount();
347 public synchronized String readLine(PipedInputStream in)
354 int available = in.available();
357 byte b[] = new byte[available];
359 input = input + new String(b, 0, b.length);
360 while ((lp = input.indexOf("\n", lp + 1)) > -1)
364 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
368 public static void main(String[] arg)
370 new Console().test(); // create console with not reference
374 public void setVisible(boolean selected)
376 frame.setVisible(selected);
377 if (selected == true)
383 public Rectangle getBounds()
387 return frame.getBounds();