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.BorderFactory;
46 import javax.swing.JButton;
47 import javax.swing.JComboBox;
48 import javax.swing.JFrame;
49 import javax.swing.JLabel;
50 import javax.swing.JPanel;
51 import javax.swing.JScrollPane;
52 import javax.swing.JTextArea;
53 import javax.swing.SwingUtilities;
54 import javax.swing.border.Border;
55 import javax.swing.text.DefaultCaret;
57 import jalview.bin.Cache;
58 import jalview.log.JLoggerI.LogLevel;
59 import jalview.log.JLoggerLog4j;
60 import jalview.log.JalviewAppender;
61 import jalview.util.ChannelProperties;
62 import jalview.util.MessageManager;
63 import jalview.util.Platform;
66 * Simple Jalview Java Console. Version 1 - allows viewing of console output
67 * after desktop is created. Acquired with thanks from RJHM's site
68 * http://www.comweb.nl/java/Console/Console.html A simple Java Console for your
69 * application (Swing version) Requires Java 1.1.5 or higher Disclaimer the use
70 * of this source is at your own risk. Permision to use and distribute into your
71 * own applications RJHM van den Bergh , rvdb@comweb.nl
74 public class Console extends WindowAdapter
75 implements WindowListener, ActionListener, Runnable
79 private JTextArea textArea;
82 * unused - tally and limit for lines in console window int lines = 0;
86 int byteslim = 102400, bytescut = 76800; // 100k and 75k cut point.
88 private Thread reader, reader2, textAppender;
92 private final PrintStream stdout = System.out, stderr = System.err;
94 private PipedInputStream pin = new PipedInputStream();
96 private PipedInputStream pin2 = new PipedInputStream();
98 private StringBuffer displayPipe = new StringBuffer();
100 Thread errorThrower; // just for testing (Throws an Exception at this Console
102 // are we attached to some parent Desktop
103 Desktop parent = null;
105 private int MIN_WIDTH = 300;
107 private int MIN_HEIGHT = 250;
109 private JComboBox<LogLevel> logLevelCombo = new JComboBox<LogLevel>();
111 protected LogLevel startingLogLevel = null;
115 // create all components and add them
116 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
117 frame = initFrame("Java Console", screenSize.width / 2,
118 screenSize.height / 2, -1, -1);
119 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
123 private void initConsole(boolean visible)
125 initConsole(visible, true);
135 private void initConsole(boolean visible, boolean redirect)
137 // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
138 // textArea = cpt.getTextArea();
139 textArea = new JTextArea();
140 textArea.setEditable(false);
142 DefaultCaret caret = (DefaultCaret) textArea.getCaret();
143 caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
144 // toggle autoscroll by clicking on the text area
145 Border pausedBorder = BorderFactory.createMatteBorder(2, 2, 2, 2,
146 textArea.getForeground());
147 Border noBorder = BorderFactory.createEmptyBorder(2, 2, 2, 2);
148 JScrollPane scrollPane = new JScrollPane(textArea);
149 scrollPane.setBorder(noBorder);
150 textArea.addMouseListener(new MouseAdapter()
153 public void mouseClicked(MouseEvent e)
155 if (e.getButton() == MouseEvent.BUTTON1)
157 if (caret.getUpdatePolicy() == DefaultCaret.ALWAYS_UPDATE)
159 caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
160 scrollPane.setBorder(pausedBorder);
164 caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
165 textArea.setCaretPosition(textArea.getDocument().getLength());
166 scrollPane.setBorder(noBorder);
172 JButton clearButton = new JButton(
173 MessageManager.getString("action.clear"));
174 JButton copyToClipboardButton = new JButton(
175 MessageManager.getString("label.copy_to_clipboard"));
176 copyToClipboardButton.addActionListener(new ActionListener()
179 public void actionPerformed(ActionEvent e)
181 copyConsoleTextToClipboard();
184 copyToClipboardButton.addMouseListener(new MouseAdapter()
186 private Color bg = textArea.getBackground();
188 private Color fg = textArea.getForeground();
191 public void mousePressed(MouseEvent e)
193 textArea.setBackground(textArea.getSelectionColor());
194 textArea.setForeground(textArea.getSelectedTextColor());
198 public void mouseReleased(MouseEvent e)
200 textArea.setBackground(bg);
201 textArea.setForeground(fg);
205 copyToClipboardButton.setToolTipText(
206 MessageManager.getString("label.copy_to_clipboard_tooltip"));
208 JLabel logLevelLabel = new JLabel(
209 MessageManager.getString("label.log_level") + ":");
211 // logLevelCombo.addItem(LogLevel.ALL);
212 logLevelCombo.addItem(LogLevel.TRACE);
213 logLevelCombo.addItem(LogLevel.DEBUG);
214 logLevelCombo.addItem(LogLevel.INFO);
215 logLevelCombo.addItem(LogLevel.WARN);
216 // logLevelCombo.addItem(LogLevel.ERROR);
217 // logLevelCombo.addItem(LogLevel.FATAL);
218 // logLevelCombo.addItem(LogLevel.ERROR);
219 // logLevelCombo.addItem(LogLevel.OFF);
220 // set startingLogLevel
222 if (jalview.bin.Console.getLogger() == null)
224 startingLogLevel = jalview.bin.Console.getCachedLogLevel();
228 startingLogLevel = jalview.bin.Console.getLogger().getLevel();
230 setChosenLogLevelCombo();
231 logLevelCombo.addActionListener(new ActionListener()
234 public void actionPerformed(ActionEvent e)
236 if (jalview.bin.Console.log != null)
238 jalview.bin.Console.log
239 .setLevel((LogLevel) logLevelCombo.getSelectedItem());
246 frame.getContentPane().setLayout(new BorderLayout());
247 frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
248 JPanel southPanel = new JPanel();
249 southPanel.setLayout(new GridBagLayout());
251 JPanel logLevelPanel = new JPanel();
252 logLevelPanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
253 logLevelPanel.add(logLevelLabel);
254 logLevelPanel.add(logLevelCombo);
255 String logLevelTooltip = MessageManager.formatMessage(
256 "label.log_level_tooltip", startingLogLevel.toString());
257 logLevelLabel.setToolTipText(logLevelTooltip);
258 logLevelCombo.setToolTipText(logLevelTooltip);
260 GridBagConstraints gbc = new GridBagConstraints();
266 southPanel.add(logLevelPanel, gbc);
270 gbc.fill = GridBagConstraints.HORIZONTAL;
271 southPanel.add(clearButton, gbc);
275 gbc.fill = GridBagConstraints.NONE;
276 southPanel.add(copyToClipboardButton, gbc);
278 southPanel.setVisible(true);
279 frame.getContentPane().add(southPanel, BorderLayout.SOUTH);
280 frame.setVisible(visible);
281 updateConsole = visible;
282 frame.addWindowListener(this);
283 clearButton.addActionListener(this);
293 quit = false; // signals the Threads that they should exit
295 // Starting two seperate threads to read from the PipedInputStreams
297 reader = new Thread(this);
298 reader.setDaemon(true);
301 reader2 = new Thread(this);
302 reader2.setDaemon(true);
304 // and a thread to append text to the textarea
305 textAppender = new Thread(this);
306 textAppender.setDaemon(true);
307 textAppender.start();
310 frame.setIconImages(ChannelProperties.getIconList());
313 private void setChosenLogLevelCombo()
315 setChosenLogLevelCombo(startingLogLevel);
318 private void setChosenLogLevelCombo(LogLevel setLogLevel)
320 logLevelCombo.setSelectedItem(setLogLevel);
321 if (!logLevelCombo.getSelectedItem().equals(setLogLevel))
323 // setLogLevel not (yet) in list
324 if (setLogLevel != null && setLogLevel instanceof LogLevel)
326 // add new item to list (might be set via .jalview_properties)
327 boolean added = false;
328 for (int i = 0; i < logLevelCombo.getItemCount(); i++)
330 LogLevel l = logLevelCombo.getItemAt(i);
331 if (l.compareTo(setLogLevel) >= 0)
333 logLevelCombo.insertItemAt(setLogLevel, i);
338 if (!added) // lower priority than others or some confusion -- add to
341 logLevelCombo.addItem(setLogLevel);
343 logLevelCombo.setSelectedItem(setLogLevel);
347 logLevelCombo.setSelectedItem(LogLevel.INFO);
352 private void copyConsoleTextToClipboard()
354 String consoleText = textArea.getText();
355 StringSelection consoleTextSelection = new StringSelection(consoleText);
356 Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
357 cb.setContents(consoleTextSelection, null);
360 PipedOutputStream pout = null, perr = null;
362 public void redirectStreams()
368 pout = new PipedOutputStream(this.pin);
369 System.setOut(new PrintStream(pout, true));
370 } catch (java.io.IOException io)
372 textArea.append("Couldn't redirect STDOUT to this console\n"
374 io.printStackTrace(stderr);
375 } catch (SecurityException se)
377 textArea.append("Couldn't redirect STDOUT to this console\n"
379 se.printStackTrace(stderr);
384 perr = new PipedOutputStream(this.pin2);
385 System.setErr(new PrintStream(perr, true));
386 } catch (java.io.IOException io)
388 textArea.append("Couldn't redirect STDERR to this console\n"
390 io.printStackTrace(stderr);
391 } catch (SecurityException se)
393 textArea.append("Couldn't redirect STDERR to this console\n"
395 se.printStackTrace(stderr);
400 public void unredirectStreams()
406 System.setOut(stdout);
409 pin = new PipedInputStream();
411 } catch (java.io.IOException io)
413 textArea.append("Couldn't unredirect STDOUT to this console\n"
415 io.printStackTrace(stderr);
416 } catch (SecurityException se)
418 textArea.append("Couldn't unredirect STDOUT to this console\n"
420 se.printStackTrace(stderr);
425 System.setErr(stderr);
428 pin2 = new PipedInputStream();
430 } catch (java.io.IOException io)
432 textArea.append("Couldn't unredirect STDERR to this console\n"
434 io.printStackTrace(stderr);
435 } catch (SecurityException se)
437 textArea.append("Couldn't unredirect STDERR to this console\n"
439 se.printStackTrace(stderr);
447 // you may omit this part for your application
450 jalview.bin.Console.outPrintln("Hello World 2");
451 jalview.bin.Console.outPrintln("All fonts available to Graphic2D:\n");
452 GraphicsEnvironment ge = GraphicsEnvironment
453 .getLocalGraphicsEnvironment();
454 String[] fontNames = ge.getAvailableFontFamilyNames();
455 for (int n = 0; n < fontNames.length; n++)
457 jalview.bin.Console.outPrintln(fontNames[n]);
459 // Testing part: simple an error thrown anywhere in this JVM will be printed
461 // We do it with a seperate Thread becasue we don't wan't to break a Thread
462 // used by the Console.
463 jalview.bin.Console.outPrintln("\nLets throw an error on this console");
464 errorThrower = new Thread(this);
465 errorThrower.setDaemon(true);
466 errorThrower.start();
469 private JFrame initFrame(String string, int i, int j, int x, int y)
471 JFrame frame = new JFrame(string);
472 frame.setName(string);
481 frame.setBounds(x, y, i, j);
486 * attach a console to the desktop - the desktop will open it if requested.
490 public Console(Desktop desktop)
493 // window name - get x,y,width, height possibly scaled
494 Rectangle bounds = parent == null ? null
495 : parent.getLastKnownDimensions("JAVA_CONSOLE_");
499 ChannelProperties.getProperty("app_name") + " Java Console",
500 bounds.width, bounds.height, bounds.x, bounds.y);
502 else if (parent != null && parent.getWidth() > 0
503 && parent.getHeight() > 0)
506 ChannelProperties.getProperty("app_name") + " Java Console",
507 parent.getWidth() / 2, parent.getHeight() / 4, parent.getX(),
513 ChannelProperties.getProperty("app_name") + " Java Console",
514 MIN_WIDTH, MIN_HEIGHT, 10, 10);
516 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
517 // parent.add(frame);
519 LogLevel level = (LogLevel) logLevelCombo.getSelectedItem();
520 if (!Platform.isJS())
522 JalviewAppender jappender = new JalviewAppender(level);
523 JalviewAppender.setTextArea(textArea);
525 if (jalview.bin.Console.log != null
526 && jalview.bin.Console.log instanceof JLoggerLog4j)
528 JLoggerLog4j.addAppender(jalview.bin.Console.log, jappender);
533 public synchronized void stopConsole()
538 * reader.notify(); reader2.notify(); if (errorThrower!=null)
539 * errorThrower.notify(); // stop all threads if (textAppender!=null)
540 * textAppender.notify();
548 } catch (Exception e)
550 jalview.bin.Console.debug("pin.close() error", e);
556 } catch (Exception e)
558 jalview.bin.Console.debug("pin2.close() error", e);
562 textAppender.join(10);
563 } catch (Exception e)
565 jalview.bin.Console.debug("textAppender.join(10) error", e);
569 if (!frame.isVisible())
578 public synchronized void windowClosed(WindowEvent evt)
580 frame.setVisible(false);
584 private void closeConsoleGui()
586 updateConsole = false;
594 parent.showConsole(false);
599 public synchronized void windowClosing(WindowEvent evt)
601 frame.setVisible(false); // default behaviour of JFrame
608 public synchronized void actionPerformed(ActionEvent evt)
611 // textArea.setText("");
615 public synchronized void run()
619 while (Thread.currentThread() == reader)
621 if (pin == null || pin.available() == 0)
626 } catch (InterruptedException ie)
628 jalview.bin.Console.debug("pin.available() error", ie);
632 while (pin.available() != 0)
634 String input = this.readLine(pin);
636 long time = System.nanoTime();
637 appendToTextArea(input);
638 // stderr.println("Time taken to stdout append:\t"
639 // + (System.nanoTime() - time) + " ns");
648 while (Thread.currentThread() == reader2)
650 if (pin2.available() == 0)
654 this.wait(100); // ##### implicated BLOCKED
655 if (pin2.available() == 0)
659 } catch (InterruptedException ie)
661 jalview.bin.Console.debug("pin.available() error", ie);
664 while (pin2.available() != 0)
666 String input = this.readLine(pin2);
668 long time = System.nanoTime();
669 appendToTextArea(input);
670 // stderr.println("Time taken to stderr append:\t"
671 // + (System.nanoTime() - time) + " ns");
679 while (Thread.currentThread() == textAppender)
683 // check string buffer - if greater than console, clear console and
684 // replace with last segment of content, otherwise, append all to
686 while (displayPipe.length() > 0)
688 StringBuffer tmp = new StringBuffer(), replace;
689 synchronized (displayPipe)
691 replace = displayPipe;
694 // Append formatted message to textarea using the Swing Thread.
695 SwingUtilities.invokeLater(new Runnable()
699 textArea.append(replace.toString());
704 if (displayPipe.length() == 0)
709 if (displayPipe.length() == 0)
711 // post a trim on Swing Thread.
712 SwingUtilities.invokeLater(new Runnable()
720 } catch (InterruptedException e)
722 jalview.bin.Console.debug("displayPipe.length() error", e);
731 } catch (InterruptedException e)
733 jalview.bin.Console.debug("this.wait(100) error", e);
742 } catch (Exception e)
744 textArea.append("\nConsole reports an Internal error.");
745 textArea.append("The error is: " + e.getMessage());
746 // Need to uncomment this to ensure that line tally is synched.
749 "Console reports an Internal error.\nThe error is: " + e);
752 // just for testing (Throw a Nullpointer after 1 second)
753 if (Thread.currentThread() == errorThrower)
758 } catch (InterruptedException ie)
760 jalview.bin.Console.debug("this.wait(1000) error", ie);
762 throw new NullPointerException(
763 MessageManager.getString("exception.application_test_npe"));
767 private void appendToTextArea(final String input)
769 if (updateConsole == false)
774 long time = System.nanoTime();
775 javax.swing.SwingUtilities.invokeLater(new Runnable()
780 displayPipe.append(input);
783 // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() -
788 private String header = null;
790 private boolean updateConsole = false;
792 private synchronized void trimBuffer(boolean clear)
794 if (header == null && textArea.getLineCount() > 5)
798 header = textArea.getText(0, textArea.getLineStartOffset(5))
799 + "\nTruncated...\n";
800 } catch (Exception e)
802 jalview.bin.Console.warn("textArea Exception", e);
806 int tlength = textArea.getDocument().getLength();
809 if (clear || (tlength > byteslim))
815 long time = System.nanoTime();
816 textArea.replaceRange(header, 0, tlength - bytescut);
817 // stderr.println("Time taken to cut:\t"
818 // + (System.nanoTime() - time) + " ns");
822 textArea.setText(header);
824 } catch (Exception e)
826 jalview.bin.Console.warn("textArea Exception", e);
828 // lines = textArea.getLineCount();
834 public synchronized String readLine(PipedInputStream in)
841 int available = in.available();
846 byte b[] = new byte[available];
848 input = input + new String(b, 0, b.length);
849 // counts lines - we don't do this for speed.
850 // while ((lp = input.indexOf("\n", lp + 1)) > -1)
854 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
862 public static void main(String[] arg)
864 new Console().test(); // create console with not reference
868 public void setVisible(boolean selected)
870 frame.setVisible(selected);
871 if (selected == true)
873 setChosenLogLevelCombo();
875 updateConsole = true;
880 // reset log level to what it was before
881 if (jalview.bin.Console.log != null)
883 jalview.bin.Console.log.setLevel(startingLogLevel);
887 updateConsole = false;
891 public Rectangle getBounds()
895 return frame.getBounds();
901 * set the banner that appears at the top of the console output
905 public void setHeader(String string)
908 if (header.charAt(header.length() - 1) != '\n')
912 textArea.insert(header, 0);
920 public String getHeader()