X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FConsole.java;h=41da01fc2da4b49da87360308c47e4b6034ae3db;hb=9d465da4f21e403c20b835264929906b538c0c9b;hp=cb53d1403366d5aa9a6da2714178336e84889f53;hpb=a1984b1c8c273ed33c7ce9283039f4027dcae2de;p=jalview.git diff --git a/src/jalview/gui/Console.java b/src/jalview/gui/Console.java index cb53d14..41da01f 100644 --- a/src/jalview/gui/Console.java +++ b/src/jalview/gui/Console.java @@ -50,10 +50,10 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; +import javax.swing.SwingUtilities; import javax.swing.border.Border; import javax.swing.text.DefaultCaret; -import jalview.bin.Cache; import jalview.log.JLoggerI.LogLevel; import jalview.log.JLoggerLog4j; import jalview.log.JalviewAppender; @@ -107,7 +107,7 @@ public class Console extends WindowAdapter private JComboBox logLevelCombo = new JComboBox(); - protected LogLevel startingLogLevel = LogLevel.INFO; + protected LogLevel startingLogLevel = null; public Console() { @@ -115,6 +115,7 @@ public class Console extends WindowAdapter Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame = initFrame("Java Console", screenSize.width / 2, screenSize.height / 2, -1, -1); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); initConsole(true); } @@ -147,6 +148,7 @@ public class Console extends WindowAdapter scrollPane.setBorder(noBorder); textArea.addMouseListener(new MouseAdapter() { + @Override public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1) @@ -172,6 +174,7 @@ public class Console extends WindowAdapter MessageManager.getString("label.copy_to_clipboard")); copyToClipboardButton.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { copyConsoleTextToClipboard(); @@ -183,12 +186,14 @@ public class Console extends WindowAdapter private Color fg = textArea.getForeground(); + @Override public void mousePressed(MouseEvent e) { textArea.setBackground(textArea.getSelectionColor()); textArea.setForeground(textArea.getSelectedTextColor()); } + @Override public void mouseReleased(MouseEvent e) { textArea.setBackground(bg); @@ -212,16 +217,25 @@ public class Console extends WindowAdapter // logLevelCombo.addItem(LogLevel.ERROR); // logLevelCombo.addItem(LogLevel.OFF); // set startingLogLevel - startingLogLevel = Cache.log == null ? LogLevel.INFO - : Cache.log.getLevel(); + + if (jalview.bin.Console.getLogger() == null) + { + startingLogLevel = jalview.bin.Console.getCachedLogLevel(); + } + else + { + startingLogLevel = jalview.bin.Console.getLogger().getLevel(); + } setChosenLogLevelCombo(); logLevelCombo.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { - if (Cache.log != null) + if (jalview.bin.Console.log != null) { - Cache.log.setLevel((LogLevel) logLevelCombo.getSelectedItem()); + jalview.bin.Console.log + .setLevel((LogLevel) logLevelCombo.getSelectedItem()); } } @@ -312,7 +326,7 @@ public class Console extends WindowAdapter boolean added = false; for (int i = 0; i < logLevelCombo.getItemCount(); i++) { - LogLevel l = (LogLevel) logLevelCombo.getItemAt(i); + LogLevel l = logLevelCombo.getItemAt(i); if (l.compareTo(setLogLevel) >= 0) { logLevelCombo.insertItemAt(setLogLevel, i); @@ -432,20 +446,20 @@ public class Console extends WindowAdapter // you may omit this part for your application // - System.out.println("Hello World 2"); - System.out.println("All fonts available to Graphic2D:\n"); + jalview.bin.Console.outPrintln("Hello World 2"); + jalview.bin.Console.outPrintln("All fonts available to Graphic2D:\n"); GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); String[] fontNames = ge.getAvailableFontFamilyNames(); for (int n = 0; n < fontNames.length; n++) { - System.out.println(fontNames[n]); + jalview.bin.Console.outPrintln(fontNames[n]); } // Testing part: simple an error thrown anywhere in this JVM will be printed // on the Console // We do it with a seperate Thread becasue we don't wan't to break a Thread // used by the Console. - System.out.println("\nLets throw an error on this console"); + jalview.bin.Console.outPrintln("\nLets throw an error on this console"); errorThrower = new Thread(this); errorThrower.setDaemon(true); errorThrower.start(); @@ -474,36 +488,32 @@ public class Console extends WindowAdapter */ public Console(Desktop desktop) { - this(desktop, true); - } - - /** - * attach a console to the desktop - the desktop will open it if requested. - * - * @param desktop - * @param showjconsole - * - if true, then redirect stdout immediately - */ - public Console(Desktop desktop, boolean showjconsole) - { parent = desktop; // window name - get x,y,width, height possibly scaled - Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_"); - if (bounds == null) + Rectangle bounds = parent == null ? null + : parent.getLastKnownDimensions("JAVA_CONSOLE_"); + if (bounds != null) { frame = initFrame( ChannelProperties.getProperty("app_name") + " Java Console", - desktop.getWidth() / 2, desktop.getHeight() / 4, - desktop.getX(), desktop.getY()); + bounds.width, bounds.height, bounds.x, bounds.y); + } + else if (parent != null && parent.getWidth() > 0 + && parent.getHeight() > 0) + { + frame = initFrame( + ChannelProperties.getProperty("app_name") + " Java Console", + parent.getWidth() / 2, parent.getHeight() / 4, parent.getX(), + parent.getY()); } else { frame = initFrame( ChannelProperties.getProperty("app_name") + " Java Console", - bounds.width, bounds.height, bounds.x, bounds.y); + MIN_WIDTH, MIN_HEIGHT, 10, 10); } frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT)); - // desktop.add(frame); + // parent.add(frame); initConsole(false); LogLevel level = (LogLevel) logLevelCombo.getSelectedItem(); if (!Platform.isJS()) @@ -511,9 +521,10 @@ public class Console extends WindowAdapter JalviewAppender jappender = new JalviewAppender(level); JalviewAppender.setTextArea(textArea); jappender.start(); - if (Cache.log != null && Cache.log instanceof JLoggerLog4j) + if (jalview.bin.Console.log != null + && jalview.bin.Console.log instanceof JLoggerLog4j) { - JLoggerLog4j.addAppender(Cache.log, jappender); + JLoggerLog4j.addAppender(jalview.bin.Console.log, jappender); } } } @@ -535,6 +546,7 @@ public class Console extends WindowAdapter pin.close(); } catch (Exception e) { + jalview.bin.Console.debug("pin.close() error", e); } try { @@ -542,18 +554,22 @@ public class Console extends WindowAdapter pin2.close(); } catch (Exception e) { + jalview.bin.Console.debug("pin2.close() error", e); } try { textAppender.join(10); } catch (Exception e) { + jalview.bin.Console.debug("textAppender.join(10) error", e); } } + /* if (!frame.isVisible()) { frame.dispose(); } + */ // System.exit(0); } @@ -606,12 +622,9 @@ public class Console extends WindowAdapter try { this.wait(100); - if (pin.available() == 0) - { - trimBuffer(false); - } } catch (InterruptedException ie) { + jalview.bin.Console.debug("pin.available() error", ie); } } @@ -637,13 +650,14 @@ public class Console extends WindowAdapter { try { - this.wait(100); + this.wait(100); // ##### implicated BLOCKED if (pin2.available() == 0) { trimBuffer(false); } } catch (InterruptedException ie) { + jalview.bin.Console.debug("pin.available() error", ie); } } while (pin2.available() != 0) @@ -668,23 +682,23 @@ public class Console extends WindowAdapter // check string buffer - if greater than console, clear console and // replace with last segment of content, otherwise, append all to // content. - long count; while (displayPipe.length() > 0) { - count = 0; StringBuffer tmp = new StringBuffer(), replace; synchronized (displayPipe) { replace = displayPipe; displayPipe = tmp; } - // simply append whole buffer - textArea.append(replace.toString()); - count += replace.length(); - if (count > byteslim) + // Append formatted message to textarea using the Swing Thread. + SwingUtilities.invokeLater(new Runnable() { - trimBuffer(false); - } + public void run() + { + textArea.append(replace.toString()); + trimBuffer(false); + } + }); } if (displayPipe.length() == 0) { @@ -693,10 +707,18 @@ public class Console extends WindowAdapter this.wait(100); if (displayPipe.length() == 0) { - trimBuffer(false); + // post a trim on Swing Thread. + SwingUtilities.invokeLater(new Runnable() + { + public void run() + { + trimBuffer(false); + } + }); } } catch (InterruptedException e) { + jalview.bin.Console.debug("displayPipe.length() error", e); } } } @@ -707,7 +729,7 @@ public class Console extends WindowAdapter this.wait(100); } catch (InterruptedException e) { - + jalview.bin.Console.debug("this.wait(100) error", e); } } if (quit) @@ -734,6 +756,7 @@ public class Console extends WindowAdapter this.wait(1000); } catch (InterruptedException ie) { + jalview.bin.Console.debug("this.wait(1000) error", ie); } throw new NullPointerException( MessageManager.getString("exception.application_test_npe")); @@ -753,9 +776,7 @@ public class Console extends WindowAdapter @Override public void run() { - displayPipe.append(input); // change to stringBuffer - // displayPipe.flush(); - + displayPipe.append(input); } }); // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() - @@ -777,7 +798,7 @@ public class Console extends WindowAdapter + "\nTruncated...\n"; } catch (Exception e) { - e.printStackTrace(); + jalview.bin.Console.warn("textArea Exception", e); } } // trim the buffer @@ -801,7 +822,7 @@ public class Console extends WindowAdapter } } catch (Exception e) { - e.printStackTrace(); + jalview.bin.Console.warn("textArea Exception", e); } // lines = textArea.getLineCount(); } @@ -856,9 +877,9 @@ public class Console extends WindowAdapter else { // reset log level to what it was before - if (Cache.log != null) + if (jalview.bin.Console.log != null) { - Cache.log.setLevel(startingLogLevel); + jalview.bin.Console.log.setLevel(startingLogLevel); } unredirectStreams();