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 java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Dimension;
26 import java.awt.GraphicsEnvironment;
27 import java.awt.GridBagConstraints;
28 import java.awt.GridBagLayout;
29 import java.awt.Rectangle;
30 import java.awt.Toolkit;
31 import java.awt.datatransfer.Clipboard;
32 import java.awt.datatransfer.StringSelection;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.MouseAdapter;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.WindowAdapter;
38 import java.awt.event.WindowEvent;
39 import java.awt.event.WindowListener;
40 import java.io.IOException;
41 import java.io.PipedInputStream;
42 import java.io.PipedOutputStream;
43 import java.io.PrintStream;
45 import javax.swing.JButton;
46 import javax.swing.JComboBox;
47 import javax.swing.JFrame;
48 import javax.swing.JLabel;
49 import javax.swing.JPanel;
50 import javax.swing.JScrollPane;
51 import javax.swing.JTextArea;
53 import org.apache.log4j.Level;
54 import org.apache.log4j.SimpleLayout;
56 import jalview.bin.Cache;
57 import jalview.util.MessageManager;
60 * Simple Jalview Java Console. Version 1 - allows viewing of console output
61 * after desktop is created. Acquired with thanks from RJHM's site
62 * http://www.comweb.nl/java/Console/Console.html A simple Java Console for your
63 * application (Swing version) Requires Java 1.1.5 or higher Disclaimer the use
64 * of this source is at your own risk. Permision to use and distribute into your
65 * own applications RJHM van den Bergh , rvdb@comweb.nl
68 public class Console extends WindowAdapter
69 implements WindowListener, ActionListener, Runnable
73 private JTextArea textArea;
76 * unused - tally and limit for lines in console window int lines = 0;
80 int byteslim = 102400, bytescut = 76800; // 100k and 75k cut point.
82 private Thread reader, reader2, textAppender;
86 private final PrintStream stdout = System.out, stderr = System.err;
88 private PipedInputStream pin = new PipedInputStream();
90 private PipedInputStream pin2 = new PipedInputStream();
92 private StringBuffer displayPipe = new StringBuffer();
94 Thread errorThrower; // just for testing (Throws an Exception at this Console
96 // are we attached to some parent Desktop
97 Desktop parent = null;
99 private int MIN_WIDTH = 300;
101 private int MIN_HEIGHT = 250;
103 private JComboBox logLevelCombo = new JComboBox();
107 // create all components and add them
108 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
109 frame = initFrame("Java Console", screenSize.width / 2,
110 screenSize.height / 2, -1, -1);
114 private void initConsole(boolean visible)
116 initConsole(visible, true);
126 private void initConsole(boolean visible, boolean redirect)
128 // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
129 // textArea = cpt.getTextArea();
130 textArea = new JTextArea();
131 textArea.setEditable(false);
132 JButton clearButton = new JButton(
133 MessageManager.getString("action.clear"));
134 JButton copyToClipboardButton = new JButton(
135 MessageManager.getString("label.copy_to_clipboard"));
136 copyToClipboardButton.addActionListener(new ActionListener()
138 public void actionPerformed(ActionEvent e)
140 copyConsoleTextToClipboard();
143 copyToClipboardButton.addMouseListener(new MouseAdapter()
145 private Color bg = textArea.getBackground();
147 private Color fg = textArea.getForeground();
149 public void mousePressed(MouseEvent e)
151 textArea.setBackground(textArea.getSelectionColor());
152 textArea.setForeground(textArea.getSelectedTextColor());
155 public void mouseReleased(MouseEvent e)
157 textArea.setBackground(bg);
158 textArea.setForeground(fg);
162 copyToClipboardButton.setToolTipText(
163 MessageManager.getString("label.copy_to_clipboard_tooltip"));
165 JLabel logLevelLabel = new JLabel(
166 MessageManager.getString("label.log_level") + ":");
168 // logLevelCombo.addItem(Level.ALL);
169 logLevelCombo.addItem(Level.TRACE);
170 logLevelCombo.addItem(Level.DEBUG);
171 logLevelCombo.addItem(Level.INFO);
172 logLevelCombo.addItem(Level.WARN);
173 // logLevelCombo.addItem(Level.ERROR);
174 // logLevelCombo.addItem(Level.FATAL);
175 // logLevelCombo.addItem(Level.OFF);
176 setChosenLogLevelCombo();
177 logLevelCombo.addActionListener(new ActionListener()
179 public void actionPerformed(ActionEvent e)
181 if (Cache.log != null)
183 Cache.log.setLevel((Level) logLevelCombo.getSelectedItem());
190 frame.getContentPane().setLayout(new BorderLayout());
191 frame.getContentPane().add(new JScrollPane(textArea),
192 BorderLayout.CENTER);
193 JPanel southPanel = new JPanel();
194 southPanel.setLayout(new GridBagLayout());
196 JPanel logLevelPanel = new JPanel();
197 logLevelPanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
198 logLevelPanel.add(logLevelLabel);
199 logLevelPanel.add(logLevelCombo);
200 logLevelLabel.setToolTipText(
201 MessageManager.getString("label.log_level_tooltip"));
202 logLevelCombo.setToolTipText(
203 MessageManager.getString("label.log_level_tooltip"));
205 GridBagConstraints gbc = new GridBagConstraints();
211 southPanel.add(logLevelPanel, gbc);
215 gbc.fill = GridBagConstraints.HORIZONTAL;
216 southPanel.add(clearButton, gbc);
220 gbc.fill = GridBagConstraints.NONE;
221 southPanel.add(copyToClipboardButton, gbc);
223 southPanel.setVisible(true);
224 frame.getContentPane().add(southPanel, BorderLayout.SOUTH);
225 frame.setVisible(visible);
226 updateConsole = visible;
227 frame.addWindowListener(this);
228 clearButton.addActionListener(this);
238 quit = false; // signals the Threads that they should exit
240 // Starting two seperate threads to read from the PipedInputStreams
242 reader = new Thread(this);
243 reader.setDaemon(true);
246 reader2 = new Thread(this);
247 reader2.setDaemon(true);
249 // and a thread to append text to the textarea
250 textAppender = new Thread(this);
251 textAppender.setDaemon(true);
252 textAppender.start();
255 private void setChosenLogLevelCombo()
257 Level currentLogLevel = Cache.log == null ? Level.INFO
258 : Cache.log.getLevel();
259 logLevelCombo.setSelectedItem(currentLogLevel);
260 if (!logLevelCombo.getSelectedItem().equals(currentLogLevel)) // currentLogLevel
263 if (currentLogLevel != null && currentLogLevel instanceof Level)
265 // add new item to list (might be set via .jalview_properties)
266 boolean added = false;
267 for (int i = 0; i < logLevelCombo.getItemCount(); i++)
269 Level l = (Level) logLevelCombo.getItemAt(i);
270 if (l.isGreaterOrEqual(currentLogLevel))
272 logLevelCombo.insertItemAt(currentLogLevel, i);
277 if (!added) // lower priority than others or some confusion -- add to
280 logLevelCombo.addItem(currentLogLevel);
282 logLevelCombo.setSelectedItem(currentLogLevel);
286 logLevelCombo.setSelectedItem(Level.INFO);
291 private void copyConsoleTextToClipboard()
293 String consoleText = textArea.getText();
294 StringSelection consoleTextSelection = new StringSelection(consoleText);
295 Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
296 cb.setContents(consoleTextSelection, null);
299 PipedOutputStream pout = null, perr = null;
301 public void redirectStreams()
307 pout = new PipedOutputStream(this.pin);
308 System.setOut(new PrintStream(pout, true));
309 } catch (java.io.IOException io)
311 textArea.append("Couldn't redirect STDOUT to this console\n"
313 io.printStackTrace(stderr);
314 } catch (SecurityException se)
316 textArea.append("Couldn't redirect STDOUT to this console\n"
318 se.printStackTrace(stderr);
323 perr = new PipedOutputStream(this.pin2);
324 System.setErr(new PrintStream(perr, true));
325 } catch (java.io.IOException io)
327 textArea.append("Couldn't redirect STDERR to this console\n"
329 io.printStackTrace(stderr);
330 } catch (SecurityException se)
332 textArea.append("Couldn't redirect STDERR to this console\n"
334 se.printStackTrace(stderr);
339 public void unredirectStreams()
345 System.setOut(stdout);
348 pin = new PipedInputStream();
350 } catch (java.io.IOException io)
352 textArea.append("Couldn't unredirect STDOUT to this console\n"
354 io.printStackTrace(stderr);
355 } catch (SecurityException se)
357 textArea.append("Couldn't unredirect STDOUT to this console\n"
359 se.printStackTrace(stderr);
364 System.setErr(stderr);
367 pin2 = new PipedInputStream();
369 } catch (java.io.IOException io)
371 textArea.append("Couldn't unredirect STDERR to this console\n"
373 io.printStackTrace(stderr);
374 } catch (SecurityException se)
376 textArea.append("Couldn't unredirect STDERR to this console\n"
378 se.printStackTrace(stderr);
386 // you may omit this part for your application
389 System.out.println("Hello World 2");
390 System.out.println("All fonts available to Graphic2D:\n");
391 GraphicsEnvironment ge = GraphicsEnvironment
392 .getLocalGraphicsEnvironment();
393 String[] fontNames = ge.getAvailableFontFamilyNames();
394 for (int n = 0; n < fontNames.length; n++)
396 System.out.println(fontNames[n]);
398 // Testing part: simple an error thrown anywhere in this JVM will be printed
400 // We do it with a seperate Thread becasue we don't wan't to break a Thread
401 // used by the Console.
402 System.out.println("\nLets throw an error on this console");
403 errorThrower = new Thread(this);
404 errorThrower.setDaemon(true);
405 errorThrower.start();
408 private JFrame initFrame(String string, int i, int j, int x, int y)
410 JFrame frame = new JFrame(string);
411 frame.setName(string);
420 frame.setBounds(x, y, i, j);
425 * attach a console to the desktop - the desktop will open it if requested.
429 public Console(Desktop desktop)
435 * attach a console to the desktop - the desktop will open it if requested.
438 * @param showjconsole
439 * - if true, then redirect stdout immediately
441 public Console(Desktop desktop, boolean showjconsole)
444 // window name - get x,y,width, height possibly scaled
445 Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
448 frame = initFrame("Jalview Java Console", desktop.getWidth() / 2,
449 desktop.getHeight() / 4, desktop.getX(), desktop.getY());
453 frame = initFrame("Jalview Java Console", bounds.width, bounds.height,
456 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
457 // desktop.add(frame);
459 JalviewAppender jappender = new JalviewAppender();
460 jappender.setLayout(new SimpleLayout());
461 JalviewAppender.setTextArea(textArea);
462 org.apache.log4j.Logger.getRootLogger().addAppender(jappender);
465 public synchronized void stopConsole()
470 * reader.notify(); reader2.notify(); if (errorThrower!=null)
471 * errorThrower.notify(); // stop all threads if (textAppender!=null)
472 * textAppender.notify();
480 } catch (Exception e)
487 } catch (Exception e)
492 textAppender.join(10);
493 } catch (Exception e)
497 if (!frame.isVisible())
505 public synchronized void windowClosed(WindowEvent evt)
507 frame.setVisible(false);
511 private void closeConsoleGui()
513 updateConsole = false;
521 parent.showConsole(false);
526 public synchronized void windowClosing(WindowEvent evt)
528 frame.setVisible(false); // default behaviour of JFrame
535 public synchronized void actionPerformed(ActionEvent evt)
538 // textArea.setText("");
542 public synchronized void run()
546 while (Thread.currentThread() == reader)
548 if (pin == null || pin.available() == 0)
553 if (pin.available() == 0)
557 } catch (InterruptedException ie)
562 while (pin.available() != 0)
564 String input = this.readLine(pin);
566 long time = System.nanoTime();
567 appendToTextArea(input);
568 // stderr.println("Time taken to stdout append:\t"
569 // + (System.nanoTime() - time) + " ns");
578 while (Thread.currentThread() == reader2)
580 if (pin2.available() == 0)
585 if (pin2.available() == 0)
589 } catch (InterruptedException ie)
593 while (pin2.available() != 0)
595 String input = this.readLine(pin2);
597 long time = System.nanoTime();
598 appendToTextArea(input);
599 // stderr.println("Time taken to stderr append:\t"
600 // + (System.nanoTime() - time) + " ns");
608 while (Thread.currentThread() == textAppender)
612 // check string buffer - if greater than console, clear console and
613 // replace with last segment of content, otherwise, append all to
616 while (displayPipe.length() > 0)
619 StringBuffer tmp = new StringBuffer(), replace;
620 synchronized (displayPipe)
622 replace = displayPipe;
625 // simply append whole buffer
626 textArea.append(replace.toString());
627 count += replace.length();
628 if (count > byteslim)
633 if (displayPipe.length() == 0)
638 if (displayPipe.length() == 0)
642 } catch (InterruptedException e)
653 } catch (InterruptedException e)
664 } catch (Exception e)
666 textArea.append("\nConsole reports an Internal error.");
667 textArea.append("The error is: " + e.getMessage());
668 // Need to uncomment this to ensure that line tally is synched.
671 "Console reports an Internal error.\nThe error is: " + e);
674 // just for testing (Throw a Nullpointer after 1 second)
675 if (Thread.currentThread() == errorThrower)
680 } catch (InterruptedException ie)
683 throw new NullPointerException(
684 MessageManager.getString("exception.application_test_npe"));
688 private void appendToTextArea(final String input)
690 if (updateConsole == false)
695 long time = System.nanoTime();
696 javax.swing.SwingUtilities.invokeLater(new Runnable()
701 displayPipe.append(input); // change to stringBuffer
702 // displayPipe.flush();
706 // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() -
711 private String header = null;
713 private boolean updateConsole = false;
715 private synchronized void trimBuffer(boolean clear)
717 if (header == null && textArea.getLineCount() > 5)
721 header = textArea.getText(0, textArea.getLineStartOffset(5))
722 + "\nTruncated...\n";
723 } catch (Exception e)
729 int tlength = textArea.getDocument().getLength();
732 if (clear || (tlength > byteslim))
738 long time = System.nanoTime();
739 textArea.replaceRange(header, 0, tlength - bytescut);
740 // stderr.println("Time taken to cut:\t"
741 // + (System.nanoTime() - time) + " ns");
745 textArea.setText(header);
747 } catch (Exception e)
751 // lines = textArea.getLineCount();
757 public synchronized String readLine(PipedInputStream in)
764 int available = in.available();
769 byte b[] = new byte[available];
771 input = input + new String(b, 0, b.length);
772 // counts lines - we don't do this for speed.
773 // while ((lp = input.indexOf("\n", lp + 1)) > -1)
777 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
781 public static void main(String[] arg)
783 new Console().test(); // create console with not reference
787 public void setVisible(boolean selected)
789 frame.setVisible(selected);
790 if (selected == true)
792 setChosenLogLevelCombo();
794 updateConsole = true;
799 // reset log level to user preference
800 if (Cache.log != null)
802 String userLogLevel = Cache.getDefault("logs.Jalview.level",
803 Level.INFO.toString());
804 Cache.log.setLevel(Level.toLevel(userLogLevel));
808 updateConsole = false;
812 public Rectangle getBounds()
816 return frame.getBounds();
822 * set the banner that appears at the top of the console output
826 public void setHeader(String string)
829 if (header.charAt(header.length() - 1) != '\n')
833 textArea.insert(header, 0);
841 public String getHeader()