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.ChannelProperties;
58 import jalview.util.MessageManager;
61 * Simple Jalview Java Console. Version 1 - allows viewing of console output
62 * after desktop is created. Acquired with thanks from RJHM's site
63 * http://www.comweb.nl/java/Console/Console.html A simple Java Console for your
64 * application (Swing version) Requires Java 1.1.5 or higher Disclaimer the use
65 * of this source is at your own risk. Permision to use and distribute into your
66 * own applications RJHM van den Bergh , rvdb@comweb.nl
69 public class Console extends WindowAdapter
70 implements WindowListener, ActionListener, Runnable
74 private JTextArea textArea;
77 * unused - tally and limit for lines in console window int lines = 0;
81 int byteslim = 102400, bytescut = 76800; // 100k and 75k cut point.
83 private Thread reader, reader2, textAppender;
87 private final PrintStream stdout = System.out, stderr = System.err;
89 private PipedInputStream pin = new PipedInputStream();
91 private PipedInputStream pin2 = new PipedInputStream();
93 private StringBuffer displayPipe = new StringBuffer();
95 Thread errorThrower; // just for testing (Throws an Exception at this Console
97 // are we attached to some parent Desktop
98 Desktop parent = null;
100 private int MIN_WIDTH = 300;
102 private int MIN_HEIGHT = 250;
104 private JComboBox<Level> logLevelCombo = new JComboBox<Level>();
106 protected Level startingLogLevel = Level.INFO;
110 // create all components and add them
111 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
112 frame = initFrame("Java Console", screenSize.width / 2,
113 screenSize.height / 2, -1, -1);
117 private void initConsole(boolean visible)
119 initConsole(visible, true);
129 private void initConsole(boolean visible, boolean redirect)
131 // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
132 // textArea = cpt.getTextArea();
133 textArea = new JTextArea();
134 textArea.setEditable(false);
135 JButton clearButton = new JButton(
136 MessageManager.getString("action.clear"));
137 JButton copyToClipboardButton = new JButton(
138 MessageManager.getString("label.copy_to_clipboard"));
139 copyToClipboardButton.addActionListener(new ActionListener()
141 public void actionPerformed(ActionEvent e)
143 copyConsoleTextToClipboard();
146 copyToClipboardButton.addMouseListener(new MouseAdapter()
148 private Color bg = textArea.getBackground();
150 private Color fg = textArea.getForeground();
152 public void mousePressed(MouseEvent e)
154 textArea.setBackground(textArea.getSelectionColor());
155 textArea.setForeground(textArea.getSelectedTextColor());
158 public void mouseReleased(MouseEvent e)
160 textArea.setBackground(bg);
161 textArea.setForeground(fg);
165 copyToClipboardButton.setToolTipText(
166 MessageManager.getString("label.copy_to_clipboard_tooltip"));
168 JLabel logLevelLabel = new JLabel(
169 MessageManager.getString("label.log_level") + ":");
171 // logLevelCombo.addItem(Level.ALL);
172 logLevelCombo.addItem(Level.TRACE);
173 logLevelCombo.addItem(Level.DEBUG);
174 logLevelCombo.addItem(Level.INFO);
175 logLevelCombo.addItem(Level.WARN);
176 // logLevelCombo.addItem(Level.ERROR);
177 // logLevelCombo.addItem(Level.FATAL);
178 // logLevelCombo.addItem(Level.OFF);
179 // set startingLogLevel
180 startingLogLevel = Cache.log == null ? Level.INFO
181 : Cache.log.getLevel();
182 setChosenLogLevelCombo();
183 logLevelCombo.addActionListener(new ActionListener()
185 public void actionPerformed(ActionEvent e)
187 if (Cache.log != null)
189 Cache.log.setLevel((Level) logLevelCombo.getSelectedItem());
196 frame.getContentPane().setLayout(new BorderLayout());
197 frame.getContentPane().add(new JScrollPane(textArea),
198 BorderLayout.CENTER);
199 JPanel southPanel = new JPanel();
200 southPanel.setLayout(new GridBagLayout());
202 JPanel logLevelPanel = new JPanel();
203 logLevelPanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
204 logLevelPanel.add(logLevelLabel);
205 logLevelPanel.add(logLevelCombo);
206 String logLevelTooltip = MessageManager.formatMessage(
207 "label.log_level_tooltip", startingLogLevel.toString());
208 logLevelLabel.setToolTipText(logLevelTooltip);
209 logLevelCombo.setToolTipText(logLevelTooltip);
211 GridBagConstraints gbc = new GridBagConstraints();
217 southPanel.add(logLevelPanel, gbc);
221 gbc.fill = GridBagConstraints.HORIZONTAL;
222 southPanel.add(clearButton, gbc);
226 gbc.fill = GridBagConstraints.NONE;
227 southPanel.add(copyToClipboardButton, gbc);
229 southPanel.setVisible(true);
230 frame.getContentPane().add(southPanel, BorderLayout.SOUTH);
231 frame.setVisible(visible);
232 updateConsole = visible;
233 frame.addWindowListener(this);
234 clearButton.addActionListener(this);
244 quit = false; // signals the Threads that they should exit
246 // Starting two seperate threads to read from the PipedInputStreams
248 reader = new Thread(this);
249 reader.setDaemon(true);
252 reader2 = new Thread(this);
253 reader2.setDaemon(true);
255 // and a thread to append text to the textarea
256 textAppender = new Thread(this);
257 textAppender.setDaemon(true);
258 textAppender.start();
261 frame.setIconImages(ChannelProperties.getIconList());
264 private void setChosenLogLevelCombo()
266 setChosenLogLevelCombo(startingLogLevel);
269 private void setChosenLogLevelCombo(Level setLogLevel)
271 logLevelCombo.setSelectedItem(setLogLevel);
272 if (!logLevelCombo.getSelectedItem().equals(setLogLevel))
274 // setLogLevel not (yet) in list
275 if (setLogLevel != null && setLogLevel instanceof Level)
277 // add new item to list (might be set via .jalview_properties)
278 boolean added = false;
279 for (int i = 0; i < logLevelCombo.getItemCount(); i++)
281 Level l = (Level) logLevelCombo.getItemAt(i);
282 if (l.isGreaterOrEqual(setLogLevel))
284 logLevelCombo.insertItemAt(setLogLevel, i);
289 if (!added) // lower priority than others or some confusion -- add to
292 logLevelCombo.addItem(setLogLevel);
294 logLevelCombo.setSelectedItem(setLogLevel);
298 logLevelCombo.setSelectedItem(Level.INFO);
303 private void copyConsoleTextToClipboard()
305 String consoleText = textArea.getText();
306 StringSelection consoleTextSelection = new StringSelection(consoleText);
307 Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
308 cb.setContents(consoleTextSelection, null);
311 PipedOutputStream pout = null, perr = null;
313 public void redirectStreams()
319 pout = new PipedOutputStream(this.pin);
320 System.setOut(new PrintStream(pout, true));
321 } catch (java.io.IOException io)
323 textArea.append("Couldn't redirect STDOUT to this console\n"
325 io.printStackTrace(stderr);
326 } catch (SecurityException se)
328 textArea.append("Couldn't redirect STDOUT to this console\n"
330 se.printStackTrace(stderr);
335 perr = new PipedOutputStream(this.pin2);
336 System.setErr(new PrintStream(perr, true));
337 } catch (java.io.IOException io)
339 textArea.append("Couldn't redirect STDERR to this console\n"
341 io.printStackTrace(stderr);
342 } catch (SecurityException se)
344 textArea.append("Couldn't redirect STDERR to this console\n"
346 se.printStackTrace(stderr);
351 public void unredirectStreams()
357 System.setOut(stdout);
360 pin = new PipedInputStream();
362 } catch (java.io.IOException io)
364 textArea.append("Couldn't unredirect STDOUT to this console\n"
366 io.printStackTrace(stderr);
367 } catch (SecurityException se)
369 textArea.append("Couldn't unredirect STDOUT to this console\n"
371 se.printStackTrace(stderr);
376 System.setErr(stderr);
379 pin2 = new PipedInputStream();
381 } catch (java.io.IOException io)
383 textArea.append("Couldn't unredirect STDERR to this console\n"
385 io.printStackTrace(stderr);
386 } catch (SecurityException se)
388 textArea.append("Couldn't unredirect STDERR to this console\n"
390 se.printStackTrace(stderr);
398 // you may omit this part for your application
401 System.out.println("Hello World 2");
402 System.out.println("All fonts available to Graphic2D:\n");
403 GraphicsEnvironment ge = GraphicsEnvironment
404 .getLocalGraphicsEnvironment();
405 String[] fontNames = ge.getAvailableFontFamilyNames();
406 for (int n = 0; n < fontNames.length; n++)
408 System.out.println(fontNames[n]);
410 // Testing part: simple an error thrown anywhere in this JVM will be printed
412 // We do it with a seperate Thread becasue we don't wan't to break a Thread
413 // used by the Console.
414 System.out.println("\nLets throw an error on this console");
415 errorThrower = new Thread(this);
416 errorThrower.setDaemon(true);
417 errorThrower.start();
420 private JFrame initFrame(String string, int i, int j, int x, int y)
422 JFrame frame = new JFrame(string);
423 frame.setName(string);
432 frame.setBounds(x, y, i, j);
437 * attach a console to the desktop - the desktop will open it if requested.
441 public Console(Desktop desktop)
447 * attach a console to the desktop - the desktop will open it if requested.
450 * @param showjconsole
451 * - if true, then redirect stdout immediately
453 public Console(Desktop desktop, boolean showjconsole)
456 // window name - get x,y,width, height possibly scaled
457 Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
461 ChannelProperties.getProperty("app_name") + " Java Console",
462 desktop.getWidth() / 2, desktop.getHeight() / 4,
463 desktop.getX(), desktop.getY());
468 ChannelProperties.getProperty("app_name") + " Java Console",
469 bounds.width, bounds.height, bounds.x, bounds.y);
471 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
472 // desktop.add(frame);
474 JalviewAppender jappender = new JalviewAppender();
475 jappender.setLayout(new SimpleLayout());
476 JalviewAppender.setTextArea(textArea);
477 org.apache.log4j.Logger.getRootLogger().addAppender(jappender);
480 public synchronized void stopConsole()
485 * reader.notify(); reader2.notify(); if (errorThrower!=null)
486 * errorThrower.notify(); // stop all threads if (textAppender!=null)
487 * textAppender.notify();
495 } catch (Exception e)
502 } catch (Exception e)
507 textAppender.join(10);
508 } catch (Exception e)
512 if (!frame.isVisible())
520 public synchronized void windowClosed(WindowEvent evt)
522 frame.setVisible(false);
526 private void closeConsoleGui()
528 updateConsole = false;
536 parent.showConsole(false);
541 public synchronized void windowClosing(WindowEvent evt)
543 frame.setVisible(false); // default behaviour of JFrame
550 public synchronized void actionPerformed(ActionEvent evt)
553 // textArea.setText("");
557 public synchronized void run()
561 while (Thread.currentThread() == reader)
563 if (pin == null || pin.available() == 0)
568 if (pin.available() == 0)
572 } catch (InterruptedException ie)
577 while (pin.available() != 0)
579 String input = this.readLine(pin);
581 long time = System.nanoTime();
582 appendToTextArea(input);
583 // stderr.println("Time taken to stdout append:\t"
584 // + (System.nanoTime() - time) + " ns");
593 while (Thread.currentThread() == reader2)
595 if (pin2.available() == 0)
600 if (pin2.available() == 0)
604 } catch (InterruptedException ie)
608 while (pin2.available() != 0)
610 String input = this.readLine(pin2);
612 long time = System.nanoTime();
613 appendToTextArea(input);
614 // stderr.println("Time taken to stderr append:\t"
615 // + (System.nanoTime() - time) + " ns");
623 while (Thread.currentThread() == textAppender)
627 // check string buffer - if greater than console, clear console and
628 // replace with last segment of content, otherwise, append all to
631 while (displayPipe.length() > 0)
634 StringBuffer tmp = new StringBuffer(), replace;
635 synchronized (displayPipe)
637 replace = displayPipe;
640 // simply append whole buffer
641 textArea.append(replace.toString());
642 count += replace.length();
643 if (count > byteslim)
648 if (displayPipe.length() == 0)
653 if (displayPipe.length() == 0)
657 } catch (InterruptedException e)
667 } catch (InterruptedException e)
678 } catch (Exception e)
680 textArea.append("\nConsole reports an Internal error.");
681 textArea.append("The error is: " + e.getMessage());
682 // Need to uncomment this to ensure that line tally is synched.
685 "Console reports an Internal error.\nThe error is: " + e);
688 // just for testing (Throw a Nullpointer after 1 second)
689 if (Thread.currentThread() == errorThrower)
694 } catch (InterruptedException ie)
697 throw new NullPointerException(
698 MessageManager.getString("exception.application_test_npe"));
702 private void appendToTextArea(final String input)
704 if (updateConsole == false)
709 long time = System.nanoTime();
710 javax.swing.SwingUtilities.invokeLater(new Runnable()
715 displayPipe.append(input); // change to stringBuffer
716 // displayPipe.flush();
720 // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() -
725 private String header = null;
727 private boolean updateConsole = false;
729 private synchronized void trimBuffer(boolean clear)
731 if (header == null && textArea.getLineCount() > 5)
735 header = textArea.getText(0, textArea.getLineStartOffset(5))
736 + "\nTruncated...\n";
737 } catch (Exception e)
743 int tlength = textArea.getDocument().getLength();
746 if (clear || (tlength > byteslim))
752 long time = System.nanoTime();
753 textArea.replaceRange(header, 0, tlength - bytescut);
754 // stderr.println("Time taken to cut:\t"
755 // + (System.nanoTime() - time) + " ns");
759 textArea.setText(header);
761 } catch (Exception e)
765 // lines = textArea.getLineCount();
771 public synchronized String readLine(PipedInputStream in)
778 int available = in.available();
783 byte b[] = new byte[available];
785 input = input + new String(b, 0, b.length);
786 // counts lines - we don't do this for speed.
787 // while ((lp = input.indexOf("\n", lp + 1)) > -1)
791 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
799 public static void main(String[] arg)
801 new Console().test(); // create console with not reference
805 public void setVisible(boolean selected)
807 frame.setVisible(selected);
808 if (selected == true)
810 setChosenLogLevelCombo();
812 updateConsole = true;
817 // reset log level to what it was before
818 if (Cache.log != null)
820 Cache.log.setLevel(startingLogLevel);
824 updateConsole = false;
828 public Rectangle getBounds()
832 return frame.getBounds();
838 * set the banner that appears at the top of the console output
842 public void setHeader(String string)
845 if (header.charAt(header.length() - 1) != '\n')
849 textArea.insert(header, 0);
857 public String getHeader()