KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > decompiler > Window


1 /* Window Copyright (C) 1999-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; see the file COPYING.LESSER. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: Window.java,v 4.3.2.1 2002/05/28 17:34:03 hoenicke Exp $
18  */

19
20 package jode.decompiler;
21 import java.applet.*;
22 import java.awt.*;
23 ///#ifndef AWT10
24
import java.awt.event.*;
25 ///#endif
26
import java.io.*;
27 import jode.GlobalOptions;
28
29 public class Window
30     implements Runnable JavaDoc
31 ///#ifndef AWT10
32
, ActionListener
33 ///#endif
34

35 {
36     TextField classpathField, classField;
37     TextArea sourcecodeArea, errorArea;
38     Checkbox verboseCheck, prettyCheck;
39     Button startButton, saveButton;
40     String JavaDoc lastClassName, lastClassPath;
41     Frame frame;
42
43     PrintWriter errStream;
44     Decompiler decompiler = new Decompiler();
45     Thread JavaDoc decompileThread;
46     
47     public Window(Container window) {
48     buildComponents(window);
49     }
50
51     private void buildComponents(Container window) {
52     if (window instanceof Frame)
53         frame = (Frame) window;
54
55     window.setFont(new Font("dialog", Font.PLAIN, 10));
56
57     classpathField = new TextField(50);
58     classField = new TextField(50);
59     sourcecodeArea = new TextArea(20, 80);
60     errorArea = new TextArea(3, 80);
61     verboseCheck = new Checkbox("verbose", true);
62     prettyCheck = new Checkbox("pretty", true);
63     startButton = new Button("start");
64     saveButton = new Button("save");
65 ///#ifdef AWT10
66
/// saveButton.disable();
67
///#else
68
saveButton.setEnabled(false);
69 ///#endif
70

71     sourcecodeArea.setEditable(false);
72     errorArea.setEditable(false);
73     Font monospaced = new Font("monospaced", Font.PLAIN, 10);
74     sourcecodeArea.setFont(monospaced);
75     errorArea.setFont(monospaced);
76
77     GridBagLayout gbl = new GridBagLayout();
78     window.setLayout(gbl);
79     GridBagConstraints labelConstr = new GridBagConstraints();
80     GridBagConstraints textConstr = new GridBagConstraints();
81     GridBagConstraints areaConstr = new GridBagConstraints();
82     GridBagConstraints checkConstr = new GridBagConstraints();
83     GridBagConstraints buttonConstr = new GridBagConstraints();
84     labelConstr.fill = GridBagConstraints.NONE;
85     textConstr.fill = GridBagConstraints.HORIZONTAL;
86     areaConstr.fill = GridBagConstraints.BOTH;
87     checkConstr.fill = GridBagConstraints.NONE;
88     buttonConstr.fill = GridBagConstraints.NONE;
89     labelConstr.anchor = GridBagConstraints.EAST;
90     textConstr.anchor = GridBagConstraints.CENTER;
91     checkConstr.anchor = GridBagConstraints.WEST;
92     buttonConstr.anchor = GridBagConstraints.CENTER;
93     labelConstr.anchor = GridBagConstraints.EAST;
94     textConstr.gridwidth = GridBagConstraints.REMAINDER;
95     textConstr.weightx = 1.0;
96     areaConstr.gridwidth = GridBagConstraints.REMAINDER;
97     areaConstr.weightx = 1.0;
98     areaConstr.weighty = 1.0;
99
100 ///#ifdef AWT10
101
/// Label label = new Label("class path: ");
102
/// gbl.setConstraints(label, labelConstr);
103
/// window.add(label);
104
/// gbl.setConstraints(classpathField, textConstr);
105
/// window.add(classpathField);
106
/// label = new Label("class name: ");
107
/// gbl.setConstraints(label, labelConstr);
108
/// window.add(label);
109
/// gbl.setConstraints(classField, textConstr);
110
/// window.add(classField);
111
/// gbl.setConstraints(verboseCheck, checkConstr);
112
/// window.add(verboseCheck);
113
/// gbl.setConstraints(prettyCheck, checkConstr);
114
/// window.add(prettyCheck);
115
/// labelConstr.weightx = 1.0;
116
/// label = new Label();
117
/// gbl.setConstraints(label, labelConstr);
118
/// window.add(label);
119
/// gbl.setConstraints(startButton, buttonConstr);
120
/// window.add(startButton);
121
/// buttonConstr.gridwidth = GridBagConstraints.REMAINDER;
122
/// gbl.setConstraints(saveButton, buttonConstr);
123
/// window.add(saveButton);
124
/// gbl.setConstraints(sourcecodeArea, areaConstr);
125
/// window.add(sourcecodeArea);
126
/// areaConstr.gridheight = GridBagConstraints.REMAINDER;
127
/// areaConstr.weighty = 0.0;
128
/// gbl.setConstraints(errorArea, areaConstr);
129
/// window.add(errorArea);
130
///#else
131
window.add(new Label("class path: "), labelConstr);
132     window.add(classpathField, textConstr);
133     window.add(new Label("class name: "), labelConstr);
134     window.add(classField, textConstr);
135     window.add(verboseCheck, checkConstr);
136     window.add(prettyCheck, checkConstr);
137     labelConstr.weightx = 1.0;
138     window.add(new Label(), labelConstr);
139     window.add(startButton, buttonConstr);
140     buttonConstr.gridwidth = GridBagConstraints.REMAINDER;
141     window.add(saveButton, buttonConstr);
142     window.add(sourcecodeArea, areaConstr);
143     areaConstr.gridheight = GridBagConstraints.REMAINDER;
144     areaConstr.weighty = 0.0;
145     window.add(errorArea, areaConstr);
146
147     startButton.addActionListener(this);
148     saveButton.addActionListener(this);
149 ///#endif
150
errStream = new PrintWriter(new AreaWriter(errorArea));
151     decompiler.setErr(errStream);
152     }
153
154     public void setClassPath(String JavaDoc cp) {
155     classpathField.setText(cp);
156     }
157     public void setClass(String JavaDoc cls) {
158     classField.setText(cls);
159     }
160     
161 ///#ifdef AWT10
162
/// public synchronized void action(Event e, Object target) {
163
///#else
164
public synchronized void actionPerformed(ActionEvent e) {
165     Object JavaDoc target = e.getSource();
166 ///#endif
167
if (target == startButton) {
168
169 ///#ifdef AWT10
170
/// startButton.disable();
171
///#else
172
startButton.setEnabled(false);
173 ///#endif
174
decompileThread = new Thread JavaDoc(this);
175         sourcecodeArea.setText("Please wait, while decompiling...\n");
176         decompileThread.start();
177     } else if (target == saveButton) {
178         if (frame == null)
179         frame = new Frame(); //XXX
180
FileDialog fd = new FileDialog(frame,
181                        "Save decompiled code",
182                        FileDialog.SAVE);
183         fd.setFile(lastClassName.substring
184                (lastClassName.lastIndexOf('.')+1).concat(".java"));
185         fd.show();
186         String JavaDoc fileName = fd.getFile();
187         if (fileName == null)
188         return;
189         try {
190         File f = new File(new File(fd.getDirectory()), fileName);
191         FileWriter out = new FileWriter(f);
192         out.write(sourcecodeArea.getText());
193         out.close();
194         } catch (IOException ex) {
195         errorArea.setText("");
196         errStream.println("Couldn't write to file "
197                        + fileName + ": ");
198         ex.printStackTrace(errStream);
199         } catch (SecurityException JavaDoc ex) {
200         errorArea.setText("");
201         errStream.println("Couldn't write to file "
202                        + fileName + ": ");
203         ex.printStackTrace(errStream);
204         }
205     }
206     }
207
208     public class AreaWriter extends Writer {
209     boolean initialized = false;
210     private TextArea area;
211
212     public AreaWriter(TextArea a) {
213         area = a;
214     }
215
216     public void write(char[] b, int off, int len) throws IOException {
217         if (!initialized) {
218         area.setText("");
219         initialized = true;
220         }
221 ///#ifdef AWT10
222
/// area.appendText(new String(b, off, len));
223
///#else
224
area.append(new String JavaDoc(b, off, len));
225 ///#endif
226
}
227
228     public void flush() {
229     }
230
231     public void close() {
232     }
233     }
234
235     public void run() {
236     decompiler.setOption("verbose", verboseCheck.getState() ? "1" : "0");
237     decompiler.setOption("pretty", prettyCheck.getState() ? "1" : "0");
238     errorArea.setText("");
239 ///#ifdef AWT10
240
/// saveButton.disable();
241
///#else
242
saveButton.setEnabled(false);
243 ///#endif
244

245     lastClassName = classField.getText();
246     String JavaDoc newClassPath = classpathField.getText();
247     if (!newClassPath.equals(lastClassPath)) {
248         decompiler.setClassPath(newClassPath);
249         lastClassPath = newClassPath;
250     }
251                     
252     try {
253         Writer writer
254         = new BufferedWriter(new AreaWriter(sourcecodeArea), 512);
255         try {
256         decompiler.decompile(lastClassName, writer, null);
257         } catch (IllegalArgumentException JavaDoc ex) {
258         sourcecodeArea.setText
259             ("`"+lastClassName+"' is not a class name.\n"
260              +"You have to give a full qualified classname "
261              +"with '.' as package delimiter \n"
262              +"and without .class ending.");
263         return;
264         }
265 ///#ifdef AWT10
266
/// saveButton.enable();
267
///#else
268
saveButton.setEnabled(true);
269 ///#endif
270
} catch (Throwable JavaDoc t) {
271         sourcecodeArea.setText("Didn't succeed.\n"
272                    +"Check the below area for more info.");
273         t.printStackTrace();
274     } finally {
275         synchronized(this) {
276         decompileThread = null;
277 ///#ifdef AWT10
278
/// startButton.enable();
279
///#else
280
startButton.setEnabled(true);
281 ///#endif
282
}
283     }
284     }
285
286     public static void main(String JavaDoc argv[]) {
287     Frame frame = new Frame(GlobalOptions.copyright);
288     Window win = new Window(frame);
289
290     String JavaDoc cp = System.getProperty("java.class.path");
291     if (cp != null)
292         win.setClassPath(cp.replace(File.pathSeparatorChar,
293                     Decompiler.altPathSeparatorChar));
294     String JavaDoc cls = win.getClass().getName();
295     win.setClass(cls);
296
297 ///#ifndef AWT10
298
frame.addWindowListener(new WindowAdapter() {
299         public void windowClosing(WindowEvent e) {
300         System.exit(0);
301         }
302     });
303 ///#endif
304
frame.pack();
305     frame.show();
306     }
307 }
308
Popular Tags