2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
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;
45 * unused - tally and limit for lines in console window int lines = 0;
49 int byteslim = 102400, bytescut = 76800; // 100k and 75k cut point.
51 private Thread reader, reader2, textAppender;
55 private final PrintStream stdout = System.out, stderr = System.err;
57 private PipedInputStream pin = new PipedInputStream();
59 private PipedInputStream pin2 = new PipedInputStream();
61 private StringBuffer displayPipe = new StringBuffer();
63 Thread errorThrower; // just for testing (Throws an Exception at this Console
65 // are we attached to some parent Desktop
66 Desktop parent = null;
70 // create all components and add them
71 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
72 frame = initFrame("Java Console", screenSize.width / 2,
73 screenSize.height / 2, -1, -1);
77 private void initConsole(boolean visible)
79 initConsole(visible, true);
89 private void initConsole(boolean visible, boolean redirect)
91 // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
92 // textArea = cpt.getTextArea();
93 textArea = new JTextArea();
94 textArea.setEditable(false);
95 JButton button = new JButton("clear");
98 frame.getContentPane().setLayout(new BorderLayout());
99 frame.getContentPane().add(new JScrollPane(textArea),
100 BorderLayout.CENTER);
101 frame.getContentPane().add(button, BorderLayout.SOUTH);
102 frame.setVisible(visible);
103 updateConsole = visible;
104 frame.addWindowListener(this);
105 button.addActionListener(this);
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);
125 // and a thread to append text to the textarea
126 textAppender = new Thread(this);
127 textAppender.setDaemon(true);
128 textAppender.start();
131 PipedOutputStream pout = null, perr = null;
133 public void redirectStreams()
139 pout = new PipedOutputStream(this.pin);
140 System.setOut(new PrintStream(pout, true));
141 } catch (java.io.IOException io)
143 textArea.append("Couldn't redirect STDOUT to this console\n"
145 io.printStackTrace(stderr);
146 } catch (SecurityException se)
148 textArea.append("Couldn't redirect STDOUT to this console\n"
150 se.printStackTrace(stderr);
155 perr = new PipedOutputStream(this.pin2);
156 System.setErr(new PrintStream(perr, true));
157 } catch (java.io.IOException io)
159 textArea.append("Couldn't redirect STDERR to this console\n"
161 io.printStackTrace(stderr);
162 } catch (SecurityException se)
164 textArea.append("Couldn't redirect STDERR to this console\n"
166 se.printStackTrace(stderr);
171 public void unredirectStreams()
177 System.setOut(stdout);
180 pin = new PipedInputStream();
182 } catch (java.io.IOException io)
184 textArea.append("Couldn't unredirect STDOUT to this console\n"
186 io.printStackTrace(stderr);
187 } catch (SecurityException se)
189 textArea.append("Couldn't unredirect STDOUT to this console\n"
191 se.printStackTrace(stderr);
196 System.setErr(stderr);
199 pin2 = new PipedInputStream();
201 } catch (java.io.IOException io)
203 textArea.append("Couldn't unredirect STDERR to this console\n"
205 io.printStackTrace(stderr);
206 } catch (SecurityException se)
208 textArea.append("Couldn't unredirect STDERR to this console\n"
210 se.printStackTrace(stderr);
218 // you may omit this part for your application
221 System.out.println("Hello World 2");
222 System.out.println("All fonts available to Graphic2D:\n");
223 GraphicsEnvironment ge = GraphicsEnvironment
224 .getLocalGraphicsEnvironment();
225 String[] fontNames = ge.getAvailableFontFamilyNames();
226 for (int n = 0; n < fontNames.length; n++)
227 System.out.println(fontNames[n]);
228 // Testing part: simple an error thrown anywhere in this JVM will be printed
230 // We do it with a seperate Thread becasue we don't wan't to break a Thread
231 // used by the Console.
232 System.out.println("\nLets throw an error on this console");
233 errorThrower = new Thread(this);
234 errorThrower.setDaemon(true);
235 errorThrower.start();
238 private JFrame initFrame(String string, int i, int j, int x, int y)
240 JFrame frame = new JFrame(string);
241 frame.setName(string);
246 frame.setBounds(x, y, i, j);
251 * attach a console to the desktop - the desktop will open it if requested.
255 public Console(Desktop desktop)
261 * attach a console to the desktop - the desktop will open it if requested.
264 * @param showjconsole
265 * - if true, then redirect stdout immediately
267 public Console(Desktop desktop, boolean showjconsole)
270 // window name - get x,y,width, height possibly scaled
271 Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
274 frame = initFrame("Jalview Java Console", desktop.getWidth() / 2,
275 desktop.getHeight() / 4, desktop.getX(), desktop.getY());
279 frame = initFrame("Jalview Java Console", bounds.width,
280 bounds.height, bounds.x, bounds.y);
282 // desktop.add(frame);
284 JalviewAppender jappender = new JalviewAppender();
285 jappender.setLayout(new SimpleLayout());
286 JalviewAppender.setTextArea(textArea);
287 org.apache.log4j.Logger.getRootLogger().addAppender(jappender);
290 public synchronized void stopConsole()
295 * reader.notify(); reader2.notify(); if (errorThrower!=null)
296 * errorThrower.notify(); // stop all threads if (textAppender!=null)
297 * textAppender.notify();
305 } catch (Exception e)
312 } catch (Exception e)
317 textAppender.join(10);
318 } catch (Exception e)
322 if (!frame.isVisible())
329 public synchronized void windowClosed(WindowEvent evt)
331 frame.setVisible(false);
335 private void closeConsoleGui()
337 updateConsole = false;
345 parent.showConsole(false);
349 public synchronized void windowClosing(WindowEvent evt)
351 frame.setVisible(false); // default behaviour of JFrame
357 public synchronized void actionPerformed(ActionEvent evt)
360 // textArea.setText("");
363 public synchronized void run()
367 while (Thread.currentThread() == reader)
369 if (pin == null || pin.available() == 0)
374 if (pin.available() == 0)
378 } catch (InterruptedException ie)
383 while (pin.available() != 0)
385 String input = this.readLine(pin);
387 long time = System.nanoTime();
388 appendToTextArea(input);
389 // stderr.println("Time taken to stdout append:\t"
390 // + (System.nanoTime() - time) + " ns");
397 while (Thread.currentThread() == reader2)
399 if (pin2.available() == 0)
404 if (pin2.available() == 0)
408 } catch (InterruptedException ie)
412 while (pin2.available() != 0)
414 String input = this.readLine(pin2);
416 long time = System.nanoTime();
417 appendToTextArea(input);
418 // stderr.println("Time taken to stderr append:\t"
419 // + (System.nanoTime() - time) + " ns");
425 while (Thread.currentThread() == textAppender)
429 // check string buffer - if greater than console, clear console and
430 // replace with last segment of content, otherwise, append all to
433 while (displayPipe.length() > 0)
436 StringBuffer tmp = new StringBuffer(), replace;
437 synchronized (displayPipe)
439 replace = displayPipe;
442 // simply append whole buffer
443 textArea.append(replace.toString());
444 count += replace.length();
445 if (count > byteslim)
450 if (displayPipe.length() == 0)
455 if (displayPipe.length() == 0)
459 } catch (InterruptedException e)
470 } catch (InterruptedException e)
481 } catch (Exception e)
483 textArea.append("\nConsole reports an Internal error.");
484 textArea.append("The error is: " + e.getMessage());
485 // Need to uncomment this to ensure that line tally is synched.
487 stderr.println("Console reports an Internal error.\nThe error is: "
491 // just for testing (Throw a Nullpointer after 1 second)
492 if (Thread.currentThread() == errorThrower)
497 } catch (InterruptedException ie)
500 throw new NullPointerException(
501 "Application test: throwing an NullPointerException It should arrive at the console");
505 private void appendToTextArea(final String input)
507 if (updateConsole == false)
512 long time = System.nanoTime();
513 javax.swing.SwingUtilities.invokeLater(new Runnable()
517 displayPipe.append(input); // change to stringBuffer
518 // displayPipe.flush();
522 // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() -
527 private String header = null;
529 private boolean updateConsole = false;
531 private synchronized void trimBuffer(boolean clear)
533 if (header == null && textArea.getLineCount() > 5)
537 header = textArea.getText(0, textArea.getLineStartOffset(5))
538 + "\nTruncated...\n";
539 } catch (Exception e)
545 int tlength = textArea.getDocument().getLength();
548 if (clear || (tlength > byteslim))
554 long time = System.nanoTime();
555 textArea.replaceRange(header, 0, tlength - bytescut);
556 // stderr.println("Time taken to cut:\t"
557 // + (System.nanoTime() - time) + " ns");
561 textArea.setText(header);
563 } catch (Exception e)
567 // lines = textArea.getLineCount();
573 public synchronized String readLine(PipedInputStream in)
580 int available = in.available();
583 byte b[] = new byte[available];
585 input = input + new String(b, 0, b.length);
586 // counts lines - we don't do this for speed.
587 // while ((lp = input.indexOf("\n", lp + 1)) > -1)
591 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
595 public static void main(String[] arg)
597 new Console().test(); // create console with not reference
601 public void setVisible(boolean selected)
603 frame.setVisible(selected);
604 if (selected == true)
607 updateConsole = true;
613 updateConsole = false;
617 public Rectangle getBounds()
621 return frame.getBounds();
627 * set the banner that appears at the top of the console output
631 public void setHeader(String string)
634 if (header.charAt(header.length() - 1) != '\n')
638 textArea.insert(header, 0);
646 public String getHeader()