minor fix for * JAL-537
[jalview.git] / src / jalview / gui / Console.java
index 57d563b..acd3dbd 100644 (file)
@@ -1,20 +1,19 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4.1)
- * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
+ * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
  * 
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * This file is part of Jalview.
  * 
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  * 
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
  */
 package jalview.gui;
 
@@ -26,13 +25,12 @@ import javax.swing.*;
 import org.apache.log4j.SimpleLayout;
 
 /**
- * Simple Jalview Java Console. Version 1 - allows viewing of console output after desktop is created.
- * Acquired with thanks from RJHM's site http://www.comweb.nl/java/Console/Console.html
- *  A simple Java Console for your application (Swing version)
- * Requires Java 1.1.5 or higher
- * Disclaimer the use of this source is at your own risk. 
- * Permision to use and distribute into your own applications
- * RJHM van den Bergh , rvdb@comweb.nl
+ * Simple Jalview Java Console. Version 1 - allows viewing of console output
+ * after desktop is created. Acquired with thanks from RJHM's site
+ * http://www.comweb.nl/java/Console/Console.html A simple Java Console for your
+ * application (Swing version) Requires Java 1.1.5 or higher Disclaimer the use
+ * of this source is at your own risk. Permision to use and distribute into your
+ * own applications RJHM van den Bergh , rvdb@comweb.nl
  */
 
 public class Console extends WindowAdapter implements WindowListener,
@@ -42,20 +40,29 @@ public class Console extends WindowAdapter implements WindowListener,
 
   private JTextArea textArea;
 
+  int lines = 0;
+
+  int lim = 1000;
+
   private Thread reader;
 
   private Thread reader2;
 
   private boolean quit;
 
+  private final PrintStream stdout = System.out;
+
+  private final PrintStream stderr = System.err;
+
   private final PipedInputStream pin = new PipedInputStream();
 
   private final PipedInputStream pin2 = new PipedInputStream();
 
-
   Thread errorThrower; // just for testing (Throws an Exception at this Console
+
   // are we attached to some parent Desktop
   Desktop parent = null;
+
   public Console()
   {
     // create all components and add them
@@ -68,7 +75,7 @@ public class Console extends WindowAdapter implements WindowListener,
   private void initConsole(boolean visible)
   {
     // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
-    //textArea = cpt.getTextArea();
+    // textArea = cpt.getTextArea();
     textArea = new JTextArea();
     textArea.setEditable(false);
     JButton button = new JButton("clear");
@@ -123,11 +130,13 @@ public class Console extends WindowAdapter implements WindowListener,
     reader2.setDaemon(true);
     reader2.start();
   }
-  public void test() {
+
+  public void test()
+  {
     // testing part
     // you may omit this part for your application
     // 
-    
+
     System.out.println("Hello World 2");
     System.out.println("All fonts available to Graphic2D:\n");
     GraphicsEnvironment ge = GraphicsEnvironment
@@ -149,9 +158,9 @@ public class Console extends WindowAdapter implements WindowListener,
   {
     JFrame frame = new JFrame(string);
     frame.setName(string);
-    if (x==-1)
+    if (x == -1)
       x = (int) (i / 2);
-    if (y==-1)
+    if (y == -1)
       y = (int) (j / 2);
     frame.setBounds(x, y, i, j);
     return frame;
@@ -167,18 +176,21 @@ public class Console extends WindowAdapter implements WindowListener,
     parent = desktop;
     // window name - get x,y,width, height possibly scaled
     Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
-    if (bounds==null)
+    if (bounds == null)
+    {
+      frame = initFrame("Jalview Java Console", desktop.getWidth() / 2,
+              desktop.getHeight() / 4, desktop.getX(), desktop.getY());
+    }
+    else
     {
-    frame = initFrame("Jalview Java Console", desktop.getWidth() / 2,
-            desktop.getHeight() / 4,desktop.getX(),desktop.getY());
-    } else {
-      frame = initFrame("Jalview Java Console", bounds.width, bounds.height, bounds.x, bounds.y);
+      frame = initFrame("Jalview Java Console", bounds.width,
+              bounds.height, bounds.x, bounds.y);
     }
     // desktop.add(frame);
     initConsole(false);
     JalviewAppender jappender = new JalviewAppender();
     jappender.setLayout(new SimpleLayout());
-    jappender.setTextArea(textArea);
+    JalviewAppender.setTextArea(textArea);
     org.apache.log4j.Logger.getRootLogger().addAppender(jappender);
   }
 
@@ -200,7 +212,7 @@ public class Console extends WindowAdapter implements WindowListener,
     } catch (Exception e)
     {
     }
-//    System.exit(0);
+    // System.exit(0);
   }
 
   public synchronized void windowClosed(WindowEvent evt)
@@ -210,7 +222,9 @@ public class Console extends WindowAdapter implements WindowListener,
     {
 
       stopConsole();
-    } else {
+    }
+    else
+    {
       parent.showConsole(false);
     }
   }
