JAL-629 Fix --nodebug turning off debug mode from properties
[jalview.git] / src / jalview / gui / Console.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Dimension;
26 import java.awt.GraphicsEnvironment;
27 import java.awt.GridBagConstraints;
28 import java.awt.GridBagLayout;
29 import java.awt.Rectangle;
30 import java.awt.Toolkit;
31 import java.awt.datatransfer.Clipboard;
32 import java.awt.datatransfer.StringSelection;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.MouseAdapter;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.WindowAdapter;
38 import java.awt.event.WindowEvent;
39 import java.awt.event.WindowListener;
40 import java.io.IOException;
41 import java.io.PipedInputStream;
42 import java.io.PipedOutputStream;
43 import java.io.PrintStream;
44
45 import javax.swing.BorderFactory;
46 import javax.swing.JButton;
47 import javax.swing.JComboBox;
48 import javax.swing.JFrame;
49 import javax.swing.JLabel;
50 import javax.swing.JPanel;
51 import javax.swing.JScrollPane;
52 import javax.swing.JTextArea;
53 import javax.swing.border.Border;
54 import javax.swing.text.DefaultCaret;
55
56 import jalview.bin.Cache;
57 import jalview.log.JLoggerI.LogLevel;
58 import jalview.log.JLoggerLog4j;
59 import jalview.log.JalviewAppender;
60 import jalview.util.ChannelProperties;
61 import jalview.util.MessageManager;
62 import jalview.util.Platform;
63
64 /**
65  * Simple Jalview Java Console. Version 1 - allows viewing of console output
66  * after desktop is created. Acquired with thanks from RJHM's site
67  * http://www.comweb.nl/java/Console/Console.html A simple Java Console for your
68  * application (Swing version) Requires Java 1.1.5 or higher Disclaimer the use
69  * of this source is at your own risk. Permision to use and distribute into your
70  * own applications RJHM van den Bergh , rvdb@comweb.nl
71  */
72
73 public class Console extends WindowAdapter
74         implements WindowListener, ActionListener, Runnable
75 {
76   private JFrame frame;
77
78   private JTextArea textArea;
79
80   /*
81    * unused - tally and limit for lines in console window int lines = 0;
82    * 
83    * int lim = 1000;
84    */
85   int byteslim = 102400, bytescut = 76800; // 100k and 75k cut point.
86
87   private Thread reader, reader2, textAppender;
88
89   private boolean quit;
90
91   private final PrintStream stdout = System.out, stderr = System.err;
92
93   private PipedInputStream pin = new PipedInputStream();
94
95   private PipedInputStream pin2 = new PipedInputStream();
96
97   private StringBuffer displayPipe = new StringBuffer();
98
99   Thread errorThrower; // just for testing (Throws an Exception at this Console
100
101   // are we attached to some parent Desktop
102   Desktop parent = null;
103
104   private int MIN_WIDTH = 300;
105
106   private int MIN_HEIGHT = 250;
107
108   private JComboBox<LogLevel> logLevelCombo = new JComboBox<LogLevel>();
109
110   protected LogLevel startingLogLevel = null;
111
112   public Console()
113   {
114     // create all components and add them
115     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
116     frame = initFrame("Java Console", screenSize.width / 2,
117             screenSize.height / 2, -1, -1);
118     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
119     initConsole(true);
120   }
121
122   private void initConsole(boolean visible)
123   {
124     initConsole(visible, true);
125   }
126
127   /**
128    * 
129    * @param visible
130    *          - open the window
131    * @param redirect
132    *          - redirect std*
133    */
134   private void initConsole(boolean visible, boolean redirect)
135   {
136     // CutAndPasteTransfer cpt = new CutAndPasteTransfer();
137     // textArea = cpt.getTextArea();
138     textArea = new JTextArea();
139     textArea.setEditable(false);
140     // autoscroll
141     DefaultCaret caret = (DefaultCaret) textArea.getCaret();
142     caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
143     // toggle autoscroll by clicking on the text area
144     Border pausedBorder = BorderFactory.createMatteBorder(2, 2, 2, 2,
145             textArea.getForeground());
146     Border noBorder = BorderFactory.createEmptyBorder(2, 2, 2, 2);
147     JScrollPane scrollPane = new JScrollPane(textArea);
148     scrollPane.setBorder(noBorder);
149     textArea.addMouseListener(new MouseAdapter()
150     {
151       @Override
152       public void mouseClicked(MouseEvent e)
153       {
154         if (e.getButton() == MouseEvent.BUTTON1)
155         {
156           if (caret.getUpdatePolicy() == DefaultCaret.ALWAYS_UPDATE)
157           {
158             caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
159             scrollPane.setBorder(pausedBorder);
160           }
161           else
162           {
163             caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
164             textArea.setCaretPosition(textArea.getDocument().getLength());
165             scrollPane.setBorder(noBorder);
166           }
167         }
168       }
169     });
170
171     JButton clearButton = new JButton(
172             MessageManager.getString("action.clear"));
173     JButton copyToClipboardButton = new JButton(
174             MessageManager.getString("label.copy_to_clipboard"));
175     copyToClipboardButton.addActionListener(new ActionListener()
176     {
177       @Override
178       public void actionPerformed(ActionEvent e)
179       {
180         copyConsoleTextToClipboard();
181       }
182     });
183     copyToClipboardButton.addMouseListener(new MouseAdapter()
184     {
185       private Color bg = textArea.getBackground();
186
187       private Color fg = textArea.getForeground();
188
189       @Override
190       public void mousePressed(MouseEvent e)
191       {
192         textArea.setBackground(textArea.getSelectionColor());
193         textArea.setForeground(textArea.getSelectedTextColor());
194       }
195
196       @Override
197       public void mouseReleased(MouseEvent e)
198       {
199         textArea.setBackground(bg);
200         textArea.setForeground(fg);
201       }
202
203     });
204     copyToClipboardButton.setToolTipText(
205             MessageManager.getString("label.copy_to_clipboard_tooltip"));
206
207     JLabel logLevelLabel = new JLabel(
208             MessageManager.getString("label.log_level") + ":");
209
210     // logLevelCombo.addItem(LogLevel.ALL);
211     logLevelCombo.addItem(LogLevel.TRACE);
212     logLevelCombo.addItem(LogLevel.DEBUG);
213     logLevelCombo.addItem(LogLevel.INFO);
214     logLevelCombo.addItem(LogLevel.WARN);
215     // logLevelCombo.addItem(LogLevel.ERROR);
216     // logLevelCombo.addItem(LogLevel.FATAL);
217     // logLevelCombo.addItem(LogLevel.ERROR);
218     // logLevelCombo.addItem(LogLevel.OFF);
219     // set startingLogLevel
220     if (jalview.bin.Console.log == null)
221     {
222       try
223       {
224         startingLogLevel = LogLevel
225                 .valueOf(Cache.getDefault(Cache.JALVIEWLOGLEVEL, null));
226       } catch (IllegalArgumentException e1)
227       {
228         jalview.bin.Console.debug(
229                 "Invalid value for preference " + Cache.JALVIEWLOGLEVEL);
230       } catch (NullPointerException e2)
231       {
232         // no value in preferences
233       } finally
234       {
235         if (startingLogLevel == null)
236         {
237           startingLogLevel = LogLevel.INFO;
238         }
239       }
240     }
241     else
242     {
243       startingLogLevel = jalview.bin.Console.log.getLevel();
244     }
245     setChosenLogLevelCombo();
246     logLevelCombo.addActionListener(new ActionListener()
247     {
248       @Override
249       public void actionPerformed(ActionEvent e)
250       {
251         if (jalview.bin.Console.log != null)
252         {
253           jalview.bin.Console.log
254                   .setLevel((LogLevel) logLevelCombo.getSelectedItem());
255         }
256       }
257
258     });
259
260     // frame = cpt;
261     frame.getContentPane().setLayout(new BorderLayout());
262     frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
263     JPanel southPanel = new JPanel();
264     southPanel.setLayout(new GridBagLayout());
265
266     JPanel logLevelPanel = new JPanel();
267     logLevelPanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
268     logLevelPanel.add(logLevelLabel);
269     logLevelPanel.add(logLevelCombo);
270     String logLevelTooltip = MessageManager.formatMessage(
271             "label.log_level_tooltip", startingLogLevel.toString());
272     logLevelLabel.setToolTipText(logLevelTooltip);
273     logLevelCombo.setToolTipText(logLevelTooltip);
274
275     GridBagConstraints gbc = new GridBagConstraints();
276     gbc.gridx = 0;
277     gbc.gridy = 0;
278     gbc.gridwidth = 1;
279     gbc.gridheight = 1;
280     gbc.weightx = 0.1;
281     southPanel.add(logLevelPanel, gbc);
282
283     gbc.gridx++;
284     gbc.weightx = 0.8;
285     gbc.fill = GridBagConstraints.HORIZONTAL;
286     southPanel.add(clearButton, gbc);
287
288     gbc.gridx++;
289     gbc.weightx = 0.1;
290     gbc.fill = GridBagConstraints.NONE;
291     southPanel.add(copyToClipboardButton, gbc);
292
293     southPanel.setVisible(true);
294     frame.getContentPane().add(southPanel, BorderLayout.SOUTH);
295     frame.setVisible(visible);
296     updateConsole = visible;
297     frame.addWindowListener(this);
298     clearButton.addActionListener(this);
299
300     if (redirect)
301     {
302       redirectStreams();
303     }
304     else
305     {
306       unredirectStreams();
307     }
308     quit = false; // signals the Threads that they should exit
309
310     // Starting two seperate threads to read from the PipedInputStreams
311     //
312     reader = new Thread(this);
313     reader.setDaemon(true);
314     reader.start();
315     //
316     reader2 = new Thread(this);
317     reader2.setDaemon(true);
318     reader2.start();
319     // and a thread to append text to the textarea
320     textAppender = new Thread(this);
321     textAppender.setDaemon(true);
322     textAppender.start();
323
324     // set icons
325     frame.setIconImages(ChannelProperties.getIconList());
326   }
327
328   private void setChosenLogLevelCombo()
329   {
330     setChosenLogLevelCombo(startingLogLevel);
331   }
332
333   private void setChosenLogLevelCombo(LogLevel setLogLevel)
334   {
335     logLevelCombo.setSelectedItem(setLogLevel);
336     if (!logLevelCombo.getSelectedItem().equals(setLogLevel))
337     {
338       // setLogLevel not (yet) in list
339       if (setLogLevel != null && setLogLevel instanceof LogLevel)
340       {
341         // add new item to list (might be set via .jalview_properties)
342         boolean added = false;
343         for (int i = 0; i < logLevelCombo.getItemCount(); i++)
344         {
345           LogLevel l = logLevelCombo.getItemAt(i);
346           if (l.compareTo(setLogLevel) >= 0)
347           {
348             logLevelCombo.insertItemAt(setLogLevel, i);
349             added = true;
350             break;
351           }
352         }
353         if (!added) // lower priority than others or some confusion -- add to
354                     // end of list
355         {
356           logLevelCombo.addItem(setLogLevel);
357         }
358         logLevelCombo.setSelectedItem(setLogLevel);
359       }
360       else
361       {
362         logLevelCombo.setSelectedItem(LogLevel.INFO);
363       }
364     }
365   }
366
367   private void copyConsoleTextToClipboard()
368   {
369     String consoleText = textArea.getText();
370     StringSelection consoleTextSelection = new StringSelection(consoleText);
371     Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
372     cb.setContents(consoleTextSelection, null);
373   }
374
375   PipedOutputStream pout = null, perr = null;
376
377   public void redirectStreams()
378   {
379     if (pout == null)
380     {
381       try
382       {
383         pout = new PipedOutputStream(this.pin);
384         System.setOut(new PrintStream(pout, true));
385       } catch (java.io.IOException io)
386       {
387         textArea.append("Couldn't redirect STDOUT to this console\n"
388                 + io.getMessage());
389         io.printStackTrace(stderr);
390       } catch (SecurityException se)
391       {
392         textArea.append("Couldn't redirect STDOUT to this console\n"
393                 + se.getMessage());
394         se.printStackTrace(stderr);
395       }
396
397       try
398       {
399         perr = new PipedOutputStream(this.pin2);
400         System.setErr(new PrintStream(perr, true));
401       } catch (java.io.IOException io)
402       {
403         textArea.append("Couldn't redirect STDERR to this console\n"
404                 + io.getMessage());
405         io.printStackTrace(stderr);
406       } catch (SecurityException se)
407       {
408         textArea.append("Couldn't redirect STDERR to this console\n"
409                 + se.getMessage());
410         se.printStackTrace(stderr);
411       }
412     }
413   }
414
415   public void unredirectStreams()
416   {
417     if (pout != null)
418     {
419       try
420       {
421         System.setOut(stdout);
422         pout.flush();
423         pout.close();
424         pin = new PipedInputStream();
425         pout = null;
426       } catch (java.io.IOException io)
427       {
428         textArea.append("Couldn't unredirect STDOUT to this console\n"
429                 + io.getMessage());
430         io.printStackTrace(stderr);
431       } catch (SecurityException se)
432       {
433         textArea.append("Couldn't unredirect STDOUT to this console\n"
434                 + se.getMessage());
435         se.printStackTrace(stderr);
436       }
437
438       try
439       {
440         System.setErr(stderr);
441         perr.flush();
442         perr.close();
443         pin2 = new PipedInputStream();
444         perr = null;
445       } catch (java.io.IOException io)
446       {
447         textArea.append("Couldn't unredirect STDERR to this console\n"
448                 + io.getMessage());
449         io.printStackTrace(stderr);
450       } catch (SecurityException se)
451       {
452         textArea.append("Couldn't unredirect STDERR to this console\n"
453                 + se.getMessage());
454         se.printStackTrace(stderr);
455       }
456     }
457   }
458
459   public void test()
460   {
461     // testing part
462     // you may omit this part for your application
463     //
464
465     jalview.bin.Console.outPrintln("Hello World 2");
466     jalview.bin.Console.outPrintln("All fonts available to Graphic2D:\n");
467     GraphicsEnvironment ge = GraphicsEnvironment
468             .getLocalGraphicsEnvironment();
469     String[] fontNames = ge.getAvailableFontFamilyNames();
470     for (int n = 0; n < fontNames.length; n++)
471     {
472       jalview.bin.Console.outPrintln(fontNames[n]);
473     }
474     // Testing part: simple an error thrown anywhere in this JVM will be printed
475     // on the Console
476     // We do it with a seperate Thread becasue we don't wan't to break a Thread
477     // used by the Console.
478     jalview.bin.Console.outPrintln("\nLets throw an error on this console");
479     errorThrower = new Thread(this);
480     errorThrower.setDaemon(true);
481     errorThrower.start();
482   }
483
484   private JFrame initFrame(String string, int i, int j, int x, int y)
485   {
486     JFrame frame = new JFrame(string);
487     frame.setName(string);
488     if (x == -1)
489     {
490       x = i / 2;
491     }
492     if (y == -1)
493     {
494       y = j / 2;
495     }
496     frame.setBounds(x, y, i, j);
497     return frame;
498   }
499
500   /**
501    * attach a console to the desktop - the desktop will open it if requested.
502    * 
503    * @param desktop
504    */
505   public Console(Desktop desktop)
506   {
507     parent = desktop;
508     // window name - get x,y,width, height possibly scaled
509     Rectangle bounds = parent == null ? null
510             : parent.getLastKnownDimensions("JAVA_CONSOLE_");
511     if (bounds != null)
512     {
513       frame = initFrame(
514               ChannelProperties.getProperty("app_name") + " Java Console",
515               bounds.width, bounds.height, bounds.x, bounds.y);
516     }
517     else if (parent != null && parent.getWidth() > 0
518             && parent.getHeight() > 0)
519     {
520       frame = initFrame(
521               ChannelProperties.getProperty("app_name") + " Java Console",
522               parent.getWidth() / 2, parent.getHeight() / 4, parent.getX(),
523               parent.getY());
524     }
525     else
526     {
527       frame = initFrame(
528               ChannelProperties.getProperty("app_name") + " Java Console",
529               MIN_WIDTH, MIN_HEIGHT, 10, 10);
530     }
531     frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
532     // parent.add(frame);
533     initConsole(false);
534     LogLevel level = (LogLevel) logLevelCombo.getSelectedItem();
535     if (!Platform.isJS())
536     {
537       JalviewAppender jappender = new JalviewAppender(level);
538       JalviewAppender.setTextArea(textArea);
539       jappender.start();
540       if (jalview.bin.Console.log != null
541               && jalview.bin.Console.log instanceof JLoggerLog4j)
542       {
543         JLoggerLog4j.addAppender(jalview.bin.Console.log, jappender);
544       }
545     }
546   }
547
548   public synchronized void stopConsole()
549   {
550     quit = true;
551     this.notifyAll();
552     /*
553      * reader.notify(); reader2.notify(); if (errorThrower!=null)
554      * errorThrower.notify(); // stop all threads if (textAppender!=null)
555      * textAppender.notify();
556      */
557     if (pout != null)
558     {
559       try
560       {
561         reader.join(10);
562         pin.close();
563       } catch (Exception e)
564       {
565         jalview.bin.Console.debug("pin.close() error", e);
566       }
567       try
568       {
569         reader2.join(10);
570         pin2.close();
571       } catch (Exception e)
572       {
573         jalview.bin.Console.debug("pin2.close() error", e);
574       }
575       try
576       {
577         textAppender.join(10);
578       } catch (Exception e)
579       {
580         jalview.bin.Console.debug("textAppender.join(10) error", e);
581       }
582     }
583     /*
584     if (!frame.isVisible())
585     {
586       frame.dispose();
587     }
588     */
589     // System.exit(0);
590   }
591
592   @Override
593   public synchronized void windowClosed(WindowEvent evt)
594   {
595     frame.setVisible(false);
596     closeConsoleGui();
597   }
598
599   private void closeConsoleGui()
600   {
601     updateConsole = false;
602     if (parent == null)
603     {
604
605       stopConsole();
606     }
607     else
608     {
609       parent.showConsole(false);
610     }
611   }
612
613   @Override
614   public synchronized void windowClosing(WindowEvent evt)
615   {
616     frame.setVisible(false); // default behaviour of JFrame
617     closeConsoleGui();
618
619     // frame.dispose();
620   }
621
622   @Override
623   public synchronized void actionPerformed(ActionEvent evt)
624   {
625     trimBuffer(true);
626     // textArea.setText("");
627   }
628
629   @Override
630   public synchronized void run()
631   {
632     try
633     {
634       while (Thread.currentThread() == reader)
635       {
636         if (pin == null || pin.available() == 0)
637         {
638           try
639           {
640             this.wait(100); // ##### implicated BLOCKED
641             if (pin.available() == 0)
642             {
643               trimBuffer(false);
644             }
645           } catch (InterruptedException ie)
646           {
647             jalview.bin.Console.debug("pin.available() error", ie);
648           }
649         }
650
651         while (pin.available() != 0)
652         {
653           String input = this.readLine(pin);
654           stdout.print(input);
655           long time = System.nanoTime();
656           appendToTextArea(input);
657           // stderr.println("Time taken to stdout append:\t"
658           // + (System.nanoTime() - time) + " ns");
659           // lines++;
660         }
661         if (quit)
662         {
663           return;
664         }
665       }
666
667       while (Thread.currentThread() == reader2)
668       {
669         if (pin2.available() == 0)
670         {
671           try
672           {
673             this.wait(100); // ##### implicated BLOCKED
674             if (pin2.available() == 0)
675             {
676               trimBuffer(false);
677             }
678           } catch (InterruptedException ie)
679           {
680             jalview.bin.Console.debug("pin.available() error", ie);
681           }
682         }
683         while (pin2.available() != 0)
684         {
685           String input = this.readLine(pin2);
686           stderr.print(input);
687           long time = System.nanoTime();
688           appendToTextArea(input);
689           // stderr.println("Time taken to stderr append:\t"
690           // + (System.nanoTime() - time) + " ns");
691           // lines++;
692         }
693         if (quit)
694         {
695           return;
696         }
697       }
698       while (Thread.currentThread() == textAppender)
699       {
700         if (updateConsole)
701         {
702           // check string buffer - if greater than console, clear console and
703           // replace with last segment of content, otherwise, append all to
704           // content.
705           long count;
706           while (displayPipe.length() > 0)
707           {
708             count = 0;
709             StringBuffer tmp = new StringBuffer(), replace;
710             synchronized (displayPipe)
711             {
712               replace = displayPipe;
713               displayPipe = tmp;
714             }
715             // simply append whole buffer
716             textArea.append(replace.toString()); // ##### implicated BLOCKED
717             count += replace.length();
718             if (count > byteslim)
719             {
720               trimBuffer(false);
721             }
722           }
723           if (displayPipe.length() == 0)
724           {
725             try
726             {
727               this.wait(100);
728               if (displayPipe.length() == 0)
729               {
730                 trimBuffer(false);
731               }
732             } catch (InterruptedException e)
733             {
734               jalview.bin.Console.debug("displayPipe.length() error", e);
735             }
736           }
737         }
738         else
739         {
740           try
741           {
742             this.wait(100);
743           } catch (InterruptedException e)
744           {
745             jalview.bin.Console.debug("this.wait(100) error", e);
746           }
747         }
748         if (quit)
749         {
750           return;
751         }
752
753       }
754     } catch (Exception e)
755     {
756       textArea.append("\nConsole reports an Internal error.");
757       textArea.append("The error is: " + e.getMessage());
758       // Need to uncomment this to ensure that line tally is synched.
759       // lines += 2;
760       stderr.println(
761               "Console reports an Internal error.\nThe error is: " + e);
762     }
763
764     // just for testing (Throw a Nullpointer after 1 second)
765     if (Thread.currentThread() == errorThrower)
766     {
767       try
768       {
769         this.wait(1000);
770       } catch (InterruptedException ie)
771       {
772         jalview.bin.Console.debug("this.wait(1000) error", ie);
773       }
774       throw new NullPointerException(
775               MessageManager.getString("exception.application_test_npe"));
776     }
777   }
778
779   private void appendToTextArea(final String input)
780   {
781     if (updateConsole == false)
782     {
783       // do nothing;
784       return;
785     }
786     long time = System.nanoTime();
787     javax.swing.SwingUtilities.invokeLater(new Runnable()
788     {
789       @Override
790       public void run()
791       {
792         displayPipe.append(input); // change to stringBuffer
793         // displayPipe.flush();
794
795       }
796     });
797     // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() -
798     // time)
799     // + " ns");
800   }
801
802   private String header = null;
803
804   private boolean updateConsole = false;
805
806   private synchronized void trimBuffer(boolean clear)
807   {
808     if (header == null && textArea.getLineCount() > 5)
809     {
810       try
811       {
812         header = textArea.getText(0, textArea.getLineStartOffset(5))
813                 + "\nTruncated...\n";
814       } catch (Exception e)
815       {
816         jalview.bin.Console.warn("textArea Exception", e);
817       }
818     }
819     // trim the buffer
820     int tlength = textArea.getDocument().getLength();
821     if (header != null)
822     {
823       if (clear || (tlength > byteslim))
824       {
825         try
826         {
827           if (!clear)
828           {
829             long time = System.nanoTime();
830             textArea.replaceRange(header, 0, tlength - bytescut);
831             // stderr.println("Time taken to cut:\t"
832             // + (System.nanoTime() - time) + " ns");
833           }
834           else
835           {
836             textArea.setText(header);
837           }
838         } catch (Exception e)
839         {
840           jalview.bin.Console.warn("textArea Exception", e);
841         }
842         // lines = textArea.getLineCount();
843       }
844     }
845
846   }
847
848   public synchronized String readLine(PipedInputStream in)
849           throws IOException
850   {
851     String input = "";
852     int lp = -1;
853     do
854     {
855       int available = in.available();
856       if (available == 0)
857       {
858         break;
859       }
860       byte b[] = new byte[available];
861       in.read(b);
862       input = input + new String(b, 0, b.length);
863       // counts lines - we don't do this for speed.
864       // while ((lp = input.indexOf("\n", lp + 1)) > -1)
865       // {
866       // lines++;
867       // }
868     } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
869     return input;
870   }
871
872   /**
873    * @j2sIgnore
874    * @param arg
875    */
876   public static void main(String[] arg)
877   {
878     new Console().test(); // create console with not reference
879
880   }
881
882   public void setVisible(boolean selected)
883   {
884     frame.setVisible(selected);
885     if (selected == true)
886     {
887       setChosenLogLevelCombo();
888       redirectStreams();
889       updateConsole = true;
890       frame.toFront();
891     }
892     else
893     {
894       // reset log level to what it was before
895       if (jalview.bin.Console.log != null)
896       {
897         jalview.bin.Console.log.setLevel(startingLogLevel);
898       }
899
900       unredirectStreams();
901       updateConsole = false;
902     }
903   }
904
905   public Rectangle getBounds()
906   {
907     if (frame != null)
908     {
909       return frame.getBounds();
910     }
911     return null;
912   }
913
914   /**
915    * set the banner that appears at the top of the console output
916    * 
917    * @param string
918    */
919   public void setHeader(String string)
920   {
921     header = string;
922     if (header.charAt(header.length() - 1) != '\n')
923     {
924       header += "\n";
925     }
926     textArea.insert(header, 0);
927   }
928
929   /**
930    * get the banner
931    * 
932    * @return
933    */
934   public String getHeader()
935   {
936     return header;
937   }
938 }