JAL-4298 Fixed initial log level to see .jalview_properties setting. Moved Java Conso...
[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.getLastKnownDimensions("JAVA_CONSOLE_");
510     if (bounds == null)
511     {
512       frame = initFrame(
513               ChannelProperties.getProperty("app_name") + " Java Console",
514               parent.getWidth() / 2, parent.getHeight() / 4, parent.getX(),
515               parent.getY());
516     }
517     else
518     {
519       frame = initFrame(
520               ChannelProperties.getProperty("app_name") + " Java Console",
521               bounds.width, bounds.height, bounds.x, bounds.y);
522     }
523     frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
524     // parent.add(frame);
525     initConsole(false);
526     LogLevel level = (LogLevel) logLevelCombo.getSelectedItem();
527     if (!Platform.isJS())
528     {
529       JalviewAppender jappender = new JalviewAppender(level);
530       JalviewAppender.setTextArea(textArea);
531       jappender.start();
532       if (jalview.bin.Console.log != null
533               && jalview.bin.Console.log instanceof JLoggerLog4j)
534       {
535         JLoggerLog4j.addAppender(jalview.bin.Console.log, jappender);
536       }
537     }
538   }
539
540   public synchronized void stopConsole()
541   {
542     quit = true;
543     this.notifyAll();
544     /*
545      * reader.notify(); reader2.notify(); if (errorThrower!=null)
546      * errorThrower.notify(); // stop all threads if (textAppender!=null)
547      * textAppender.notify();
548      */
549     if (pout != null)
550     {
551       try
552       {
553         reader.join(10);
554         pin.close();
555       } catch (Exception e)
556       {
557       }
558       try
559       {
560         reader2.join(10);
561         pin2.close();
562       } catch (Exception e)
563       {
564       }
565       try
566       {
567         textAppender.join(10);
568       } catch (Exception e)
569       {
570       }
571     }
572     /*
573     if (!frame.isVisible())
574     {
575       frame.dispose();
576     }
577     */
578     // System.exit(0);
579   }
580
581   @Override
582   public synchronized void windowClosed(WindowEvent evt)
583   {
584     frame.setVisible(false);
585     closeConsoleGui();
586   }
587
588   private void closeConsoleGui()
589   {
590     updateConsole = false;
591     if (parent == null)
592     {
593
594       stopConsole();
595     }
596     else
597     {
598       parent.showConsole(false);
599     }
600   }
601
602   @Override
603   public synchronized void windowClosing(WindowEvent evt)
604   {
605     frame.setVisible(false); // default behaviour of JFrame
606     closeConsoleGui();
607
608     // frame.dispose();
609   }
610
611   @Override
612   public synchronized void actionPerformed(ActionEvent evt)
613   {
614     trimBuffer(true);
615     // textArea.setText("");
616   }
617
618   @Override
619   public synchronized void run()
620   {
621     try
622     {
623       while (Thread.currentThread() == reader)
624       {
625         if (pin == null || pin.available() == 0)
626         {
627           try
628           {
629             this.wait(100);
630             if (pin.available() == 0)
631             {
632               trimBuffer(false);
633             }
634           } catch (InterruptedException ie)
635           {
636           }
637         }
638
639         while (pin.available() != 0)
640         {
641           String input = this.readLine(pin);
642           stdout.print(input);
643           long time = System.nanoTime();
644           appendToTextArea(input);
645           // stderr.println("Time taken to stdout append:\t"
646           // + (System.nanoTime() - time) + " ns");
647           // lines++;
648         }
649         if (quit)
650         {
651           return;
652         }
653       }
654
655       while (Thread.currentThread() == reader2)
656       {
657         if (pin2.available() == 0)
658         {
659           try
660           {
661             this.wait(100);
662             if (pin2.available() == 0)
663             {
664               trimBuffer(false);
665             }
666           } catch (InterruptedException ie)
667           {
668           }
669         }
670         while (pin2.available() != 0)
671         {
672           String input = this.readLine(pin2);
673           stderr.print(input);
674           long time = System.nanoTime();
675           appendToTextArea(input);
676           // stderr.println("Time taken to stderr append:\t"
677           // + (System.nanoTime() - time) + " ns");
678           // lines++;
679         }
680         if (quit)
681         {
682           return;
683         }
684       }
685       while (Thread.currentThread() == textAppender)
686       {
687         if (updateConsole)
688         {
689           // check string buffer - if greater than console, clear console and
690           // replace with last segment of content, otherwise, append all to
691           // content.
692           long count;
693           while (displayPipe.length() > 0)
694           {
695             count = 0;
696             StringBuffer tmp = new StringBuffer(), replace;
697             synchronized (displayPipe)
698             {
699               replace = displayPipe;
700               displayPipe = tmp;
701             }
702             // simply append whole buffer
703             textArea.append(replace.toString());
704             count += replace.length();
705             if (count > byteslim)
706             {
707               trimBuffer(false);
708             }
709           }
710           if (displayPipe.length() == 0)
711           {
712             try
713             {
714               this.wait(100);
715               if (displayPipe.length() == 0)
716               {
717                 trimBuffer(false);
718               }
719             } catch (InterruptedException e)
720             {
721             }
722           }
723         }
724         else
725         {
726           try
727           {
728             this.wait(100);
729           } catch (InterruptedException e)
730           {
731
732           }
733         }
734         if (quit)
735         {
736           return;
737         }
738
739       }
740     } catch (Exception e)
741     {
742       textArea.append("\nConsole reports an Internal error.");
743       textArea.append("The error is: " + e.getMessage());
744       // Need to uncomment this to ensure that line tally is synched.
745       // lines += 2;
746       stderr.println(
747               "Console reports an Internal error.\nThe error is: " + e);
748     }
749
750     // just for testing (Throw a Nullpointer after 1 second)
751     if (Thread.currentThread() == errorThrower)
752     {
753       try
754       {
755         this.wait(1000);
756       } catch (InterruptedException ie)
757       {
758       }
759       throw new NullPointerException(
760               MessageManager.getString("exception.application_test_npe"));
761     }
762   }
763
764   private void appendToTextArea(final String input)
765   {
766     if (updateConsole == false)
767     {
768       // do nothing;
769       return;
770     }
771     long time = System.nanoTime();
772     javax.swing.SwingUtilities.invokeLater(new Runnable()
773     {
774       @Override
775       public void run()
776       {
777         displayPipe.append(input); // change to stringBuffer
778         // displayPipe.flush();
779
780       }
781     });
782     // stderr.println("Time taken to Spawnappend:\t" + (System.nanoTime() -
783     // time)
784     // + " ns");
785   }
786
787   private String header = null;
788
789   private boolean updateConsole = false;
790
791   private synchronized void trimBuffer(boolean clear)
792   {
793     if (header == null && textArea.getLineCount() > 5)
794     {
795       try
796       {
797         header = textArea.getText(0, textArea.getLineStartOffset(5))
798                 + "\nTruncated...\n";
799       } catch (Exception e)
800       {
801         e.printStackTrace();
802       }
803     }
804     // trim the buffer
805     int tlength = textArea.getDocument().getLength();
806     if (header != null)
807     {
808       if (clear || (tlength > byteslim))
809       {
810         try
811         {
812           if (!clear)
813           {
814             long time = System.nanoTime();
815             textArea.replaceRange(header, 0, tlength - bytescut);
816             // stderr.println("Time taken to cut:\t"
817             // + (System.nanoTime() - time) + " ns");
818           }
819           else
820           {
821             textArea.setText(header);
822           }
823         } catch (Exception e)
824         {
825           e.printStackTrace();
826         }
827         // lines = textArea.getLineCount();
828       }
829     }
830
831   }
832
833   public synchronized String readLine(PipedInputStream in)
834           throws IOException
835   {
836     String input = "";
837     int lp = -1;
838     do
839     {
840       int available = in.available();
841       if (available == 0)
842       {
843         break;
844       }
845       byte b[] = new byte[available];
846       in.read(b);
847       input = input + new String(b, 0, b.length);
848       // counts lines - we don't do this for speed.
849       // while ((lp = input.indexOf("\n", lp + 1)) > -1)
850       // {
851       // lines++;
852       // }
853     } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
854     return input;
855   }
856
857   /**
858    * @j2sIgnore
859    * @param arg
860    */
861   public static void main(String[] arg)
862   {
863     new Console().test(); // create console with not reference
864
865   }
866
867   public void setVisible(boolean selected)
868   {
869     frame.setVisible(selected);
870     if (selected == true)
871     {
872       setChosenLogLevelCombo();
873       redirectStreams();
874       updateConsole = true;
875       frame.toFront();
876     }
877     else
878     {
879       // reset log level to what it was before
880       if (jalview.bin.Console.log != null)
881       {
882         jalview.bin.Console.log.setLevel(startingLogLevel);
883       }
884
885       unredirectStreams();
886       updateConsole = false;
887     }
888   }
889
890   public Rectangle getBounds()
891   {
892     if (frame != null)
893     {
894       return frame.getBounds();
895     }
896     return null;
897   }
898
899   /**
900    * set the banner that appears at the top of the console output
901    * 
902    * @param string
903    */
904   public void setHeader(String string)
905   {
906     header = string;
907     if (header.charAt(header.length() - 1) != '\n')
908     {
909       header += "\n";
910     }
911     textArea.insert(header, 0);
912   }
913
914   /**
915    * get the banner
916    * 
917    * @return
918    */
919   public String getHeader()
920   {
921     return header;
922   }
923 }