2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
22 import java.awt.event.*;
25 import org.apache.log4j.SimpleLayout;
28 * Simple Jalview Java Console. Version 1 - allows viewing of console output
29 * after desktop is created. Acquired with thanks from RJHM's site
30 * http://www.comweb.nl/java/Console/Console.html A simple Java Console for your
31 * application (Swing version) Requires Java 1.1.5 or higher Disclaimer the use
32 * of this source is at your own risk. Permision to use and distribute into your
33 * own applications RJHM van den Bergh , rvdb@comweb.nl
36 public class Console extends WindowAdapter implements WindowListener,
37 ActionListener, Runnable
41 private JTextArea textArea;
47 private Thread reader;
49 private Thread reader2;
53 private final PrintStream stdout = System.out;
55 private final PrintStream stderr = System.err;
57 private final PipedInputStream pin = new PipedInputStream();
59 private final PipedInputStream pin2 = new PipedInputStream();
61 Thread errorThrower; // just for testing (Throws an Exception at this Console
63 // are we attached to some parent Desktop
64 Desktop parent = null;
68 // create all components and add them
69 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
70 frame = initFrame("Java Console", screenSize.width / 2,
71 screenSize.height / 2, -1, -1);
75 private void initConsole(boolean visible)
77 // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
78 // textArea = cpt.getTextArea();
79 textArea = new JTextArea();
80 textArea.setEditable(false);
81 JButton button = new JButton("clear");
84 frame.getContentPane().setLayout(new BorderLayout());
85 frame.getContentPane().add(new JScrollPane(textArea),
87 frame.getContentPane().add(button, BorderLayout.SOUTH);
88 frame.setVisible(visible);
90 frame.addWindowListener(this);
91 button.addActionListener(this);
95 PipedOutputStream pout = new PipedOutputStream(this.pin);
96 System.setOut(new PrintStream(pout, true));
97 } catch (java.io.IOException io)
99 textArea.append("Couldn't redirect STDOUT to this console\n"
101 } catch (SecurityException se)
103 textArea.append("Couldn't redirect STDOUT to this console\n"
109 PipedOutputStream pout2 = new PipedOutputStream(this.pin2);
110 System.setErr(new PrintStream(pout2, true));
111 } catch (java.io.IOException io)
113 textArea.append("Couldn't redirect STDERR to this console\n"
115 } catch (SecurityException se)
117 textArea.append("Couldn't redirect STDERR to this console\n"
121 quit = false; // signals the Threads that they should exit
123 // Starting two seperate threads to read from the PipedInputStreams
125 reader = new Thread(this);
126 reader.setDaemon(true);
129 reader2 = new Thread(this);
130 reader2.setDaemon(true);
137 // you may omit this part for your application
140 System.out.println("Hello World 2");
141 System.out.println("All fonts available to Graphic2D:\n");
142 GraphicsEnvironment ge = GraphicsEnvironment
143 .getLocalGraphicsEnvironment();
144 String[] fontNames = ge.getAvailableFontFamilyNames();
145 for (int n = 0; n < fontNames.length; n++)
146 System.out.println(fontNames[n]);
147 // Testing part: simple an error thrown anywhere in this JVM will be printed
149 // We do it with a seperate Thread becasue we don't wan't to break a Thread
150 // used by the Console.
151 System.out.println("\nLets throw an error on this console");
152 errorThrower = new Thread(this);
153 errorThrower.setDaemon(true);
154 errorThrower.start();
157 private JFrame initFrame(String string, int i, int j, int x, int y)
159 JFrame frame = new JFrame(string);
160 frame.setName(string);
165 frame.setBounds(x, y, i, j);
170 * attach a console to the desktop - the desktop will open it if requested.
174 public Console(Desktop desktop)
177 // window name - get x,y,width, height possibly scaled
178 Rectangle bounds = desktop.getLastKnownDimensions("JAVA_CONSOLE_");
181 frame = initFrame("Jalview Java Console", desktop.getWidth() / 2,
182 desktop.getHeight() / 4, desktop.getX(), desktop.getY());
186 frame = initFrame("Jalview Java Console", bounds.width,
187 bounds.height, bounds.x, bounds.y);
189 // desktop.add(frame);
191 JalviewAppender jappender = new JalviewAppender();
192 jappender.setLayout(new SimpleLayout());
193 JalviewAppender.setTextArea(textArea);
194 org.apache.log4j.Logger.getRootLogger().addAppender(jappender);
197 public synchronized void stopConsole()
200 this.notifyAll(); // stop all threads
205 } catch (Exception e)
212 } catch (Exception e)
218 public synchronized void windowClosed(WindowEvent evt)
220 frame.setVisible(false);
228 parent.showConsole(false);
232 public synchronized void windowClosing(WindowEvent evt)
234 frame.setVisible(false); // default behaviour of JFrame
238 public synchronized void actionPerformed(ActionEvent evt)
241 // textArea.setText("");
244 public synchronized void run()
248 while (Thread.currentThread() == reader)
253 } catch (InterruptedException ie)
256 while (pin.available() != 0)
258 String input = this.readLine(pin);
260 textArea.append(input);
268 while (Thread.currentThread() == reader2)
273 } catch (InterruptedException ie)
276 while (pin2.available() != 0)
278 String input = this.readLine(pin2);
280 textArea.append(input);
287 } catch (Exception e)
289 textArea.append("\nConsole reports an Internal error.");
290 textArea.append("The error is: " + e.getMessage());
292 stderr.println("Console reports an Internal error.\nThe error is: "
296 // just for testing (Throw a Nullpointer after 1 second)
297 if (Thread.currentThread() == errorThrower)
302 } catch (InterruptedException ie)
305 throw new NullPointerException(
306 "Application test: throwing an NullPointerException It should arrive at the console");
310 private synchronized void trimBuffer(boolean clear)
313 if (clear || lines > lim)
319 // minimum length for truncation/clearing
320 String header = textArea.getText(0, textArea.getLineEndOffset(5))
321 + "\n..Truncated..\n"; // keep first 5 lines for startup info
325 truncate = textArea.getLineEndOffset(lines - lim - 7);
326 textArea.setText(header
327 + textArea.getText(truncate, textArea.getText()
333 textArea.setText(header);
337 } catch (Exception e)
341 lines = textArea.getLineCount();
346 public synchronized String readLine(PipedInputStream in)
353 int available = in.available();
356 byte b[] = new byte[available];
358 input = input + new String(b, 0, b.length);
359 while ((lp = input.indexOf("\n", lp + 1)) > -1)
363 } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
367 public static void main(String[] arg)
369 new Console().test(); // create console with not reference
373 public void setVisible(boolean selected)
375 frame.setVisible(selected);
376 if (selected == true)
382 public Rectangle getBounds()
386 return frame.getBounds();