Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 13:01:17 GMT
  2. Package jalview.gui

File Console.java

 

Coverage histogram

../../img/srcFileCovDistChart6.png
36% of files have more coverage

Code metrics

102
308
34
1
936
727
113
0.37
9.06
34
3.32

Classes

Class Line # Actions
Console 74 308 113
0.513513551.4%
 

Contributing tests

This file is covered by 256 tests. .

Source view

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