JAL-4298 try much harder to avoid modifying console gui anywhere outside swing worker...
[jalview.git] / src / jalview / gui / Console.java
index cb53d14..89380a3 100644 (file)
@@ -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;
@@ -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);
   }
 
@@ -212,16 +213,17 @@ public class Console extends WindowAdapter
     // logLevelCombo.addItem(LogLevel.ERROR);
     // logLevelCombo.addItem(LogLevel.OFF);
     // set startingLogLevel
-    startingLogLevel = Cache.log == null ? LogLevel.INFO
-            : Cache.log.getLevel();
+    startingLogLevel = jalview.bin.Console.log == null ? LogLevel.INFO
+            : jalview.bin.Console.log.getLevel();
     setChosenLogLevelCombo();
     logLevelCombo.addActionListener(new ActionListener()
     {
       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());
         }
       }
 
@@ -432,20 +434,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();
@@ -511,9 +513,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);
       }
     }
   }
@@ -550,10 +553,12 @@ public class Console extends WindowAdapter
       {
       }
     }
+    /*
     if (!frame.isVisible())
     {
       frame.dispose();
     }
+    */
     // System.exit(0);
   }
 
@@ -606,10 +611,10 @@ public class Console extends WindowAdapter
           try
           {
             this.wait(100);
-            if (pin.available() == 0)
-            {
-              trimBuffer(false);
-            }
+//            if (pin.available() == 0)
+//            {
+//              trimBuffer(false);
+//            }
           } catch (InterruptedException ie)
           {
           }
@@ -668,23 +673,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,7 +698,14 @@ 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)
             {
@@ -753,9 +765,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() -
@@ -856,9 +866,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();