@@ -218,12 +232,13 @@ public class Console extends WindowAdapter implements WindowListener,
   public synchronized void windowClosing(WindowEvent evt)
   {
     frame.setVisible(false); // default behaviour of JFrame
-//    frame.dispose();
+    // frame.dispose();
   }
 
   public synchronized void actionPerformed(ActionEvent evt)
   {
-    textArea.setText("");
+    trimBuffer(true);
+    // textArea.setText("");
   }
 
   public synchronized void run()
@@ -232,41 +247,54 @@ public class Console extends WindowAdapter implements WindowListener,
     {
       while (Thread.currentThread() == reader)
       {
-        try
+        if (pin.available()==0) 
+          {try
         {
           this.wait(100);
         } catch (InterruptedException ie)
         {
-        }
-        if (pin.available() != 0)
+        }}
+        
+        while (pin.available() != 0)
         {
           String input = this.readLine(pin);
+          stdout.print(input);
           textArea.append(input);
+          // lines++;
         }
         if (quit)
           return;
+        trimBuffer(false);
       }
 
       while (Thread.currentThread() == reader2)
       {
-        try
+        if (pin2.available()==0) {
+          try
         {
           this.wait(100);
         } catch (InterruptedException ie)
         {
         }
-        if (pin2.available() != 0)
+        }
+        while (pin2.available() != 0)
         {
           String input = this.readLine(pin2);
+          stderr.print(input);
           textArea.append(input);
+          // lines++;
         }
         if (quit)
           return;
+        trimBuffer(false);
       }
     } catch (Exception e)
     {
       textArea.append("\nConsole reports an Internal error.");
-      textArea.append("The error is: " + e);
+      textArea.append("The error is: " + e.getMessage());
+      lines += 2;
+      stderr.println("Console reports an Internal error.\nThe error is: "
+              + e);
     }
 
     // just for testing (Throw a Nullpointer after 1 second)
@@ -281,6 +309,41 @@ public class Console extends WindowAdapter implements WindowListener,
       throw new NullPointerException(
               "Application test: throwing an NullPointerException It should arrive at the console");
     }
+  }
+
+  private synchronized void trimBuffer(boolean clear)
+  {
+    // trim the buffer
+    if (clear || lines > lim)
+    {
+      try
+      {
+        if (lines > 5)
+        {
+          // minimum length for truncation/clearing
+          String header = textArea.getText(0, textArea.getLineEndOffset(5))
+                  + "\n..Truncated..\n"; // keep first 5 lines for startup info
+          int truncate;
+          if (!clear)
+          {
+            truncate = textArea.getLineEndOffset(lines - lim - 7);
+            textArea.setText(header
+                    + textArea.getText(truncate, textArea.getText()
+                            .length()
+                            - truncate));
+          }
+          else
+          {
+            textArea.setText(header);
+          }
+        }
+
+      } catch (Exception e)
+      {
+        e.printStackTrace();
+      }
+      lines = textArea.getLineCount();
+    }
 
   }
 
@@ -288,6 +351,7 @@ public class Console extends WindowAdapter implements WindowListener,
           throws IOException
   {
     String input = "";
+    int lp = -1;
     do
     {
       int available = in.available();
@@ -296,6 +360,10 @@ public class Console extends WindowAdapter implements WindowListener,
       byte b[] = new byte[available];
       in.read(b);
       input = input + new String(b, 0, b.length);
+      while ((lp = input.indexOf("\n", lp + 1)) > -1)
+      {
+        lines++;
+      }
     } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
     return input;
   }
@@ -303,7 +371,7 @@ public class Console extends WindowAdapter implements WindowListener,
   public static void main(String[] arg)
   {
     new Console().test(); // create console with not reference
-    
+
   }
 
   public void setVisible(boolean selected)
@@ -312,12 +380,12 @@ public class Console extends WindowAdapter implements WindowListener,
     if (selected == true)
     {
       frame.toFront();
-    }    
+    }
   }
 
   public Rectangle getBounds()
   {
-    if (frame!=null)
+    if (frame != null)
     {
       return frame.getBounds();
     }