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;
27 import java.awt.event.*;
30 import org.apache.log4j.SimpleLayout;
33 * Simple Jalview Java Console. Version 1 - allows viewing of console output
34 * after desktop is created. Acquired with thanks from RJHM's site
35 * http://www.comweb.nl/java/Console/Console.html A simple Java Console for your
36 * application (Swing version) Requires Java 1.1.5 or higher Disclaimer the use
37 * of this source is at your own risk. Permision to use and distribute into your
38 * own applications RJHM van den Bergh , rvdb@comweb.nl
41 public class Console extends WindowAdapter implements WindowListener,
42 ActionListener, Runnable
46 private JTextArea textArea;
49 * unused - tally and limit for lines in console window int lines = 0;
53 int byteslim = 102400, bytescut = 76800; // 100k and 75k cut point.
55 private Thread reader, reader2, textAppender;
59 private final PrintStream stdout = System.out, stderr = System.err;
61 private PipedInputStream pin = new PipedInputStream();
63 private PipedInputStream pin2 = new PipedInputStream();
65 private StringBuffer displayPipe = new StringBuffer();
67 Thread errorThrower; // just for testing (Throws an Exception at this Console
69 // are we attached to some parent Desktop
70 Desktop parent = null;
74 // create all components and add them
75 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
76 frame = initFrame("Java Console", screenSize.width / 2,
77 screenSize.height / 2, -1, -1);
81 private void initConsole(boolean visible)
83 initConsole(visible, true);
93 private void initConsole(boolean visible, boolean redirect)
95 // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
96 // textArea = cpt.getTextArea();
97 textArea = new JTextArea();
98 textArea.setEditable(false);
99 JButton button = new JButton(MessageManager.getString("action.clear"));
102 frame.getContentPane().setLayout(new BorderLayout());
103 frame.getContentPane().add(new JScrollPane(textArea),
104 BorderLayout.CENTER);
105 frame.getContentPane().add(button, BorderLayout.SOUTH);
106 frame.setVisible(visible);
107 updateConsole = visible;
108 frame.addWindowListener(this);
109 button.addActionListener(this);
118 quit = false; // signals the Threads that they should exit
120 // Starting two seperate threads to read from the PipedInputStreams
122 reader = new Thread(this);
123 reader.setDaemon(true);
126 reader2 = new Thread(this);
127 reader2.setDaemon(true);
129 // and a thread to append text to the textarea
130 textAppender = new Thread(this);
131 textAppender.setDaemon(true);
132 textAppender.start();
135 PipedOutputStream pout = null, perr = null;
137 public void redirectStreams()
143 pout = new PipedOutputStream(this.pin);
144 System.setOut(new PrintStream(pout, true));
145 } catch (java.io.IOException io)
147 textArea.append("Couldn't redirect STDOUT to this console\n"
149 io.printStackTrace(stderr);
150 } catch (SecurityException se)
152 textArea.append("Couldn't redirect STDOUT to this console\n"
154 se.printStackTrace(stderr);
159 perr = new PipedOutputStream(this.pin2);
160 System.setErr(new PrintStream(perr, true));
161 } catch (java.io.IOException io)
163 textArea.append("Couldn't redirect STDERR to this console\n"
165 io.printStackTrace(stderr);
166 } catch (SecurityException se)
168 textArea.append("Couldn't redirect STDERR to this console\n"
170 se.printStackTrace(stderr);
175 public void unredirectStreams()
181 System.setOut(stdout);
184 pin = new PipedInputStream();
186 } catch (java.io.IOException io)
188 textArea.append("Couldn't unredirect STDOUT to this console\n"
190 io.printStackTrace(stderr);
191 } catch (SecurityException se)
193 textArea.append("Couldn't unredirect STDOUT to this console\n"
195 se.printStackTrace(stderr);
200 System.setErr(stderr);
203 pin2 = new PipedInputStream();
205 } catch (java.io.IOException io)
207 textArea.append("Couldn't unredirect STDERR to this console\n"
209 io.printStackTrace(stderr);
210 } catch (SecurityException se)
212 textArea.append("Couldn't unredirect STDERR to this console\n"
214 se.printStackTrace(stderr);
222 // you may omit this part for your application
225 System.out.println("Hello World 2");
226 System.out.println("All fonts available to Graphic2D:\n");
227 GraphicsEnvironment ge = GraphicsEnvironment
228 .getLocalGraphicsEnvironment();
229 String[] fontNames = ge.getAvailableFontFamilyNames();
230 for (int n = 0; n < fontNames.length; n++)
231 System.out.println(fontNames[n]);
232 // Testing part: simple an error thrown anywhere in this JVM will be printed
234 // We do it with a seperate Thread becasue we don't wan't to break a Thread
235 // used by the Console.
236 System.out.println("\nLets throw an error on this console");
237 errorThrower = new Thread(this);
238 errorThrower.setDaemon(true);
239 errorThrower.start();
242 private JFrame initFrame(String string, int i, int j, int x, int y)
244 JFrame frame = new JFrame(string);
245 frame.setName(string);
250 frame.setBounds(x, y, i, j);
255 * attach a console to the desktop - the desktop will open it if requested.
259 public Console(Desktop desktop)
265 * attach a console to the desktop - the desktop will open it if requested.
268 * @param showjconsole
269 * - if true, then redirect stdout immediately
271 public Console(Desktop desktop, boolean showjconsole)
274 // window name - get x,y,width, height possibly scaled
275 Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
278 frame = initFrame("Jalview Java Console", desktop.getWidth() / 2,
279 desktop.getHeight() / 4, desktop.getX(), desktop.getY());
283 frame = initFrame("Jalview Java Console", bounds.width,
284 bounds.height, bounds.x, bounds.y);
286 // desktop.add(frame);
288 JalviewAppender jappender = new JalviewAppender();
289 jappender.setLayout(new SimpleLayout());
290 JalviewAppender.setTextArea(textArea);
291 org.apache.log4j.Logger.getRootLogger().addAppender(jappender);
294 public synchronized void stopConsole()
299 * reader.notify(); reader2.notify(); if (errorThrower!=null)
300 * errorThrower.notify(); // stop all threads if (textAppender!=null)
301 * textAppender.notify();
309 } catch (Exception e)
316 } catch (Exception e)
321 textAppender.join(10);
322 } catch (Exception e)
326 if (!frame.isVisible())
333 public synchronized void windowClosed(WindowEvent evt)
335 frame.setVisible(false);
339 private void closeConsoleGui()
341 updateConsole = false;
349 parent.showConsole(false);
353 public synchronized void windowClosing(WindowEvent evt)
355 frame.setVisible(false); // default behaviour of JFrame
361 public synchronized void actionPerformed(ActionEvent evt)
364 // textArea.setText("");
367 public synchronized void run()
371 while (Thread.currentThread() == reader)
373 if (pin == null || pin.available() == 0)
378 if (pin.available() == 0)
382 } catch (InterruptedException ie)
387 while (pin.available() != 0)
389 String input = this.readLine(pin);
391 long time = System.nanoTime();
392 appendToTextArea(input);
393 // stderr.println("Time taken to stdout append:\t"
394 // + (System.nanoTime() - time) + " ns");
401 while (Thread.currentThread() == reader2)
403 if (pin2.available() == 0)
408 if (pin2.available() == 0)
412 } catch (InterruptedException ie)
416 while (pin2.available() != 0)
418 String input = this.readLine(pin2);
420 long time = System.nanoTime();
421 appendToTextArea(input);
422 // stderr.println("Time taken to stderr append:\t"
423 // + (System.nanoTime() - time) + " ns");
429 while (Thread.currentThread() == textAppender)
433 // check string buffer - if greater than console, clear console and
434 // replace with last segment of content, otherwise, append all to
437 while (displayPipe.length() > 0)
440 StringBuffer tmp = new StringBuffer(), replace;
441 synchronized (displayPipe)
443 replace = displayPipe;
446 // simply append whole buffer
447 textArea.append(replace.toString());
448 count += replace.length();
449 if (count > byteslim)
454 if (displayPipe.length() == 0)
459 if (displayPipe.length() == 0)
463 } catch (InterruptedException e)
474 } catch (InterruptedException e)
485 } catch (Exception e)
487 textArea.append("\nConsole reports an Internal error.");
488 textArea.append("The error is: " + e.getMessage());
489 // Need to uncomment this to ensure that line tally is synched.
491 stderr.println("Console reports an Internal error.\nThe error is: "
495 // just for testing (Throw a Nullpointer after 1 second)
496 if (Thread.currentThread() == errorThrower)
501 } catch (InterruptedException ie)
504 throw new NullPointerException(MessageManager.getString("exception.application_test_npe"));
508 private void appendToTextArea(final String input)
510 if (updateConsole == false)
515 long time = System.nanoTime();
516 javax.swing.SwingUtilities.invokeLater(new Runnable()
520 displayPipe.append(input); // change to stringBuffer
521 // displayPipe.flush();
525 // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() -
530 private String header = null;
532 private boolean updateConsole = false;
534 private synchronized void trimBuffer(boolean clear)
536 if (header == null && textArea.getLineCount() > 5)
540 header = textArea.getText(0, textArea.getLineStartOffset(5))
541 + "\nTruncated...\n";
542 } catch (Exception e)
548 int tlength = textArea.getDocument().getLength();
551 if (clear || (tlength > byteslim))
557 long time = System.nanoTime();
558 textArea.replaceRange(header, 0, tlength - bytescut);
559 // stderr.println("Time taken to cut:\t"
560 // + (System.nanoTime() - time) + " ns");
564 textArea.setText(header);
566 } catch (Exception e)
570 // lines = textArea.getLineCount();
576 public synchronized String readLine(PipedInputStream in)
583 int available = in.available();
586 byte b[] = new byte[available];
588 input = input + new String(b, 0, b.length);
589 // counts lines - we don't do this for speed.
590 // while ((lp = input.indexOf("\n", lp + 1)) > -1)
594 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
598 public static void main(String[] arg)
600 new Console().test(); // create console with not reference
604 public void setVisible(boolean selected)
606 frame.setVisible(selected);
607 if (selected == true)
610 updateConsole = true;
616 updateConsole = false;
620 public Rectangle getBounds()
624 return frame.getBounds();
630 * set the banner that appears at the top of the console output
634 public void setHeader(String string)
637 if (header.charAt(header.length() - 1) != '\n')
641 textArea.insert(header, 0);
649 public String getHeader()