Home | History | Annotate | Download | only in gui
      1 /*
      2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
      3  *             of Java bytecode.
      4  *
      5  * Copyright (c) 2002-2009 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 proguard.*;
     24 
     25 import javax.swing.*;
     26 import java.awt.*;
     27 import java.io.PrintStream;
     28 
     29 
     30 /**
     31  * This <code>Runnable</code> runs ProGuard, sending console output to a text
     32  * area and any exceptions to message dialogs.
     33  *
     34  * @see ProGuard
     35  * @author Eric Lafortune
     36  */
     37 final class ProGuardRunnable implements Runnable
     38 {
     39     private final JTextArea     consoleTextArea;
     40     private final Configuration configuration;
     41     private final String        configurationFileName;
     42 
     43 
     44     /**
     45      * Creates a new ProGuardRunnable object.
     46      * @param consoleTextArea       the text area to send the console output to.
     47      * @param configuration         the ProGuard configuration.
     48      * @param configurationFileName the optional file name of the configuration,
     49      *                              for informational purposes.
     50      */
     51     public ProGuardRunnable(JTextArea     consoleTextArea,
     52                             Configuration configuration,
     53                             String        configurationFileName)
     54     {
     55         this.consoleTextArea       = consoleTextArea;
     56         this.configuration         = configuration;
     57         this.configurationFileName = configurationFileName;
     58     }
     59 
     60 
     61     // Implementation for Runnable.
     62 
     63     public void run()
     64     {
     65         consoleTextArea.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
     66         consoleTextArea.setText("");
     67 
     68         // Redirect the System's out and err streams to the console text area.
     69         PrintStream oldOut = System.out;
     70         PrintStream oldErr = System.err;
     71 
     72         PrintStream printStream =
     73             new PrintStream(new TextAreaOutputStream(consoleTextArea), true);
     74 
     75         System.setOut(printStream);
     76         System.setErr(printStream);
     77 
     78         try
     79         {
     80             // Create a new ProGuard object with the GUI's configuration.
     81             ProGuard proGuard = new ProGuard(configuration);
     82 
     83             // Run it.
     84             proGuard.execute();
     85 
     86             // Print out the completion message.
     87             System.out.println("Processing completed successfully");
     88         }
     89         catch (Exception ex)
     90         {
     91             //ex.printStackTrace();
     92 
     93             // Print out the exception message.
     94             System.out.println(ex.getMessage());
     95 
     96             // Show a dialog as well.
     97             MessageDialogRunnable.showMessageDialog(consoleTextArea,
     98                                                     ex.getMessage(),
     99                                                     msg("errorProcessing"),
    100                                                     JOptionPane.ERROR_MESSAGE);
    101         }
    102         catch (OutOfMemoryError er)
    103         {
    104             // Forget about the ProGuard object as quickly as possible.
    105             System.gc();
    106 
    107             // Print out a message suggesting what to do next.
    108             System.out.println(msg("outOfMemoryInfo", configurationFileName));
    109 
    110             // Show a dialog as well.
    111             MessageDialogRunnable.showMessageDialog(consoleTextArea,
    112                                                     msg("outOfMemory"),
    113                                                     msg("errorProcessing"),
    114                                                     JOptionPane.ERROR_MESSAGE);
    115         }
    116         finally
    117         {
    118             // Make sure all output has been sent to the console text area.
    119             printStream.close();
    120 
    121             // Restore the old System's out and err streams.
    122             System.setOut(oldOut);
    123             System.setErr(oldErr);
    124         }
    125 
    126         consoleTextArea.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    127 
    128         // Reset the global static redirection lock.
    129         ProGuardGUI.systemOutRedirected = false;
    130     }
    131 
    132 
    133     // Small utility methods.
    134 
    135     /**
    136      * Returns the message from the GUI resources that corresponds to the given
    137      * key.
    138      */
    139     private String msg(String messageKey)
    140     {
    141          return GUIResources.getMessage(messageKey);
    142     }
    143 
    144 
    145     /**
    146      * Returns the message from the GUI resources that corresponds to the given
    147      * key and argument.
    148      */
    149     private String msg(String messageKey,
    150                        Object messageArgument)
    151     {
    152          return GUIResources.getMessage(messageKey, new Object[] {messageArgument});
    153     }
    154 }
    155