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.border.Border;
54 import javax.swing.text.DefaultCaret;
56 import jalview.log.JLoggerI.LogLevel;
57 import jalview.log.JLoggerLog4j;
58 import jalview.log.JalviewAppender;
59 import jalview.util.ChannelProperties;
60 import jalview.util.MessageManager;
61 import jalview.util.Platform;
64 * Simple Jalview Java Console. Version 1 - allows viewing of console output
65 * after desktop is created. Acquired with thanks from RJHM's site
66 * http://www.comweb.nl/java/Console/Console.html A simple Java Console for your
67 * application (Swing version) Requires Java 1.1.5 or higher Disclaimer the use
68 * of this source is at your own risk. Permision to use and distribute into your
69 * own applications RJHM van den Bergh , rvdb@comweb.nl
72 public class Console extends WindowAdapter
73 implements WindowListener, ActionListener, Runnable
77 private JTextArea textArea;
80 * unused - tally and limit for lines in console window int lines = 0;
84 int byteslim = 102400, bytescut = 76800; // 100k and 75k cut point.
86 private Thread reader, reader2, textAppender;
90 private final PrintStream stdout = System.out, stderr = System.err;
92 private PipedInputStream pin = new PipedInputStream();
94 private PipedInputStream pin2 = new PipedInputStream();
96 private StringBuffer displayPipe = new StringBuffer();
98 Thread errorThrower; // just for testing (Throws an Exception at this Console
100 // are we attached to some parent Desktop
101 Desktop parent = null;
103 private int MIN_WIDTH = 300;
105 private int MIN_HEIGHT = 250;
107 private JComboBox<LogLevel> logLevelCombo = new JComboBox<LogLevel>();
109 protected LogLevel startingLogLevel = LogLevel.INFO;
113 // create all components and add them
114 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
115 frame = initFrame("Java Console", screenSize.width / 2,
116 screenSize.height / 2, -1, -1);
117 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
121 private void initConsole(boolean visible)
123 initConsole(visible, true);
133 private void initConsole(boolean visible, boolean redirect)
135 // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
136 // textArea = cpt.getTextArea();
137 textArea = new JTextArea();
138 textArea.setEditable(false);
140 DefaultCaret caret = (DefaultCaret) textArea.getCaret();
141 caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
142 // toggle autoscroll by clicking on the text area
143 Border pausedBorder = BorderFactory.createMatteBorder(2, 2, 2, 2,
144 textArea.getForeground());
145 Border noBorder = BorderFactory.createEmptyBorder(2, 2, 2, 2);
146 JScrollPane scrollPane = new JScrollPane(textArea);
147 scrollPane.setBorder(noBorder);
148 textArea.addMouseListener(new MouseAdapter()
150 public void mouseClicked(MouseEvent e)
152 if (e.getButton() == MouseEvent.BUTTON1)
154 if (caret.getUpdatePolicy() == DefaultCaret.ALWAYS_UPDATE)
156 caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
157 scrollPane.setBorder(pausedBorder);
161 caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
162 textArea.setCaretPosition(textArea.getDocument().getLength());
163 scrollPane.setBorder(noBorder);
169 JButton clearButton = new JButton(
170 MessageManager.getString("action.clear"));
171 JButton copyToClipboardButton = new JButton(
172 MessageManager.getString("label.copy_to_clipboard"));
173 copyToClipboardButton.addActionListener(new ActionListener()
175 public void actionPerformed(ActionEvent e)
177 copyConsoleTextToClipboard();
180 copyToClipboardButton.addMouseListener(new MouseAdapter()
182 private Color bg = textArea.getBackground();
184 private Color fg = textArea.getForeground();
186 public void mousePressed(MouseEvent e)
188 textArea.setBackground(textArea.getSelectionColor());
189 textArea.setForeground(textArea.getSelectedTextColor());
192 public void mouseReleased(MouseEvent e)
194 textArea.setBackground(bg);
195 textArea.setForeground(fg);
199 copyToClipboardButton.setToolTipText(
200 MessageManager.getString("label.copy_to_clipboard_tooltip"));
202 JLabel logLevelLabel = new JLabel(
203 MessageManager.getString("label.log_level") + ":");
205 // logLevelCombo.addItem(LogLevel.ALL);
206 logLevelCombo.addItem(LogLevel.TRACE);
207 logLevelCombo.addItem(LogLevel.DEBUG);
208 logLevelCombo.addItem(LogLevel.INFO);
209 logLevelCombo.addItem(LogLevel.WARN);
210 // logLevelCombo.addItem(LogLevel.ERROR);
211 // logLevelCombo.addItem(LogLevel.FATAL);
212 // logLevelCombo.addItem(LogLevel.ERROR);
213 // logLevelCombo.addItem(LogLevel.OFF);
214 // set startingLogLevel
215 startingLogLevel = jalview.bin.Console.log == null ? LogLevel.INFO
216 : jalview.bin.Console.log.getLevel();
217 setChosenLogLevelCombo();
218 logLevelCombo.addActionListener(new ActionListener()
220 public void actionPerformed(ActionEvent e)
222 if (jalview.bin.Console.log != null)
224 jalview.bin.Console.log
225 .setLevel((LogLevel) logLevelCombo.getSelectedItem());
232 frame.getContentPane().setLayout(new BorderLayout());
233 frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
234 JPanel southPanel = new JPanel();
235 southPanel.setLayout(new GridBagLayout());
237 JPanel logLevelPanel = new JPanel();
238 logLevelPanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
239 logLevelPanel.add(logLevelLabel);
240 logLevelPanel.add(logLevelCombo);
241 String logLevelTooltip = MessageManager.formatMessage(
242 "label.log_level_tooltip", startingLogLevel.toString());
243 logLevelLabel.setToolTipText(logLevelTooltip);
244 logLevelCombo.setToolTipText(logLevelTooltip);
246 GridBagConstraints gbc = new GridBagConstraints();
252 southPanel.add(logLevelPanel, gbc);
256 gbc.fill = GridBagConstraints.HORIZONTAL;
257 southPanel.add(clearButton, gbc);
261 gbc.fill = GridBagConstraints.NONE;
262 southPanel.add(copyToClipboardButton, gbc);
264 southPanel.setVisible(true);
265 frame.getContentPane().add(southPanel, BorderLayout.SOUTH);
266 frame.setVisible(visible);
267 updateConsole = visible;
268 frame.addWindowListener(this);
269 clearButton.addActionListener(this);
279 quit = false; // signals the Threads that they should exit
281 // Starting two seperate threads to read from the PipedInputStreams
283 reader = new Thread(this);
284 reader.setDaemon(true);
287 reader2 = new Thread(this);
288 reader2.setDaemon(true);
290 // and a thread to append text to the textarea
291 textAppender = new Thread(this);
292 textAppender.setDaemon(true);
293 textAppender.start();
296 frame.setIconImages(ChannelProperties.getIconList());
299 private void setChosenLogLevelCombo()
301 setChosenLogLevelCombo(startingLogLevel);
304 private void setChosenLogLevelCombo(LogLevel setLogLevel)
306 logLevelCombo.setSelectedItem(setLogLevel);
307 if (!logLevelCombo.getSelectedItem().equals(setLogLevel))
309 // setLogLevel not (yet) in list
310 if (setLogLevel != null && setLogLevel instanceof LogLevel)
312 // add new item to list (might be set via .jalview_properties)
313 boolean added = false;
314 for (int i = 0; i < logLevelCombo.getItemCount(); i++)
316 LogLevel l = (LogLevel) logLevelCombo.getItemAt(i);
317 if (l.compareTo(setLogLevel) >= 0)
319 logLevelCombo.insertItemAt(setLogLevel, i);
324 if (!added) // lower priority than others or some confusion -- add to
327 logLevelCombo.addItem(setLogLevel);
329 logLevelCombo.setSelectedItem(setLogLevel);
333 logLevelCombo.setSelectedItem(LogLevel.INFO);
338 private void copyConsoleTextToClipboard()
340 String consoleText = textArea.getText();
341 StringSelection consoleTextSelection = new StringSelection(consoleText);
342 Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
343 cb.setContents(consoleTextSelection, null);
346 PipedOutputStream pout = null, perr = null;
348 public void redirectStreams()
354 pout = new PipedOutputStream(this.pin);
355 System.setOut(new PrintStream(pout, true));
356 } catch (java.io.IOException io)
358 textArea.append("Couldn't redirect STDOUT to this console\n"
360 io.printStackTrace(stderr);
361 } catch (SecurityException se)
363 textArea.append("Couldn't redirect STDOUT to this console\n"
365 se.printStackTrace(stderr);
370 perr = new PipedOutputStream(this.pin2);
371 System.setErr(new PrintStream(perr, true));
372 } catch (java.io.IOException io)
374 textArea.append("Couldn't redirect STDERR to this console\n"
376 io.printStackTrace(stderr);
377 } catch (SecurityException se)
379 textArea.append("Couldn't redirect STDERR to this console\n"
381 se.printStackTrace(stderr);
386 public void unredirectStreams()
392 System.setOut(stdout);
395 pin = new PipedInputStream();
397 } catch (java.io.IOException io)
399 textArea.append("Couldn't unredirect STDOUT to this console\n"
401 io.printStackTrace(stderr);
402 } catch (SecurityException se)
404 textArea.append("Couldn't unredirect STDOUT to this console\n"
406 se.printStackTrace(stderr);
411 System.setErr(stderr);
414 pin2 = new PipedInputStream();
416 } catch (java.io.IOException io)
418 textArea.append("Couldn't unredirect STDERR to this console\n"
420 io.printStackTrace(stderr);
421 } catch (SecurityException se)
423 textArea.append("Couldn't unredirect STDERR to this console\n"
425 se.printStackTrace(stderr);
433 // you may omit this part for your application
436 System.out.println("Hello World 2");
437 System.out.println("All fonts available to Graphic2D:\n");
438 GraphicsEnvironment ge = GraphicsEnvironment
439 .getLocalGraphicsEnvironment();
440 String[] fontNames = ge.getAvailableFontFamilyNames();
441 for (int n = 0; n < fontNames.length; n++)
443 System.out.println(fontNames[n]);
445 // Testing part: simple an error thrown anywhere in this JVM will be printed
447 // We do it with a seperate Thread becasue we don't wan't to break a Thread
448 // used by the Console.
449 System.out.println("\nLets throw an error on this console");
450 errorThrower = new Thread(this);
451 errorThrower.setDaemon(true);
452 errorThrower.start();
455 private JFrame initFrame(String string, int i, int j, int x, int y)
457 JFrame frame = new JFrame(string);
458 frame.setName(string);
467 frame.setBounds(x, y, i, j);
472 * attach a console to the desktop - the desktop will open it if requested.
476 public Console(Desktop desktop)
482 * attach a console to the desktop - the desktop will open it if requested.
485 * @param showjconsole
486 * - if true, then redirect stdout immediately
488 public Console(Desktop desktop, boolean showjconsole)
491 // window name - get x,y,width, height possibly scaled
492 Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
496 ChannelProperties.getProperty("app_name") + " Java Console",
497 desktop.getWidth() / 2, desktop.getHeight() / 4,
498 desktop.getX(), desktop.getY());
503 ChannelProperties.getProperty("app_name") + " Java Console",
504 bounds.width, bounds.height, bounds.x, bounds.y);
506 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
507 // desktop.add(frame);
509 LogLevel level = (LogLevel) logLevelCombo.getSelectedItem();
510 if (!Platform.isJS())
512 JalviewAppender jappender = new JalviewAppender(level);
513 JalviewAppender.setTextArea(textArea);
515 if (jalview.bin.Console.log != null
516 && jalview.bin.Console.log instanceof JLoggerLog4j)
518 JLoggerLog4j.addAppender(jalview.bin.Console.log, jappender);
523 public synchronized void stopConsole()
528 * reader.notify(); reader2.notify(); if (errorThrower!=null)
529 * errorThrower.notify(); // stop all threads if (textAppender!=null)
530 * textAppender.notify();
538 } catch (Exception e)
545 } catch (Exception e)
550 textAppender.join(10);
551 } catch (Exception e)
556 if (!frame.isVisible())
565 public synchronized void windowClosed(WindowEvent evt)
567 frame.setVisible(false);
571 private void closeConsoleGui()
573 updateConsole = false;
581 parent.showConsole(false);
586 public synchronized void windowClosing(WindowEvent evt)
588 frame.setVisible(false); // default behaviour of JFrame
595 public synchronized void actionPerformed(ActionEvent evt)
598 // textArea.setText("");
602 public synchronized void run()
606 while (Thread.currentThread() == reader)
608 if (pin == null || pin.available() == 0)
613 if (pin.available() == 0)
617 } catch (InterruptedException ie)
622 while (pin.available() != 0)
624 String input = this.readLine(pin);
626 long time = System.nanoTime();
627 appendToTextArea(input);
628 // stderr.println("Time taken to stdout append:\t"
629 // + (System.nanoTime() - time) + " ns");
638 while (Thread.currentThread() == reader2)
640 if (pin2.available() == 0)
645 if (pin2.available() == 0)
649 } catch (InterruptedException ie)
653 while (pin2.available() != 0)
655 String input = this.readLine(pin2);
657 long time = System.nanoTime();
658 appendToTextArea(input);
659 // stderr.println("Time taken to stderr append:\t"
660 // + (System.nanoTime() - time) + " ns");
668 while (Thread.currentThread() == textAppender)
672 // check string buffer - if greater than console, clear console and
673 // replace with last segment of content, otherwise, append all to
676 while (displayPipe.length() > 0)
679 StringBuffer tmp = new StringBuffer(), replace;
680 synchronized (displayPipe)
682 replace = displayPipe;
685 // simply append whole buffer
686 textArea.append(replace.toString());
687 count += replace.length();
688 if (count > byteslim)
693 if (displayPipe.length() == 0)
698 if (displayPipe.length() == 0)
702 } catch (InterruptedException e)
712 } catch (InterruptedException e)
723 } catch (Exception e)
725 textArea.append("\nConsole reports an Internal error.");
726 textArea.append("The error is: " + e.getMessage());
727 // Need to uncomment this to ensure that line tally is synched.
730 "Console reports an Internal error.\nThe error is: " + e);
733 // just for testing (Throw a Nullpointer after 1 second)
734 if (Thread.currentThread() == errorThrower)
739 } catch (InterruptedException ie)
742 throw new NullPointerException(
743 MessageManager.getString("exception.application_test_npe"));
747 private void appendToTextArea(final String input)
749 if (updateConsole == false)
754 long time = System.nanoTime();
755 javax.swing.SwingUtilities.invokeLater(new Runnable()
760 displayPipe.append(input); // change to stringBuffer
761 // displayPipe.flush();
765 // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() -
770 private String header = null;
772 private boolean updateConsole = false;
774 private synchronized void trimBuffer(boolean clear)
776 if (header == null && textArea.getLineCount() > 5)
780 header = textArea.getText(0, textArea.getLineStartOffset(5))
781 + "\nTruncated...\n";
782 } catch (Exception e)
788 int tlength = textArea.getDocument().getLength();
791 if (clear || (tlength > byteslim))
797 long time = System.nanoTime();
798 textArea.replaceRange(header, 0, tlength - bytescut);
799 // stderr.println("Time taken to cut:\t"
800 // + (System.nanoTime() - time) + " ns");
804 textArea.setText(header);
806 } catch (Exception e)
810 // lines = textArea.getLineCount();
816 public synchronized String readLine(PipedInputStream in)
823 int available = in.available();
828 byte b[] = new byte[available];
830 input = input + new String(b, 0, b.length);
831 // counts lines - we don't do this for speed.
832 // while ((lp = input.indexOf("\n", lp + 1)) > -1)
836 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
844 public static void main(String[] arg)
846 new Console().test(); // create console with not reference
850 public void setVisible(boolean selected)
852 frame.setVisible(selected);
853 if (selected == true)
855 setChosenLogLevelCombo();
857 updateConsole = true;
862 // reset log level to what it was before
863 if (jalview.bin.Console.log != null)
865 jalview.bin.Console.log.setLevel(startingLogLevel);
869 updateConsole = false;
873 public Rectangle getBounds()
877 return frame.getBounds();
883 * set the banner that appears at the top of the console output
887 public void setHeader(String string)
890 if (header.charAt(header.length() - 1) != '\n')
894 textArea.insert(header, 0);
902 public String getHeader()