2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.util.MessageManager;
25 import java.awt.BorderLayout;
26 import java.awt.Dimension;
27 import java.awt.GraphicsEnvironment;
28 import java.awt.Rectangle;
29 import java.awt.Toolkit;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.awt.event.WindowAdapter;
33 import java.awt.event.WindowEvent;
34 import java.awt.event.WindowListener;
35 import java.io.IOException;
36 import java.io.PipedInputStream;
37 import java.io.PipedOutputStream;
38 import java.io.PrintStream;
40 import javax.swing.JButton;
41 import javax.swing.JFrame;
42 import javax.swing.JScrollPane;
43 import javax.swing.JTextArea;
45 import org.apache.log4j.SimpleLayout;
48 * Simple Jalview Java Console. Version 1 - allows viewing of console output
49 * after desktop is created. Acquired with thanks from RJHM's site
50 * http://www.comweb.nl/java/Console/Console.html A simple Java Console for your
51 * application (Swing version) Requires Java 1.1.5 or higher Disclaimer the use
52 * of this source is at your own risk. Permision to use and distribute into your
53 * own applications RJHM van den Bergh , rvdb@comweb.nl
56 public class Console extends WindowAdapter
57 implements WindowListener, ActionListener, Runnable
61 private JTextArea textArea;
64 * unused - tally and limit for lines in console window int lines = 0;
68 int byteslim = 102400, bytescut = 76800; // 100k and 75k cut point.
70 private Thread reader, reader2, textAppender;
74 private final PrintStream stdout = System.out, stderr = System.err;
76 private PipedInputStream pin = new PipedInputStream();
78 private PipedInputStream pin2 = new PipedInputStream();
80 private StringBuffer displayPipe = new StringBuffer();
82 Thread errorThrower; // just for testing (Throws an Exception at this Console
84 // are we attached to some parent Desktop
85 Desktop parent = null;
87 private int MIN_WIDTH = 300;
89 private int MIN_HEIGHT = 250;
93 // create all components and add them
94 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
95 frame = initFrame("Java Console", screenSize.width / 2,
96 screenSize.height / 2, -1, -1);
100 private void initConsole(boolean visible)
102 initConsole(visible, true);
112 private void initConsole(boolean visible, boolean redirect)
114 // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
115 // textArea = cpt.getTextArea();
116 textArea = new JTextArea();
117 textArea.setEditable(false);
118 JButton button = new JButton(MessageManager.getString("action.clear"));
121 frame.getContentPane().setLayout(new BorderLayout());
122 frame.getContentPane().add(new JScrollPane(textArea),
123 BorderLayout.CENTER);
124 frame.getContentPane().add(button, BorderLayout.SOUTH);
125 frame.setVisible(visible);
126 updateConsole = visible;
127 frame.addWindowListener(this);
128 button.addActionListener(this);
137 quit = false; // signals the Threads that they should exit
139 // Starting two seperate threads to read from the PipedInputStreams
141 reader = new Thread(this);
142 reader.setDaemon(true);
145 reader2 = new Thread(this);
146 reader2.setDaemon(true);
148 // and a thread to append text to the textarea
149 textAppender = new Thread(this);
150 textAppender.setDaemon(true);
151 textAppender.start();
154 PipedOutputStream pout = null, perr = null;
156 public void redirectStreams()
162 pout = new PipedOutputStream(this.pin);
163 System.setOut(new PrintStream(pout, true));
164 } catch (java.io.IOException io)
166 textArea.append("Couldn't redirect STDOUT to this console\n"
168 io.printStackTrace(stderr);
169 } catch (SecurityException se)
171 textArea.append("Couldn't redirect STDOUT to this console\n"
173 se.printStackTrace(stderr);
178 perr = new PipedOutputStream(this.pin2);
179 System.setErr(new PrintStream(perr, true));
180 } catch (java.io.IOException io)
182 textArea.append("Couldn't redirect STDERR to this console\n"
184 io.printStackTrace(stderr);
185 } catch (SecurityException se)
187 textArea.append("Couldn't redirect STDERR to this console\n"
189 se.printStackTrace(stderr);
194 public void unredirectStreams()
200 System.setOut(stdout);
203 pin = new PipedInputStream();
205 } catch (java.io.IOException io)
207 textArea.append("Couldn't unredirect STDOUT to this console\n"
209 io.printStackTrace(stderr);
210 } catch (SecurityException se)
212 textArea.append("Couldn't unredirect STDOUT to this console\n"
214 se.printStackTrace(stderr);
219 System.setErr(stderr);
222 pin2 = new PipedInputStream();
224 } catch (java.io.IOException io)
226 textArea.append("Couldn't unredirect STDERR to this console\n"
228 io.printStackTrace(stderr);
229 } catch (SecurityException se)
231 textArea.append("Couldn't unredirect STDERR to this console\n"
233 se.printStackTrace(stderr);
241 // you may omit this part for your application
244 System.out.println("Hello World 2");
245 System.out.println("All fonts available to Graphic2D:\n");
246 GraphicsEnvironment ge = GraphicsEnvironment
247 .getLocalGraphicsEnvironment();
248 String[] fontNames = ge.getAvailableFontFamilyNames();
249 for (int n = 0; n < fontNames.length; n++)
251 System.out.println(fontNames[n]);
253 // Testing part: simple an error thrown anywhere in this JVM will be printed
255 // We do it with a seperate Thread becasue we don't wan't to break a Thread
256 // used by the Console.
257 System.out.println("\nLets throw an error on this console");
258 errorThrower = new Thread(this);
259 errorThrower.setDaemon(true);
260 errorThrower.start();
263 private JFrame initFrame(String string, int i, int j, int x, int y)
265 JFrame frame = new JFrame(string);
266 frame.setName(string);
275 frame.setBounds(x, y, i, j);
280 * attach a console to the desktop - the desktop will open it if requested.
284 public Console(Desktop desktop)
290 * attach a console to the desktop - the desktop will open it if requested.
293 * @param showjconsole
294 * - if true, then redirect stdout immediately
296 public Console(Desktop desktop, boolean showjconsole)
299 // window name - get x,y,width, height possibly scaled
300 Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
303 frame = initFrame("Jalview Java Console", desktop.getWidth() / 2,
304 desktop.getHeight() / 4, desktop.getX(), desktop.getY());
308 frame = initFrame("Jalview Java Console", bounds.width, bounds.height,
311 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
312 // desktop.add(frame);
314 JalviewAppender jappender = new JalviewAppender();
315 jappender.setLayout(new SimpleLayout());
316 JalviewAppender.setTextArea(textArea);
317 org.apache.log4j.Logger.getRootLogger().addAppender(jappender);
320 public synchronized void stopConsole()
325 * reader.notify(); reader2.notify(); if (errorThrower!=null)
326 * errorThrower.notify(); // stop all threads if (textAppender!=null)
327 * textAppender.notify();
335 } catch (Exception e)
342 } catch (Exception e)
347 textAppender.join(10);
348 } catch (Exception e)
352 if (!frame.isVisible())
360 public synchronized void windowClosed(WindowEvent evt)
362 frame.setVisible(false);
366 private void closeConsoleGui()
368 updateConsole = false;
376 parent.showConsole(false);
381 public synchronized void windowClosing(WindowEvent evt)
383 frame.setVisible(false); // default behaviour of JFrame
390 public synchronized void actionPerformed(ActionEvent evt)
393 // textArea.setText("");
397 public synchronized void run()
401 while (Thread.currentThread() == reader)
403 if (pin == null || pin.available() == 0)
408 if (pin.available() == 0)
412 } catch (InterruptedException ie)
417 while (pin.available() != 0)
419 String input = this.readLine(pin);
421 long time = System.nanoTime();
422 appendToTextArea(input);
423 // stderr.println("Time taken to stdout append:\t"
424 // + (System.nanoTime() - time) + " ns");
433 while (Thread.currentThread() == reader2)
435 if (pin2.available() == 0)
440 if (pin2.available() == 0)
444 } catch (InterruptedException ie)
448 while (pin2.available() != 0)
450 String input = this.readLine(pin2);
452 long time = System.nanoTime();
453 appendToTextArea(input);
454 // stderr.println("Time taken to stderr append:\t"
455 // + (System.nanoTime() - time) + " ns");
463 while (Thread.currentThread() == textAppender)
467 // check string buffer - if greater than console, clear console and
468 // replace with last segment of content, otherwise, append all to
471 while (displayPipe.length() > 0)
474 StringBuffer tmp = new StringBuffer(), replace;
475 synchronized (displayPipe)
477 replace = displayPipe;
480 // simply append whole buffer
481 textArea.append(replace.toString());
482 count += replace.length();
483 if (count > byteslim)
488 if (displayPipe.length() == 0)
493 if (displayPipe.length() == 0)
497 } catch (InterruptedException e)
508 } catch (InterruptedException e)
519 } catch (Exception e)
521 textArea.append("\nConsole reports an Internal error.");
522 textArea.append("The error is: " + e.getMessage());
523 // Need to uncomment this to ensure that line tally is synched.
526 "Console reports an Internal error.\nThe error is: " + e);
529 // just for testing (Throw a Nullpointer after 1 second)
530 if (Thread.currentThread() == errorThrower)
535 } catch (InterruptedException ie)
538 throw new NullPointerException(
539 MessageManager.getString("exception.application_test_npe"));
543 private void appendToTextArea(final String input)
545 if (updateConsole == false)
550 long time = System.nanoTime();
551 javax.swing.SwingUtilities.invokeLater(new Runnable()
556 displayPipe.append(input); // change to stringBuffer
557 // displayPipe.flush();
561 // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() -
566 private String header = null;
568 private boolean updateConsole = false;
570 private synchronized void trimBuffer(boolean clear)
572 if (header == null && textArea.getLineCount() > 5)
576 header = textArea.getText(0, textArea.getLineStartOffset(5))
577 + "\nTruncated...\n";
578 } catch (Exception e)
584 int tlength = textArea.getDocument().getLength();
587 if (clear || (tlength > byteslim))
593 long time = System.nanoTime();
594 textArea.replaceRange(header, 0, tlength - bytescut);
595 // stderr.println("Time taken to cut:\t"
596 // + (System.nanoTime() - time) + " ns");
600 textArea.setText(header);
602 } catch (Exception e)
606 // lines = textArea.getLineCount();
612 public synchronized String readLine(PipedInputStream in)
619 int available = in.available();
624 byte b[] = new byte[available];
626 input = input + new String(b, 0, b.length);
627 // counts lines - we don't do this for speed.
628 // while ((lp = input.indexOf("\n", lp + 1)) > -1)
632 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
636 public static void main(String[] arg)
638 new Console().test(); // create console with not reference
642 public void setVisible(boolean selected)
644 frame.setVisible(selected);
645 if (selected == true)
648 updateConsole = true;
654 updateConsole = false;
658 public Rectangle getBounds()
662 return frame.getBounds();
668 * set the banner that appears at the top of the console output
672 public void setHeader(String string)
675 if (header.charAt(header.length() - 1) != '\n')
679 textArea.insert(header, 0);
687 public String getHeader()