~ubuntu-branches/ubuntu/breezy/proguard/breezy

« back to all changes in this revision

Viewing changes to src/proguard/gui/ProGuardRunnable.java

  • Committer: Bazaar Package Importer
  • Author(s): Sam Clegg
  • Date: 2005-06-17 14:25:24 UTC
  • Revision ID: james.westby@ubuntu.com-20050617142524-thz2yfa3vemy3j9d
Tags: upstream-3.2
ImportĀ upstreamĀ versionĀ 3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: ProGuardRunnable.java,v 1.5 2004/08/15 12:39:30 eric Exp $
 
2
 *
 
3
 * ProGuard -- shrinking, optimization, and obfuscation of Java class files.
 
4
 *
 
5
 * Copyright (c) 2002-2004 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
 
39
{
 
40
    private JTextArea     consoleTextArea;
 
41
    private Configuration configuration;
 
42
    private String        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        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
        catch (Exception ex)
 
88
        {
 
89
            // Print out the exception message.
 
90
            System.out.println(ex.getMessage());
 
91
 
 
92
            // Show a dialog as well.
 
93
            MessageDialogRunnable.showMessageDialog(consoleTextArea,
 
94
                                                    ex.getMessage(),
 
95
                                                    msg("errorProcessing"),
 
96
                                                    JOptionPane.ERROR_MESSAGE);
 
97
        }
 
98
        catch (OutOfMemoryError er)
 
99
        {
 
100
            // Forget about the ProGuard object as quickly as possible.
 
101
            System.gc();
 
102
 
 
103
            // Print out a message suggesting what to do next.
 
104
            System.out.println(msg("outOfMemoryInfo", configurationFileName));
 
105
 
 
106
            // Show a dialog as well.
 
107
            MessageDialogRunnable.showMessageDialog(consoleTextArea,
 
108
                                                    msg("outOfMemory"),
 
109
                                                    msg("errorProcessing"),
 
110
                                                    JOptionPane.ERROR_MESSAGE);
 
111
        }
 
112
 
 
113
        // Make sure all output has been sent to the console text area.
 
114
        printStream.flush();
 
115
 
 
116
        // Restore the old System's out and err streams.
 
117
        System.setOut(oldOut);
 
118
        System.setErr(oldErr);
 
119
 
 
120
        consoleTextArea.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
 
121
 
 
122
        // Reset the global static redirection lock.
 
123
        ProGuardGUI.systemOutRedirected = false;
 
124
    }
 
125
 
 
126
 
 
127
    // Small utility methods.
 
128
 
 
129
    /**
 
130
     * Returns the message from the GUI resources that corresponds to the given
 
131
     * key.
 
132
     */
 
133
    private String msg(String messageKey)
 
134
    {
 
135
         return GUIResources.getMessage(messageKey);
 
136
    }
 
137
 
 
138
 
 
139
    /**
 
140
     * Returns the message from the GUI resources that corresponds to the given
 
141
     * key and argument.
 
142
     */
 
143
    private String msg(String messageKey,
 
144
                       Object messageArgument)
 
145
    {
 
146
         return GUIResources.getMessage(messageKey, new Object[] {messageArgument});
 
147
    }
 
148
}