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,
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
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");
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
{
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;
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);
} catch (Exception e)
{
}
-// System.exit(0);
+ // System.exit(0);
}
public synchronized void windowClosed(WindowEvent evt)
{
stopConsole();
- } else {
+ }
+ else
+ {
parent.showConsole(false);
}
}
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
} 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)
} 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)
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();
}
}
throws IOException
{
String input = "";
+ int lp = -1;
do
{
int available = in.available();
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;
}
public static void main(String[] arg)
{
new Console().test(); // create console with not reference
-
+
}
public void setVisible(boolean selected)
if (selected == true)
{
frame.toFront();
- }
+ }
}
public Rectangle getBounds()
{
- if (frame!=null)
+ if (frame != null)
{
return frame.getBounds();
}