JAL-4298 reuse existing log level preference method
[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     
221     if (jalview.bin.Console.getLogger() == null)
222     {
223       startingLogLevel = jalview.bin.Console.getCachedLogLevel();
224     }
225     else
226     {
227       startingLogLevel = jalview.bin.Console.getLogger().getLevel();
228     }
229     setChosenLogLevelCombo();
230     logLevelCombo.addActionListener(new ActionListener()
231     {
232       @Override
233       public void actionPerformed(ActionEvent e)
234       {
235         if (jalview.bin.Console.log != null)
236         {
237           jalview.bin.Console.log
238                   .setLevel((LogLevel) logLevelCombo.getSelectedItem());
239         }
240       }
241
242     });
243
244     // frame = cpt;
245     frame.getContentPane().setLayout(new BorderLayout());
246     frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
247     JPanel southPanel = new JPanel();
248     southPanel.setLayout(new GridBagLayout());
249
250     JPanel logLevelPanel = new JPanel();
251     logLevelPanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
252     logLevelPanel.add(logLevelLabel);
253     logLevelPanel.add(logLevelCombo);
254     String logLevelTooltip = MessageManager.formatMessage(
255             "label.log_level_tooltip", startingLogLevel.toString());
256     logLevelLabel.setToolTipText(logLevelTooltip);
257     logLevelCombo.setToolTipText(logLevelTooltip);
258
259     GridBagConstraints gbc = new GridBagConstraints();
260     gbc.gridx = 0;
261     gbc.gridy = 0;
262     gbc.gridwidth = 1;
263     gbc.gridheight = 1;
264     gbc.weightx = 0.1;
265     southPanel.add(logLevelPanel, gbc);
266
267     gbc.gridx++;
268     gbc.weightx = 0.8;
269     gbc.fill = GridBagConstraints.HORIZONTAL;
270     southPanel.add(clearButton, gbc);
271
272     gbc.gridx++;
273     gbc.weightx = 0.1;
274     gbc.fill = GridBagConstraints.NONE;
275     southPanel.add(copyToClipboardButton, gbc);
276
277     southPanel.setVisible(true);
278     frame.getContentPane().add(southPanel, BorderLayout.SOUTH);
279     frame.setVisible(visible);
280     updateConsole = visible;
281     frame.addWindowListener(this);
282     clearButton.addActionListener(this);
283
284     if (redirect)
285     {
286       redirectStreams();
287     }
288     else
289     {
290       unredirectStreams();
291     }
292     quit = false; // signals the Threads that they should exit
293
294     // Starting two seperate threads to read from the PipedInputStreams
295     //
296     reader = new Thread(this);
297     reader.setDaemon(true);
298     reader.start();
299     //
300     reader2 = new Thread(this);
301     reader2.setDaemon(true);
302     reader2.start();
303     // and a thread to append text to the textarea
304     textAppender = new Thread(this);
305     textAppender.setDaemon(true);
306     textAppender.start();
307
308     // set icons
309     frame.setIconImages(ChannelProperties.getIconList());
310   }
311
312   private void setChosenLogLevelCombo()
313   {
314     setChosenLogLevelCombo(startingLogLevel);
315   }
316
317   private void setChosenLogLevelCombo(LogLevel setLogLevel)
318   {
319     logLevelCombo.setSelectedItem(setLogLevel);
320     if (!logLevelCombo.getSelectedItem().equals(setLogLevel))
321     {
322       // setLogLevel not (yet) in list
323       if (setLogLevel != null && setLogLevel instanceof LogLevel)
324       {
325         // add new item to list (might be set via .jalview_properties)
326         boolean added = false;
327         for (int i = 0; i < logLevelCombo.getItemCount(); i++)
328         {
329           LogLevel l = logLevelCombo.getItemAt(i);
330           if (l.compareTo(setLogLevel) >= 0)
331           {
332             logLevelCombo.insertItemAt(setLogLevel, i);
333             added = true;
334             break;
335           }
336         }
337         if (!added) // lower priority than others or some confusion -- add to
338                     // end of list
339         {
340           logLevelCombo.addItem(setLogLevel);
341         }
342         logLevelCombo.setSelectedItem(setLogLevel);
343       }
344       else
345       {
346         logLevelCombo.setSelectedItem(LogLevel.INFO);
347       }
348     }
349   }
350
351   private void copyConsoleTextToClipboard()
352   {
353     String consoleText = textArea.getText();
354     StringSelection consoleTextSelection = new StringSelection(consoleText);
355     Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
356     cb.setContents(consoleTextSelection, null);
357   }
358
359   PipedOutputStream pout = null, perr = null;
360
361   public void redirectStreams()
362   {
363     if (pout == null)
364     {
365       try
366       {
367         pout = new PipedOutputStream(this.pin);
368         System.setOut(new PrintStream(pout, true));
369       } catch (java.io.IOException io)
370       {
371         textArea.append("Couldn't redirect STDOUT to this console\n"
372                 + io.getMessage());
373         io.printStackTrace(stderr);
374       } catch (SecurityException se)
375       {
376         textArea.append("Couldn't redirect STDOUT to this console\n"
377                 + se.getMessage());
378         se.printStackTrace(stderr);
379       }
380
381       try
382       {
383         perr = new PipedOutputStream(this.pin2);
384         System.setErr(new PrintStream(perr, true));
385       } catch (java.io.IOException io)
386       {
387         textArea.append("Couldn't redirect STDERR to this console\n"
388                 + io.getMessage());
389         io.printStackTrace(stderr);
390       } catch (SecurityException se)
391       {
392         textArea.append("Couldn't redirect STDERR to this console\n"
393                 + se.getMessage());
394         se.printStackTrace(stderr);
395       }
396     }
397   }
398
399   public void unredirectStreams()
400   {
401     if (pout != null)
402     {
403       try
404       {
405         System.setOut(stdout);
406         pout.flush();
407         pout.close();
408         pin = new PipedInputStream();
409         pout = null;
410       } catch (java.io.IOException io)
411       {
412         textArea.append("Couldn't unredirect STDOUT to this console\n"
413                 + io.getMessage());
414         io.printStackTrace(stderr);
415       } catch (SecurityException se)
416       {
417         textArea.append("Couldn't unredirect STDOUT to this console\n"
418                 + se.getMessage());
419         se.printStackTrace(stderr);
420       }
421
422       try
423       {
424         System.setErr(stderr);
425         perr.flush();
426         perr.close();
427         pin2 = new PipedInputStream();
428         perr = null;
429       } catch (java.io.IOException io)
430       {
431         textArea.append("Couldn't unredirect STDERR to this console\n"
432                 + io.getMessage());
433         io.printStackTrace(stderr);
434       } catch (SecurityException se)
435       {
436         textArea.append("Couldn't unredirect STDERR to this console\n"
437                 + se.getMessage());
438         se.printStackTrace(stderr);
439       }
440     }
441   }
442
443   public void test()
444   {
445     // testing part
446     // you may omit this part for your application
447     //
448
449     jalview.bin.Console.outPrintln("Hello World 2");
450     jalview.bin.Console.outPrintln("All fonts available to Graphic2D:\n");
451     GraphicsEnvironment ge = GraphicsEnvironment
452             .getLocalGraphicsEnvironment();
453     String[] fontNames = ge.getAvailableFontFamilyNames();
454     for (int n = 0; n < fontNames.length; n++)
455     {
456       jalview.bin.Console.outPrintln(fontNames[n]);
457     }
458     // Testing part: simple an error thrown anywhere in this JVM will be printed
459     // on the Console
460     // We do it with a seperate Thread becasue we don't wan't to break a Thread
461     // used by the Console.
462     jalview.bin.Console.outPrintln("\nLets throw an error on this console");
463     errorThrower = new Thread(this);
464     errorThrower.setDaemon(true);
465     errorThrower.start();
466   }
467
468   private JFrame initFrame(String string, int i, int j, int x, int y)
469   {
470     JFrame frame = new JFrame(string);
471     frame.setName(string);
472     if (x == -1)
473     {
474       x = i / 2;
475     }
476     if (y == -1)
477     {
478       y = j / 2;
479     }
480     frame.setBounds(x, y, i, j);
481     return frame;
482   }
483
484   /**
485    * attach a console to the desktop - the desktop will open it if requested.
486    * 
487    * @param desktop
488    */
489   public Console(Desktop desktop)
490   {
491     parent = desktop;
492     // window name - get x,y,width, height possibly scaled
493     Rectangle bounds = parent == null ? null
494             : parent.getLastKnownDimensions("JAVA_CONSOLE_");
495     if (bounds != null)
496     {
497       frame = initFrame(
498               ChannelProperties.getProperty("app_name") + " Java Console",
499               bounds.width, bounds.height, bounds.x, bounds.y);
500     }
501     else if (parent != null && parent.getWidth() > 0
502             && parent.getHeight() > 0)
503     {
504       frame = initFrame(
505               ChannelProperties.getProperty("app_name") + " Java Console",
506               parent.getWidth() / 2, parent.getHeight() / 4, parent.getX(),
507               parent.getY());
508     }
509     else
510     {
511       frame = initFrame(
512               ChannelProperties.getProperty("app_name") + " Java Console",
513               MIN_WIDTH, MIN_HEIGHT, 10, 10);
514     }
515     frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
516     // parent.add(frame);
517     initConsole(false);
518     LogLevel level = (LogLevel) logLevelCombo.getSelectedItem();
519     if (!Platform.isJS())
520     {
521       JalviewAppender jappender = new JalviewAppender(level);
522       JalviewAppender.setTextArea(textArea);
523       jappender.start();
524       if (jalview.bin.Console.log != null
525               && jalview.bin.Console.log instanceof JLoggerLog4j)
526       {
527         JLoggerLog4j.addAppender(jalview.bin.Console.log, jappender);
528       }
529     }
530   }
531
532   public synchronized void stopConsole()
533   {
534     quit = true;
535     this.notifyAll();
536     /*
537      * reader.notify(); reader2.notify(); if (errorThrower!=null)
538      * errorThrower.notify(); // stop all threads if (textAppender!=null)
539      * textAppender.notify();
540      */
541     if (pout != null)
542     {
543       try
544       {
545         reader.join(10);
546         pin.close();
547       } catch (Exception e)
548       {
549         jalview.bin.Console.debug("pin.close() error", e);
550       }
551       try
552       {
553         reader2.join(10);
554         pin2.close();
555       } catch (Exception e)
556       {
557         jalview.bin.Console.debug("pin2.close() error", e);
558       }
559       try
560       {
561         textAppender.join(10);
562       } catch (Exception e)
563       {
564         jalview.bin.Console.debug("textAppender.join(10) error", e);
565       }
566     }
567     /*
568     if (!frame.isVisible())
569     {
570       frame.dispose();
571     }
572     */
573     // System.exit(0);
574   }
575
576   @Override
577   public synchronized void windowClosed(WindowEvent evt)
578   {
579     frame.setVisible(false);
580     closeConsoleGui();
581   }
582
583   private void closeConsoleGui()
584   {
585     updateConsole = false;
586     if (parent == null)
587     {
588
589       stopConsole();
590     }
591     else
592     {
593       parent.showConsole(false);
594     }
595   }
596
597   @Override
598   public synchronized void windowClosing(WindowEvent evt)
599   {
600     frame.setVisible(false); // default behaviour of JFrame
601     closeConsoleGui();
602
603     // frame.dispose();
604   }
605
606   @Override
607   public synchronized void actionPerformed(ActionEvent evt)
608   {
609     trimBuffer(true);
610     // textArea.setText("");
611   }
612
613   @Override
614   public synchronized void run()
615   {
616     try
617     {
618       while (Thread.currentThread() == reader)
619       {
620         if (pin == null || pin.available() == 0)
621         {
622           try
623           {
624             this.wait(100); // ##### implicated BLOCKED
625             if (pin.available() == 0)
626             {
627               trimBuffer(false);
628             }
629           } catch (InterruptedException ie)
630           {
631             jalview.bin.Console.debug("pin.available() error", ie);
632           }
633         }
634
635         while (pin.available() != 0)
636         {
637           String input = this.readLine(pin);
638           stdout.print(input);
639           long time = System.nanoTime();
640           appendToTextArea(input);
641           // stderr.println("Time taken to stdout append:\t"
642           // + (System.nanoTime() - time) + " ns");
643           // lines++;
644         }
645         if (quit)
646         {
647           return;
648         }
649       }
650
651       while (Thread.currentThread() == reader2)
652       {
653         if (pin2.available() == 0)
654         {
655           try
656           {
657             this.wait(100); // ##### implicated BLOCKED
658             if (pin2.available() == 0)
659             {
660               trimBuffer(false);
661             }
662           } catch (InterruptedException ie)
663           {
664             jalview.bin.Console.debug("pin.available() error", ie);
665           }
666         }
667         while (pin2.available() != 0)
668         {
669           String input = this.readLine(pin2);
670           stderr.print(input);
671           long time = System.nanoTime();
672           appendToTextArea(input);
673           // stderr.println("Time taken to stderr append:\t"
674           // + (System.nanoTime() - time) + " ns");
675           // lines++;
676         }
677         if (quit)
678         {
679           return;
680         }
681       }
682       while (Thread.currentThread() == textAppender)
683       {
684         if (updateConsole)
685         {
686           // check string buffer - if greater than console, clear console and
687           // replace with last segment of content, otherwise, append all to
688           // content.
689           long count;
690           while (displayPipe.length() > 0)
691           {
692             count = 0;
693             StringBuffer tmp = new StringBuffer(), replace;
694             synchronized (displayPipe)
695             {
696               replace = displayPipe;
697               displayPipe = tmp;
698             }
699             // simply append whole buffer
700             synchronized (textArea.getDocument())
701             {
702               textArea.append(replace.toString()); // ##### implicated BLOCKED
703               count += replace.length();
704               if (count > byteslim)
705               {
706                 trimBuffer(false);
707               }
708             }
709
710           }
711           if (displayPipe.length() == 0)
712           {
713             try
714             {
715               this.wait(100);
716               if (displayPipe.length() == 0)
717               {
718                 trimBuffer(false);
719               }
720             } catch (InterruptedException e)
721             {
722               jalview.bin.Console.debug("displayPipe.length() error", e);
723             }
724           }
725         }
726         else
727         {
728           try
729           {
730             this.wait(100);
731           } catch (InterruptedException e)
732           {
733             jalview.bin.Console.debug("this.wait(100) error", e);
734           }
735         }
736         if (quit)
737         {
738           return;
739         }
740
741       }
742     } catch (Exception e)
743     {
744       textArea.append("\nConsole reports an Internal error.");
745       textArea.append("The error is: " + e.getMessage());
746       // Need to uncomment this to ensure that line tally is synched.
747       // lines += 2;
748       stderr.println(
749               "Console reports an Internal error.\nThe error is: " + e);
750     }
751
752     // just for testing (Throw a Nullpointer after 1 second)
753     if (Thread.currentThread() == errorThrower)
754     {
755       try
756       {
757         this.wait(1000);
758       } catch (InterruptedException ie)
759       {
760         jalview.bin.Console.debug("this.wait(1000) error", ie);
761       }
762       throw new NullPointerException(
763               MessageManager.getString("exception.application_test_npe"));
764     }
765   }
766
767   private void appendToTextArea(final String input)
768   {
769     if (updateConsole == false)
770     {
771       // do nothing;
772       return;
773     }
774     long time = System.nanoTime();
775     javax.swing.SwingUtilities.invokeLater(new Runnable()
776     {
777       @Override
778       public void run()
779       {
780         displayPipe.append(input); // change to stringBuffer
781         // displayPipe.flush();
782
783       }
784     });
785     // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() -
786     // time)
787     // + " ns");
788   }
789
790   private String header = null;
791
792   private boolean updateConsole = false;
793
794   private synchronized void trimBuffer(boolean clear)
795   {
796     if (header == null && textArea.getLineCount() > 5)
797     {
798       try
799       {
800         header = textArea.getText(0, textArea.getLineStartOffset(5))
801                 + "\nTruncated...\n";
802       } catch (Exception e)
803       {
804         jalview.bin.Console.warn("textArea Exception", e);
805       }
806     }
807     // trim the buffer
808     int tlength = textArea.getDocument().getLength();
809     if (header != null)
810     {
811       if (clear || (tlength > byteslim))
812       {
813         try
814         {
815           if (!clear)
816           {
817             long time = System.nanoTime();
818             textArea.replaceRange(header, 0, tlength - bytescut);
819             // stderr.println("Time taken to cut:\t"
820             // + (System.nanoTime() - time) + " ns");
821           }
822           else
823           {
824             textArea.setText(header);
825           }
826         } catch (Exception e)
827         {
828           jalview.bin.Console.warn("textArea Exception", e);
829         }
830         // lines = textArea.getLineCount();
831       }
832     }
833
834   }
835
836   public synchronized String readLine(PipedInputStream in)
837           throws IOException
838   {
839     String input = "";
840     int lp = -1;
841     do
842     {
843       int available = in.available();
844       if (available == 0)
845       {
846         break;
847       }
848       byte b[] = new byte[available];
849       in.read(b);
850       input = input + new String(b, 0, b.length);
851       // counts lines - we don't do this for speed.
852       // while ((lp = input.indexOf("\n", lp + 1)) > -1)
853       // {
854       // lines++;
855       // }
856     } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
857     return input;
858   }
859
860   /**
861    * @j2sIgnore
862    * @param arg
863    */
864   public static void main(String[] arg)
865   {
866     new Console().test(); // create console with not reference
867
868   }
869
870   public void setVisible(boolean selected)
871   {
872     frame.setVisible(selected);
873     if (selected == true)
874     {
875       setChosenLogLevelCombo();
876       redirectStreams();
877       updateConsole = true;
878       frame.toFront();
879     }
880     else
881     {
882       // reset log level to what it was before
883       if (jalview.bin.Console.log != null)
884       {
885         jalview.bin.Console.log.setLevel(startingLogLevel);
886       }
887
888       unredirectStreams();
889       updateConsole = false;
890     }
891   }
892
893   public Rectangle getBounds()
894   {
895     if (frame != null)
896     {
897       return frame.getBounds();
898     }
899     return null;
900   }
901
902   /**
903    * set the banner that appears at the top of the console output
904    * 
905    * @param string
906    */
907   public void setHeader(String string)
908   {
909     header = string;
910     if (header.charAt(header.length() - 1) != '\n')
911     {
912       header += "\n";
913     }
914     textArea.insert(header, 0);
915   }
916
917   /**
918    * get the banner
919    * 
920    * @return
921    */
922   public String getHeader()
923   {
924     return header;
925   }
926 }