KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > gui > ProGuardRunnable


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@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 java.awt.*;
24 import java.io.*;
25
26 import javax.swing.*;
27
28 import proguard.*;
29
30
31 /**
32  * This <code>Runnable</code> runs ProGuard, sending console output to a text
33  * area and any exceptions to message dialogs.
34  *
35  * @see ProGuard
36  * @author Eric Lafortune
37  */

38 class ProGuardRunnable implements Runnable JavaDoc
39 {
40     private JTextArea consoleTextArea;
41     private Configuration configuration;
42     private String JavaDoc configurationFileName;
43
44
45     /**
46      * Creates a new ProGuardRunnable object.
47      * @param consoleTextArea the text area to send the console output to.
48      * @param configuration the ProGuard configuration.
49      * @param configurationFileName the optional file name of the configuration,
50      * for informational purposes.
51      */

52     public ProGuardRunnable(JTextArea consoleTextArea,
53                             Configuration configuration,
54                             String JavaDoc configurationFileName)
55     {
56         this.consoleTextArea = consoleTextArea;
57         this.configuration = configuration;
58         this.configurationFileName = configurationFileName;
59     }
60
61
62     // Implementation for Runnable.
63

64     public void run()
65     {
66         consoleTextArea.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
67         consoleTextArea.setText("");
68
69         // Redirect the System's out and err streams to the console text area.
70
PrintStream oldOut = System.out;
71         PrintStream oldErr = System.err;
72
73         PrintStream printStream =
74             new PrintStream(new TextAreaOutputStream(consoleTextArea), true);
75
76         System.setOut(printStream);
77         System.setErr(printStream);
78
79         try
80         {
81             // Create a new ProGuard object with the GUI's configuration.
82
ProGuard proGuard = new ProGuard(configuration);
83
84             // Run it.
85
proGuard.execute();
86
87             // Print out the completion message.
88
System.out.println("Processing completed successfully");
89         }
90         catch (Exception JavaDoc ex)
91         {
92             //ex.printStackTrace();
93

94             // Print out the exception message.
95
System.out.println(ex.getMessage());
96
97             // Show a dialog as well.
98
MessageDialogRunnable.showMessageDialog(consoleTextArea,
99                                                     ex.getMessage(),
100                                                     msg("errorProcessing"),
101                                                     JOptionPane.ERROR_MESSAGE);
102         }
103         catch (OutOfMemoryError JavaDoc er)
104         {
105             // Forget about the ProGuard object as quickly as possible.
106
System.gc();
107
108             // Print out a message suggesting what to do next.
109
System.out.println(msg("outOfMemoryInfo", configurationFileName));
110
111             // Show a dialog as well.
112
MessageDialogRunnable.showMessageDialog(consoleTextArea,
113                                                     msg("outOfMemory"),
114                                                     msg("errorProcessing"),
115                                                     JOptionPane.ERROR_MESSAGE);
116         }
117
118         // Make sure all output has been sent to the console text area.
119
printStream.flush();
120
121         // Restore the old System's out and err streams.
122
System.setOut(oldOut);
123         System.setErr(oldErr);
124
125         consoleTextArea.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
126
127         // Reset the global static redirection lock.
128
ProGuardGUI.systemOutRedirected = false;
129     }
130
131
132     // Small utility methods.
133

134     /**
135      * Returns the message from the GUI resources that corresponds to the given
136      * key.
137      */

138     private String JavaDoc msg(String JavaDoc messageKey)
139     {
140          return GUIResources.getMessage(messageKey);
141     }
142
143
144     /**
145      * Returns the message from the GUI resources that corresponds to the given
146      * key and argument.
147      */

148     private String JavaDoc msg(String JavaDoc messageKey,
149                        Object JavaDoc messageArgument)
150     {
151          return GUIResources.getMessage(messageKey, new Object JavaDoc[] {messageArgument});
152     }
153 }
154
Popular Tags