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.logging.log4j.Level;
55 import jalview.bin.Cache;
56 import jalview.util.ChannelProperties;
57 import jalview.util.Log4j;
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 Log4j.setLevel(Cache.log,
190 (Level) logLevelCombo.getSelectedItem());
197 frame.getContentPane().setLayout(new BorderLayout());
198 frame.getContentPane().add(new JScrollPane(textArea),
199 BorderLayout.CENTER);
200 JPanel southPanel = new JPanel();
201 southPanel.setLayout(new GridBagLayout());
203 JPanel logLevelPanel = new JPanel();
204 logLevelPanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
205 logLevelPanel.add(logLevelLabel);
206 logLevelPanel.add(logLevelCombo);
207 String logLevelTooltip = MessageManager.formatMessage(
208 "label.log_level_tooltip", startingLogLevel.toString());
209 logLevelLabel.setToolTipText(logLevelTooltip);
210 logLevelCombo.setToolTipText(logLevelTooltip);
212 GridBagConstraints gbc = new GridBagConstraints();
218 southPanel.add(logLevelPanel, gbc);
222 gbc.fill = GridBagConstraints.HORIZONTAL;
223 southPanel.add(clearButton, gbc);
227 gbc.fill = GridBagConstraints.NONE;
228 southPanel.add(copyToClipboardButton, gbc);
230 southPanel.setVisible(true);
231 frame.getContentPane().add(southPanel, BorderLayout.SOUTH);
232 frame.setVisible(visible);
233 updateConsole = visible;
234 frame.addWindowListener(this);
235 clearButton.addActionListener(this);
245 quit = false; // signals the Threads that they should exit
247 // Starting two seperate threads to read from the PipedInputStreams
249 reader = new Thread(this);
250 reader.setDaemon(true);
253 reader2 = new Thread(this);
254 reader2.setDaemon(true);
256 // and a thread to append text to the textarea
257 textAppender = new Thread(this);
258 textAppender.setDaemon(true);
259 textAppender.start();
262 frame.setIconImages(ChannelProperties.getIconList());
265 private void setChosenLogLevelCombo()
267 setChosenLogLevelCombo(startingLogLevel);
270 private void setChosenLogLevelCombo(Level setLogLevel)
272 logLevelCombo.setSelectedItem(setLogLevel);
273 if (!logLevelCombo.getSelectedItem().equals(setLogLevel))
275 // setLogLevel not (yet) in list
276 if (setLogLevel != null && setLogLevel instanceof Level)
278 // add new item to list (might be set via .jalview_properties)
279 boolean added = false;
280 for (int i = 0; i < logLevelCombo.getItemCount(); i++)
282 Level l = (Level) logLevelCombo.getItemAt(i);
283 if (l.isMoreSpecificThan(setLogLevel))
285 logLevelCombo.insertItemAt(setLogLevel, i);
290 if (!added) // lower priority than others or some confusion -- add to
293 logLevelCombo.addItem(setLogLevel);
295 logLevelCombo.setSelectedItem(setLogLevel);
299 logLevelCombo.setSelectedItem(Level.INFO);
304 private void copyConsoleTextToClipboard()
306 String consoleText = textArea.getText();
307 StringSelection consoleTextSelection = new StringSelection(consoleText);
308 Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
309 cb.setContents(consoleTextSelection, null);
312 PipedOutputStream pout = null, perr = null;
314 public void redirectStreams()
320 pout = new PipedOutputStream(this.pin);
321 System.setOut(new PrintStream(pout, true));
322 } catch (java.io.IOException io)
324 textArea.append("Couldn't redirect STDOUT to this console\n"
326 io.printStackTrace(stderr);
327 } catch (SecurityException se)
329 textArea.append("Couldn't redirect STDOUT to this console\n"
331 se.printStackTrace(stderr);
336 perr = new PipedOutputStream(this.pin2);
337 System.setErr(new PrintStream(perr, true));
338 } catch (java.io.IOException io)
340 textArea.append("Couldn't redirect STDERR to this console\n"
342 io.printStackTrace(stderr);
343 } catch (SecurityException se)
345 textArea.append("Couldn't redirect STDERR to this console\n"
347 se.printStackTrace(stderr);
352 public void unredirectStreams()
358 System.setOut(stdout);
361 pin = new PipedInputStream();
363 } catch (java.io.IOException io)
365 textArea.append("Couldn't unredirect STDOUT to this console\n"
367 io.printStackTrace(stderr);
368 } catch (SecurityException se)
370 textArea.append("Couldn't unredirect STDOUT to this console\n"
372 se.printStackTrace(stderr);
377 System.setErr(stderr);
380 pin2 = new PipedInputStream();
382 } catch (java.io.IOException io)
384 textArea.append("Couldn't unredirect STDERR to this console\n"
386 io.printStackTrace(stderr);
387 } catch (SecurityException se)
389 textArea.append("Couldn't unredirect STDERR to this console\n"
391 se.printStackTrace(stderr);
399 // you may omit this part for your application
402 System.out.println("Hello World 2");
403 System.out.println("All fonts available to Graphic2D:\n");
404 GraphicsEnvironment ge = GraphicsEnvironment
405 .getLocalGraphicsEnvironment();
406 String[] fontNames = ge.getAvailableFontFamilyNames();
407 for (int n = 0; n < fontNames.length; n++)
409 System.out.println(fontNames[n]);
411 // Testing part: simple an error thrown anywhere in this JVM will be printed
413 // We do it with a seperate Thread becasue we don't wan't to break a Thread
414 // used by the Console.
415 System.out.println("\nLets throw an error on this console");
416 errorThrower = new Thread(this);
417 errorThrower.setDaemon(true);
418 errorThrower.start();
421 private JFrame initFrame(String string, int i, int j, int x, int y)
423 JFrame frame = new JFrame(string);
424 frame.setName(string);
433 frame.setBounds(x, y, i, j);
438 * attach a console to the desktop - the desktop will open it if requested.
442 public Console(Desktop desktop)
448 * attach a console to the desktop - the desktop will open it if requested.
451 * @param showjconsole
452 * - if true, then redirect stdout immediately
454 public Console(Desktop desktop, boolean showjconsole)
457 // window name - get x,y,width, height possibly scaled
458 Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
462 ChannelProperties.getProperty("app_name") + " Java Console",
463 desktop.getWidth() / 2, desktop.getHeight() / 4,
464 desktop.getX(), desktop.getY());
469 ChannelProperties.getProperty("app_name") + " Java Console",
470 bounds.width, bounds.height, bounds.x, bounds.y);
472 frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
473 // desktop.add(frame);
475 JalviewAppender jappender = new JalviewAppender();
476 JalviewAppender.setTextArea(textArea);
477 Log4j.addAppenderToRootLogger(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 Log4j.setLevel(Cache.log, 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()