From 587aceacc56c474f2769ee2cf03d059de4a87ef3 Mon Sep 17 00:00:00 2001 From: jprocter Date: Fri, 2 Apr 2010 09:38:52 +0000 Subject: [PATCH] better truncation mechanism --- src/jalview/gui/Console.java | 125 +++++++++++++++++++++++++++++------------- 1 file changed, 86 insertions(+), 39 deletions(-) diff --git a/src/jalview/gui/Console.java b/src/jalview/gui/Console.java index 39b0ab3..ad186ec 100644 --- a/src/jalview/gui/Console.java +++ b/src/jalview/gui/Console.java @@ -26,13 +26,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,21 +41,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 @@ -69,7 +76,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"); @@ -124,11 +131,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 @@ -150,9 +159,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; @@ -168,12 +177,15 @@ 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", bounds.width, bounds.height, bounds.x, bounds.y); + 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); } // desktop.add(frame); initConsole(false); @@ -201,7 +213,7 @@ public class Console extends WindowAdapter implements WindowListener, } catch (Exception e) { } -// System.exit(0); + // System.exit(0); } public synchronized void windowClosed(WindowEvent evt) @@ -211,7 +223,9 @@ public class Console extends WindowAdapter implements WindowListener, { stopConsole(); - } else { + } + else + { parent.showConsole(false); } } @@ -219,14 +233,15 @@ 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(""); } - int lines=0,lim=1000; + public synchronized void run() { try @@ -239,15 +254,16 @@ public class Console extends WindowAdapter implements WindowListener, } catch (InterruptedException ie) { } - if (pin.available() != 0) + while (pin.available() != 0) { String input = this.readLine(pin); stdout.print(input); textArea.append(input); - lines++; + // lines++; } if (quit) return; + trimBuffer(false); } while (Thread.currentThread() == reader2) @@ -258,21 +274,24 @@ public class Console extends WindowAdapter implements WindowListener, } catch (InterruptedException ie) { } - if (pin2.available() != 0) + while (pin2.available() != 0) { String input = this.readLine(pin2); stderr.print(input); textArea.append(input); - lines++; + // lines++; } if (quit) return; + trimBuffer(false); } } catch (Exception e) { textArea.append("\nConsole reports an Internal error."); - textArea.append("The error is: " + e); - lines+=2; + 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) @@ -287,17 +306,40 @@ 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 (lines>lim) + if (clear || lines > lim) { - try { - String header = textArea.getText(0,textArea.getLineEndOffset(5))+"\n..Truncated..\n"; // keep first 5 lines for startup info - int truncate = textArea.getLineEndOffset(lim-7-lines); - textArea.setText(header+textArea.getText(truncate,textArea.getText().length()-truncate)); } catch (Exception e) + try + { + if (lines > 5) { - e.printStackTrace(); + // 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); + } } - lines = textArea.getLineCount(); + + } catch (Exception e) + { + e.printStackTrace(); + } + lines = textArea.getLineCount(); } } @@ -306,6 +348,7 @@ public class Console extends WindowAdapter implements WindowListener, throws IOException { String input = ""; + int lp = -1; do { int available = in.available(); @@ -314,6 +357,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; } @@ -321,7 +368,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) @@ -330,12 +377,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(); } -- 1.7.10.2