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