2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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/>.
20 import jalview.util.MessageManager;
24 import java.awt.event.*;
27 import org.apache.log4j.SimpleLayout;
30 * Simple Jalview Java Console. Version 1 - allows viewing of console output
31 * after desktop is created. Acquired with thanks from RJHM's site
32 * http://www.comweb.nl/java/Console/Console.html A simple Java Console for your
33 * application (Swing version) Requires Java 1.1.5 or higher Disclaimer the use
34 * of this source is at your own risk. Permision to use and distribute into your
35 * own applications RJHM van den Bergh , rvdb@comweb.nl
38 public class Console extends WindowAdapter implements WindowListener,
39 ActionListener, Runnable
43 private JTextArea textArea;
46 * unused - tally and limit for lines in console window int lines = 0;
50 int byteslim = 102400, bytescut = 76800; // 100k and 75k cut point.
52 private Thread reader, reader2, textAppender;
56 private final PrintStream stdout = System.out, stderr = System.err;
58 private PipedInputStream pin = new PipedInputStream();
60 private PipedInputStream pin2 = new PipedInputStream();
62 private StringBuffer displayPipe = new StringBuffer();
64 Thread errorThrower; // just for testing (Throws an Exception at this Console
66 // are we attached to some parent Desktop
67 Desktop parent = null;
71 // create all components and add them
72 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
73 frame = initFrame("Java Console", screenSize.width / 2,
74 screenSize.height / 2, -1, -1);
78 private void initConsole(boolean visible)
80 initConsole(visible, true);
90 private void initConsole(boolean visible, boolean redirect)
92 // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
93 // textArea = cpt.getTextArea();
94 textArea = new JTextArea();
95 textArea.setEditable(false);
96 JButton button = new JButton(MessageManager.getString("action.clear"));
99 frame.getContentPane().setLayout(new BorderLayout());
100 frame.getContentPane().add(new JScrollPane(textArea),
101 BorderLayout.CENTER);
102 frame.getContentPane().add(button, BorderLayout.SOUTH);
103 frame.setVisible(visible);
104 updateConsole = visible;
105 frame.addWindowListener(this);
106 button.addActionListener(this);
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);
126 // and a thread to append text to the textarea
127 textAppender = new Thread(this);
128 textAppender.setDaemon(true);
129 textAppender.start();
132 PipedOutputStream pout = null, perr = null;
134 public void redirectStreams()
140 pout = new PipedOutputStream(this.pin);
141 System.setOut(new PrintStream(pout, true));
142 } catch (java.io.IOException io)
144 textArea.append("Couldn't redirect STDOUT to this console\n"
146 io.printStackTrace(stderr);
147 } catch (SecurityException se)
149 textArea.append("Couldn't redirect STDOUT to this console\n"
151 se.printStackTrace(stderr);
156 perr = new PipedOutputStream(this.pin2);
157 System.setErr(new PrintStream(perr, true));
158 } catch (java.io.IOException io)
160 textArea.append("Couldn't redirect STDERR to this console\n"
162 io.printStackTrace(stderr);
163 } catch (SecurityException se)
165 textArea.append("Couldn't redirect STDERR to this console\n"
167 se.printStackTrace(stderr);
172 public void unredirectStreams()
178 System.setOut(stdout);
181 pin = new PipedInputStream();
183 } catch (java.io.IOException io)
185 textArea.append("Couldn't unredirect STDOUT to this console\n"
187 io.printStackTrace(stderr);
188 } catch (SecurityException se)
190 textArea.append("Couldn't unredirect STDOUT to this console\n"
192 se.printStackTrace(stderr);
197 System.setErr(stderr);
200 pin2 = new PipedInputStream();
202 } catch (java.io.IOException io)
204 textArea.append("Couldn't unredirect STDERR to this console\n"
206 io.printStackTrace(stderr);
207 } catch (SecurityException se)
209 textArea.append("Couldn't unredirect STDERR to this console\n"
211 se.printStackTrace(stderr);
219 // you may omit this part for your application
222 System.out.println("Hello World 2");
223 System.out.println("All fonts available to Graphic2D:\n");
224 GraphicsEnvironment ge = GraphicsEnvironment
225 .getLocalGraphicsEnvironment();
226 String[] fontNames = ge.getAvailableFontFamilyNames();
227 for (int n = 0; n < fontNames.length; n++)
228 System.out.println(fontNames[n]);
229 // Testing part: simple an error thrown anywhere in this JVM will be printed
231 // We do it with a seperate Thread becasue we don't wan't to break a Thread
232 // used by the Console.
233 System.out.println("\nLets throw an error on this console");
234 errorThrower = new Thread(this);
235 errorThrower.setDaemon(true);
236 errorThrower.start();
239 private JFrame initFrame(String string, int i, int j, int x, int y)
241 JFrame frame = new JFrame(string);
242 frame.setName(string);
247 frame.setBounds(x, y, i, j);
252 * attach a console to the desktop - the desktop will open it if requested.
256 public Console(Desktop desktop)
262 * attach a console to the desktop - the desktop will open it if requested.
265 * @param showjconsole
266 * - if true, then redirect stdout immediately
268 public Console(Desktop desktop, boolean showjconsole)
271 // window name - get x,y,width, height possibly scaled
272 Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
275 frame = initFrame("Jalview Java Console", desktop.getWidth() / 2,
276 desktop.getHeight() / 4, desktop.getX(), desktop.getY());
280 frame = initFrame("Jalview Java Console", bounds.width,
281 bounds.height, bounds.x, bounds.y);
283 // desktop.add(frame);
285 JalviewAppender jappender = new JalviewAppender();
286 jappender.setLayout(new SimpleLayout());
287 JalviewAppender.setTextArea(textArea);
288 org.apache.log4j.Logger.getRootLogger().addAppender(jappender);
291 public synchronized void stopConsole()
296 * reader.notify(); reader2.notify(); if (errorThrower!=null)
297 * errorThrower.notify(); // stop all threads if (textAppender!=null)
298 * textAppender.notify();
306 } catch (Exception e)
313 } catch (Exception e)
318 textAppender.join(10);
319 } catch (Exception e)
323 if (!frame.isVisible())
330 public synchronized void windowClosed(WindowEvent evt)
332 frame.setVisible(false);
336 private void closeConsoleGui()
338 updateConsole = false;
346 parent.showConsole(false);
350 public synchronized void windowClosing(WindowEvent evt)
352 frame.setVisible(false); // default behaviour of JFrame
358 public synchronized void actionPerformed(ActionEvent evt)
361 // textArea.setText("");
364 public synchronized void run()
368 while (Thread.currentThread() == reader)
370 if (pin == null || pin.available() == 0)
375 if (pin.available() == 0)
379 } catch (InterruptedException ie)
384 while (pin.available() != 0)
386 String input = this.readLine(pin);
388 long time = System.nanoTime();
389 appendToTextArea(input);
390 // stderr.println("Time taken to stdout append:\t"
391 // + (System.nanoTime() - time) + " ns");
398 while (Thread.currentThread() == reader2)
400 if (pin2.available() == 0)
405 if (pin2.available() == 0)
409 } catch (InterruptedException ie)
413 while (pin2.available() != 0)
415 String input = this.readLine(pin2);
417 long time = System.nanoTime();
418 appendToTextArea(input);
419 // stderr.println("Time taken to stderr append:\t"
420 // + (System.nanoTime() - time) + " ns");
426 while (Thread.currentThread() == textAppender)
430 // check string buffer - if greater than console, clear console and
431 // replace with last segment of content, otherwise, append all to
434 while (displayPipe.length() > 0)
437 StringBuffer tmp = new StringBuffer(), replace;
438 synchronized (displayPipe)
440 replace = displayPipe;
443 // simply append whole buffer
444 textArea.append(replace.toString());
445 count += replace.length();
446 if (count > byteslim)
451 if (displayPipe.length() == 0)
456 if (displayPipe.length() == 0)
460 } catch (InterruptedException e)
471 } catch (InterruptedException e)
482 } catch (Exception e)
484 textArea.append("\nConsole reports an Internal error.");
485 textArea.append("The error is: " + e.getMessage());
486 // Need to uncomment this to ensure that line tally is synched.
488 stderr.println("Console reports an Internal error.\nThe error is: "
492 // just for testing (Throw a Nullpointer after 1 second)
493 if (Thread.currentThread() == errorThrower)
498 } catch (InterruptedException ie)
501 throw new NullPointerException(
502 "Application test: throwing an NullPointerException It should arrive at the console");
506 private void appendToTextArea(final String input)
508 if (updateConsole == false)
513 long time = System.nanoTime();
514 javax.swing.SwingUtilities.invokeLater(new Runnable()
518 displayPipe.append(input); // change to stringBuffer
519 // displayPipe.flush();
523 // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() -
528 private String header = null;
530 private boolean updateConsole = false;
532 private synchronized void trimBuffer(boolean clear)
534 if (header == null && textArea.getLineCount() > 5)
538 header = textArea.getText(0, textArea.getLineStartOffset(5))
539 + "\nTruncated...\n";
540 } catch (Exception e)
546 int tlength = textArea.getDocument().getLength();
549 if (clear || (tlength > byteslim))
555 long time = System.nanoTime();
556 textArea.replaceRange(header, 0, tlength - bytescut);
557 // stderr.println("Time taken to cut:\t"
558 // + (System.nanoTime() - time) + " ns");
562 textArea.setText(header);
564 } catch (Exception e)
568 // lines = textArea.getLineCount();
574 public synchronized String readLine(PipedInputStream in)
581 int available = in.available();
584 byte b[] = new byte[available];
586 input = input + new String(b, 0, b.length);
587 // counts lines - we don't do this for speed.
588 // while ((lp = input.indexOf("\n", lp + 1)) > -1)
592 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
596 public static void main(String[] arg)
598 new Console().test(); // create console with not reference
602 public void setVisible(boolean selected)
604 frame.setVisible(selected);
605 if (selected == true)
608 updateConsole = true;
614 updateConsole = false;
618 public Rectangle getBounds()
622 return frame.getBounds();
628 * set the banner that appears at the top of the console output
632 public void setHeader(String string)
635 if (header.charAt(header.length() - 1) != '\n')
639 textArea.insert(header, 0);
647 public String getHeader()