1 19 20 package jode.decompiler; 21 import java.applet.*; 22 import java.awt.*; 23 import java.awt.event.*; 25 import java.io.*; 27 import jode.GlobalOptions; 28 29 public class Window 30 implements Runnable 31 , ActionListener 33 35 { 36 TextField classpathField, classField; 37 TextArea sourcecodeArea, errorArea; 38 Checkbox verboseCheck, prettyCheck; 39 Button startButton, saveButton; 40 String lastClassName, lastClassPath; 41 Frame frame; 42 43 PrintWriter errStream; 44 Decompiler decompiler = new Decompiler(); 45 Thread 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 saveButton.setEnabled(false); 69 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 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 errStream = new PrintWriter(new AreaWriter(errorArea)); 151 decompiler.setErr(errStream); 152 } 153 154 public void setClassPath(String cp) { 155 classpathField.setText(cp); 156 } 157 public void setClass(String cls) { 158 classField.setText(cls); 159 } 160 161 public synchronized void actionPerformed(ActionEvent e) { 165 Object target = e.getSource(); 166 if (target == startButton) { 168 169 startButton.setEnabled(false); 173 decompileThread = new Thread (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(); 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 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 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 area.append(new String (b, off, len)); 225 } 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 saveButton.setEnabled(false); 243 245 lastClassName = classField.getText(); 246 String 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 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 saveButton.setEnabled(true); 269 } catch (Throwable 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 startButton.setEnabled(true); 281 } 283 } 284 } 285 286 public static void main(String argv[]) { 287 Frame frame = new Frame(GlobalOptions.copyright); 288 Window win = new Window(frame); 289 290 String cp = System.getProperty("java.class.path"); 291 if (cp != null) 292 win.setClassPath(cp.replace(File.pathSeparatorChar, 293 Decompiler.altPathSeparatorChar)); 294 String cls = win.getClass().getName(); 295 win.setClass(cls); 296 297 frame.addWindowListener(new WindowAdapter() { 299 public void windowClosing(WindowEvent e) { 300 System.exit(0); 301 } 302 }); 303 frame.pack(); 305 frame.show(); 306 } 307 } 308 | Popular Tags |