Home | History | Annotate | Download | only in gui
      1 /*
      2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
      3  *             of Java bytecode.
      4  *
      5  * Copyright (c) 2002-2014 Eric Lafortune (eric (at) graphics.cornell.edu)
      6  *
      7  * This program is free software; you can redistribute it and/or modify it
      8  * under the terms of the GNU General Public License as published by the Free
      9  * Software Foundation; either version 2 of the License, or (at your option)
     10  * any later version.
     11  *
     12  * This program is distributed in the hope that it will be useful, but WITHOUT
     13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
     15  * more details.
     16  *
     17  * You should have received a copy of the GNU General Public License along
     18  * with this program; if not, write to the Free Software Foundation, Inc.,
     19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
     20  */
     21 package proguard.gui;
     22 
     23 import javax.swing.*;
     24 import java.io.*;
     25 
     26 
     27 /**
     28  * This <code>PrintStream</code> appends its output to a given text area.
     29  *
     30  * @author Eric Lafortune
     31  */
     32 final class TextAreaOutputStream extends FilterOutputStream implements Runnable
     33 {
     34     private final JTextArea textArea;
     35 
     36 
     37     public TextAreaOutputStream(JTextArea textArea)
     38     {
     39         super(new ByteArrayOutputStream());
     40 
     41         this.textArea = textArea;
     42     }
     43 
     44 
     45     // Implementation for FilterOutputStream.
     46 
     47     public void flush() throws IOException
     48     {
     49         super.flush();
     50 
     51         try
     52         {
     53             // Append the accumulated buffer contents to the text area.
     54             SwingUtil.invokeAndWait(this);
     55         }
     56         catch (Exception e)
     57         {
     58             // Nothing.
     59         }
     60     }
     61 
     62 
     63     // Implementation for Runnable.
     64 
     65     public void run()
     66     {
     67         ByteArrayOutputStream out = (ByteArrayOutputStream)super.out;
     68 
     69         // Has any new text been written?
     70         String text = out.toString();
     71         if (text.length() > 0)
     72         {
     73             // Append the accumulated text to the text area.
     74             textArea.append(text);
     75 
     76             // Clear the buffer.
     77             out.reset();
     78         }
     79     }
     80 }
     81