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