2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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/>.
22 import java.awt.event.*;
25 import org.apache.log4j.SimpleLayout;
28 * Simple Jalview Java Console. Version 1 - allows viewing of console output
29 * after desktop is created. Acquired with thanks from RJHM's site
30 * http://www.comweb.nl/java/Console/Console.html A simple Java Console for your
31 * application (Swing version) Requires Java 1.1.5 or higher Disclaimer the use
32 * of this source is at your own risk. Permision to use and distribute into your
33 * own applications RJHM van den Bergh , rvdb@comweb.nl
36 public class Console extends WindowAdapter implements WindowListener,
37 ActionListener, Runnable
41 private JTextArea textArea;
42 /* unused - tally and limit for lines in console window
47 int byteslim = 102400, bytescut = 76800; // 100k and 75k cut point.
49 private Thread reader, reader2, textAppender;
53 private final PrintStream stdout = System.out, stderr = System.err;
55 private PipedInputStream pin = new PipedInputStream();
57 private PipedInputStream pin2 = new PipedInputStream();
59 private StringBuffer displayPipe = new StringBuffer();
61 Thread errorThrower; // just for testing (Throws an Exception at this Console
63 // are we attached to some parent Desktop
64 Desktop parent = null;
68 // create all components and add them
69 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
70 frame = initFrame("Java Console", screenSize.width / 2,
71 screenSize.height / 2, -1, -1);
75 private void initConsole(boolean visible)
77 initConsole(visible, true);
82 * @param visible - open the window
83 * @param redirect - redirect std*
85 private void initConsole(boolean visible, boolean redirect)
87 // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
88 // textArea = cpt.getTextArea();
89 textArea = new JTextArea();
90 textArea.setEditable(false);
91 JButton button = new JButton("clear");
94 frame.getContentPane().setLayout(new BorderLayout());
95 frame.getContentPane().add(new JScrollPane(textArea),
97 frame.getContentPane().add(button, BorderLayout.SOUTH);
98 frame.setVisible(visible);
99 updateConsole = visible;
100 frame.addWindowListener(this);
101 button.addActionListener(this);
108 quit = false; // signals the Threads that they should exit
110 // Starting two seperate threads to read from the PipedInputStreams
112 reader = new Thread(this);
113 reader.setDaemon(true);
116 reader2 = new Thread(this);
117 reader2.setDaemon(true);
119 // and a thread to append text to the textarea
120 textAppender = new Thread(this);
121 textAppender.setDaemon(true);
122 textAppender.start();
124 PipedOutputStream pout=null,perr=null;
125 public void redirectStreams() {
130 pout = new PipedOutputStream(this.pin);
131 System.setOut(new PrintStream(pout, true));
132 } catch (java.io.IOException io)
134 textArea.append("Couldn't redirect STDOUT to this console\n"
136 io.printStackTrace(stderr);
137 } catch (SecurityException se)
139 textArea.append("Couldn't redirect STDOUT to this console\n"
141 se.printStackTrace(stderr);
146 perr = new PipedOutputStream(this.pin2);
147 System.setErr(new PrintStream(perr, true));
148 } catch (java.io.IOException io)
150 textArea.append("Couldn't redirect STDERR to this console\n"
152 io.printStackTrace(stderr);
153 } catch (SecurityException se)
155 textArea.append("Couldn't redirect STDERR to this console\n"
157 se.printStackTrace(stderr);
161 public void unredirectStreams() {
166 System.setOut(stdout);
169 pin = new PipedInputStream();
171 } catch (java.io.IOException io)
173 textArea.append("Couldn't unredirect STDOUT to this console\n"
175 io.printStackTrace(stderr);
176 } catch (SecurityException se)
178 textArea.append("Couldn't unredirect STDOUT to this console\n"
180 se.printStackTrace(stderr);
185 System.setErr(stderr);
188 pin2 = new PipedInputStream();
190 } catch (java.io.IOException io)
192 textArea.append("Couldn't unredirect STDERR to this console\n"
194 io.printStackTrace(stderr);
195 } catch (SecurityException se)
197 textArea.append("Couldn't unredirect STDERR to this console\n"
199 se.printStackTrace(stderr);
207 // you may omit this part for your application
210 System.out.println("Hello World 2");
211 System.out.println("All fonts available to Graphic2D:\n");
212 GraphicsEnvironment ge = GraphicsEnvironment
213 .getLocalGraphicsEnvironment();
214 String[] fontNames = ge.getAvailableFontFamilyNames();
215 for (int n = 0; n < fontNames.length; n++)
216 System.out.println(fontNames[n]);
217 // Testing part: simple an error thrown anywhere in this JVM will be printed
219 // We do it with a seperate Thread becasue we don't wan't to break a Thread
220 // used by the Console.
221 System.out.println("\nLets throw an error on this console");
222 errorThrower = new Thread(this);
223 errorThrower.setDaemon(true);
224 errorThrower.start();
227 private JFrame initFrame(String string, int i, int j, int x, int y)
229 JFrame frame = new JFrame(string);
230 frame.setName(string);
235 frame.setBounds(x, y, i, j);
240 * attach a console to the desktop - the desktop will open it if requested.
244 public Console(Desktop desktop)
249 * attach a console to the desktop - the desktop will open it if requested.
252 * @param showjconsole - if true, then redirect stdout immediately
254 public Console(Desktop desktop, boolean showjconsole)
257 // window name - get x,y,width, height possibly scaled
258 Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
261 frame = initFrame("Jalview Java Console", desktop.getWidth() / 2,
262 desktop.getHeight() / 4, desktop.getX(), desktop.getY());
266 frame = initFrame("Jalview Java Console", bounds.width,
267 bounds.height, bounds.x, bounds.y);
269 // desktop.add(frame);
271 JalviewAppender jappender = new JalviewAppender();
272 jappender.setLayout(new SimpleLayout());
273 JalviewAppender.setTextArea(textArea);
274 org.apache.log4j.Logger.getRootLogger().addAppender(jappender);
278 public synchronized void stopConsole()
284 if (errorThrower!=null)
285 errorThrower.notify(); // stop all threads
286 if (textAppender!=null)
287 textAppender.notify();
295 } catch (Exception e)
302 } catch (Exception e)
307 textAppender.join(10);
308 } catch (Exception e)
312 if (!frame.isVisible())
319 public synchronized void windowClosed(WindowEvent evt)
321 frame.setVisible(false);
324 private void closeConsoleGui() {
325 updateConsole = false;
333 parent.showConsole(false);
337 public synchronized void windowClosing(WindowEvent evt)
339 frame.setVisible(false); // default behaviour of JFrame
345 public synchronized void actionPerformed(ActionEvent evt)
348 // textArea.setText("");
351 public synchronized void run()
355 while (Thread.currentThread() == reader)
357 if (pin==null || pin.available() == 0)
362 if (pin.available() == 0)
366 } catch (InterruptedException ie)
371 while (pin.available() != 0)
373 String input = this.readLine(pin);
375 long time = System.nanoTime();
376 appendToTextArea(input);
377 //stderr.println("Time taken to stdout append:\t"
378 // + (System.nanoTime() - time) + " ns");
385 while (Thread.currentThread() == reader2)
387 if (pin2.available() == 0)
392 if (pin2.available() == 0)
396 } catch (InterruptedException ie)
400 while (pin2.available() != 0)
402 String input = this.readLine(pin2);
404 long time = System.nanoTime();
405 appendToTextArea(input);
406 //stderr.println("Time taken to stderr append:\t"
407 // + (System.nanoTime() - time) + " ns");
413 while (Thread.currentThread() == textAppender)
417 // check string buffer - if greater than console, clear console and
418 // replace with last segment of content, otherwise, append all to
421 while (displayPipe.length() > 0)
424 StringBuffer tmp = new StringBuffer(), replace;
425 synchronized (displayPipe)
427 replace = displayPipe;
430 // simply append whole buffer
431 textArea.append(replace.toString());
432 count += replace.length();
433 if (count > byteslim)
438 if (displayPipe.length() == 0)
443 if (displayPipe.length() == 0)
447 } catch (InterruptedException e)
458 } catch (InterruptedException e)
469 } catch (Exception e)
471 textArea.append("\nConsole reports an Internal error.");
472 textArea.append("The error is: " + e.getMessage());
473 // Need to uncomment this to ensure that line tally is synched.
475 stderr.println("Console reports an Internal error.\nThe error is: "
479 // just for testing (Throw a Nullpointer after 1 second)
480 if (Thread.currentThread() == errorThrower)
485 } catch (InterruptedException ie)
488 throw new NullPointerException(
489 "Application test: throwing an NullPointerException It should arrive at the console");
493 private void appendToTextArea(final String input)
495 if (updateConsole == false)
500 long time = System.nanoTime();
501 javax.swing.SwingUtilities.invokeLater(new Runnable()
505 displayPipe.append(input); // change to stringBuffer
506 // displayPipe.flush();
510 // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() -
515 private String header = null;
517 private boolean updateConsole = false;
519 private synchronized void trimBuffer(boolean clear)
521 if (header == null && textArea.getLineCount() > 5)
525 header = textArea.getText(0, textArea.getLineStartOffset(5))
526 + "\nTruncated...\n";
527 } catch (Exception e)
533 int tlength = textArea.getDocument().getLength();
536 if (clear || (tlength > byteslim))
542 long time = System.nanoTime();
543 textArea.replaceRange(header, 0, tlength - bytescut);
544 //stderr.println("Time taken to cut:\t"
545 // + (System.nanoTime() - time) + " ns");
549 textArea.setText(header);
551 } catch (Exception e)
555 // lines = textArea.getLineCount();
561 public synchronized String readLine(PipedInputStream in)
568 int available = in.available();
571 byte b[] = new byte[available];
573 input = input + new String(b, 0, b.length);
574 // counts lines - we don't do this for speed.
575 // while ((lp = input.indexOf("\n", lp + 1)) > -1)
579 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
583 public static void main(String[] arg)
585 new Console().test(); // create console with not reference
589 public void setVisible(boolean selected)
591 frame.setVisible(selected);
592 if (selected == true)
595 updateConsole = true;
601 updateConsole = false;
605 public Rectangle getBounds()
609 return frame.getBounds();
615 * set the banner that appears at the top of the console output
618 public void setHeader(String string)
621 if (header.charAt(header.length()-1)!='\n') {
624 textArea.insert(header, 0);
630 public String getHeader()