0001: /*
0002: * ImageListener.java Copyright (c) 2006,07 Swaroop Belur
0003: *
0004: * This program is free software; you can redistribute it and/or
0005: * modify it under the terms of the GNU General Public License
0006: * as published by the Free Software Foundation; either version 2
0007: * of the License, or (at your option) any later version.
0008:
0009: * This program is distributed in the hope that it will be useful,
0010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012: * GNU General Public License for more details.
0013:
0014: * You should have received a copy of the GNU General Public License
0015: * along with this program; if not, write to the Free Software
0016: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017: *
0018: */
0019:
0020: package net.sf.jdec.ui.event;
0021:
0022: import java.awt.BorderLayout;
0023: import java.awt.Color;
0024: import java.awt.Component;
0025: import java.awt.event.ActionEvent;
0026: import java.awt.event.ActionListener;
0027: import java.awt.event.MouseAdapter;
0028: import java.awt.event.MouseEvent;
0029: import java.io.DataInputStream;
0030: import java.io.DataOutputStream;
0031: import java.io.File;
0032: import java.io.FileInputStream;
0033: import java.io.FileNotFoundException;
0034: import java.io.FileOutputStream;
0035: import java.io.FileReader;
0036: import java.io.FileWriter;
0037: import java.io.IOException;
0038: import java.util.ArrayList;
0039: import java.util.StringTokenizer;
0040:
0041: import javax.swing.BoxLayout;
0042: import javax.swing.ButtonGroup;
0043: import javax.swing.JButton;
0044: import javax.swing.JCheckBox;
0045: import javax.swing.JDialog;
0046: import javax.swing.JEditorPane;
0047: import javax.swing.JFileChooser;
0048: import javax.swing.JFrame;
0049: import javax.swing.JLabel;
0050: import javax.swing.JOptionPane;
0051: import javax.swing.JPanel;
0052: import javax.swing.JPopupMenu;
0053: import javax.swing.JScrollPane;
0054: import javax.swing.JTabbedPane;
0055: import javax.swing.JTextArea;
0056: import javax.swing.JViewport;
0057:
0058: import net.sf.jdec.format.Formatter;
0059: import net.sf.jdec.io.Writer;
0060: import net.sf.jdec.main.ConsoleLauncher;
0061: import net.sf.jdec.ui.adapter.DecompilerBridge;
0062: import net.sf.jdec.ui.config.UIConfig;
0063: import net.sf.jdec.ui.core.Console;
0064: import net.sf.jdec.ui.core.JdecTree;
0065: import net.sf.jdec.ui.core.Manager;
0066: import net.sf.jdec.ui.main.UILauncher;
0067: import net.sf.jdec.ui.util.ClassFileFilter;
0068: import net.sf.jdec.ui.util.FileOpener;
0069: import net.sf.jdec.ui.util.FindDialog;
0070: import net.sf.jdec.ui.util.JavaFileFilter;
0071: import net.sf.jdec.ui.util.LocalVariableWindow;
0072: import net.sf.jdec.ui.util.LogWriter;
0073: import net.sf.jdec.ui.util.Tips;
0074: import net.sf.jdec.ui.util.UIConstants;
0075: import net.sf.jdec.ui.util.UIUtil;
0076: import net.sf.jdec.ui.util.editor.TextEditor;
0077: import net.sf.jdec.config.Configuration;
0078:
0079: public class ImageListener implements ActionListener {
0080:
0081: public void actionPerformed(ActionEvent e) {
0082: try {
0083: Manager.auxlabel.setText("");
0084: if (e.getActionCommand().equalsIgnoreCase("Format")) {
0085: UIUtil.codeReformatted = true;
0086: //File file = UILauncher.getUIConfigRef().getCurrentOpFile();
0087:
0088: JTabbedPane tabs = UIUtil.getUIUtil()
0089: .getRightTabbedPane();
0090:
0091: if (tabs != null) {
0092: tabs.setSelectedIndex(tabs
0093: .indexOfTab("Decompiled Output"));
0094: Component op = tabs.getSelectedComponent();
0095: JScrollPane opTab = (JScrollPane) op;
0096: Object o = opTab.getComponent(0);
0097: JViewport view = (JViewport) o;
0098: Object o2 = view.getView();
0099: if (o2 != null) {
0100: JEditorPane rdwrPane = (JEditorPane) o2;
0101: Formatter formatter = new Formatter(rdwrPane
0102: .getText());
0103: rdwrPane.setText(formatter.getOutput());
0104: JOptionPane.showMessageDialog(null,
0105: "Formatting done...");
0106: }
0107: }
0108: return;
0109: }
0110: if (e.getActionCommand().equals("back")) {
0111: ArrayList list = UIUtil.decompiledClasses;
0112: int pos = UIUtil.historyindex;
0113: // System.out.println(pos);
0114: String prev = "";
0115: if (pos != -1 && pos != 0) {
0116: if (list.size() > 1) {
0117: prev = (String) list.get((pos - 1));
0118: UIUtil.historyindex = pos - 1;
0119: JTabbedPane pane = getRightTabbedPane();
0120: displayHistoryFile(pane, prev);
0121: }
0122: } else if (pos == -1) {
0123: if (list.size() > 1) {
0124: prev = (String) list.get((list.size() - 2));
0125: UIUtil.historyindex = list.size() - 2;
0126: JTabbedPane pane = getRightTabbedPane();
0127: displayHistoryFile(pane, prev);
0128: }
0129: } else {
0130: Manager.auxlabel
0131: .setText("Could Not Load Previous FIle");
0132: Manager.auxlabel.setForeground(Color.BLUE);
0133: Manager.auxlabel.setBackground(Color.WHITE);
0134: }
0135: return;
0136: }
0137: if (e.getActionCommand().equals("forward")) {
0138: ArrayList list = UIUtil.decompiledClasses;
0139: int pos = UIUtil.historyindex;
0140: // System.out.println(pos);
0141: if (pos != -1 && pos < (list.size() - 1)) {
0142: if (list.size() > 1) {
0143: String next = (String) list.get((pos + 1));
0144: UIUtil.historyindex = pos + 1;
0145: JTabbedPane pane = getRightTabbedPane();
0146: displayHistoryFile(pane, next);
0147: }
0148: } else {
0149: Manager.auxlabel
0150: .setText("Could Not Load Next FIle");
0151: Manager.auxlabel.setForeground(Color.BLUE);
0152: Manager.auxlabel.setBackground(Color.WHITE);
0153: }
0154:
0155: return;
0156:
0157: }
0158:
0159: if (e.getActionCommand().equals("RunJDec")) {
0160:
0161: Thread t1 = new Thread() {
0162:
0163: public void run() {
0164: try {
0165:
0166: if (!UIUtil.getUIUtil().getJDecOption()
0167: .equals("decompileJar")) // Not Jar
0168: {
0169:
0170: String filename = UILauncher
0171: .getUIutil().getJavaClassFile();
0172: File f = new File(filename);
0173:
0174: if (!f.exists()
0175: || (filename != null && !filename
0176: .trim().endsWith(
0177: ".class"))) {
0178: JOptionPane
0179: .showMessageDialog(
0180: UILauncher
0181: .getMainFrame(),
0182: "Chosen File is not a valid class file");
0183: Manager.getManager()
0184: .setShowProgressBar(false);
0185: return;
0186: }
0187:
0188: }
0189: if (UIUtil.getUIUtil().getJDecOption()
0190: .equals("decompileJar")) {
0191:
0192: String jarfilename = UILauncher
0193: .getUIutil().getJarFilePath();
0194: File f = new File(jarfilename);
0195:
0196: if (!f.exists()
0197: || (jarfilename != null && !isValidArchiveFileExt(jarfilename
0198: .trim()))) {
0199:
0200: JOptionPane
0201: .showMessageDialog(
0202: UILauncher
0203: .getMainFrame(),
0204: "Chosen File is not a valid archive file");
0205: Manager.getManager()
0206: .setShowProgressBar(false);
0207: return;
0208: }
0209:
0210: }
0211: Console c = UILauncher.getUIutil()
0212: .getConsoleFrame();
0213: if (c != null) {
0214: JEditorPane rdwr = c.getComponent();
0215: rdwr.setText("");
0216: String s = "Jdec Started..\n";
0217: s = rdwr.getText() + "\n\n" + s;
0218: rdwr.setText(s);
0219: }
0220: Manager.getManager().setShowProgressBar(
0221: true);
0222: DecompilerBridge bridge = DecompilerBridge
0223: .getInstance(UIUtil.getUIUtil());
0224:
0225: bridge.setOption(UIUtil.getUIUtil()
0226: .getJDecOption());
0227:
0228: if (c != null) {
0229: JEditorPane rdwr = c.getComponent();
0230: String s = "Current Task: "
0231: + UILauncher.getUIutil()
0232: .getJDecOption() + "\n";
0233: s += "Current File :"
0234: + UILauncher.getUIutil()
0235: .getJavaClassFile()
0236: + "\n";
0237: s = rdwr.getText() + "\n\n" + s;
0238: rdwr.setText(s);
0239: }
0240:
0241: if (UIUtil.getUIUtil().getJDecOption()
0242: .equals("decompileJar")) {
0243: // Configuration.setkFolderPath(Configuration.getOutputFolderPath()+File.separator+"JARDECOMPILED");
0244: UILauncher.getUIutil().setJarOption(
0245: true);
0246: }
0247:
0248: JTabbedPane tabs = getRightTabbedPane();
0249: int index = tabs
0250: .indexOfTab("Jdec Configuration");
0251: if (index != -1)
0252: tabs.remove(index);
0253:
0254: index = tabs.indexOfTab("Constant Pool");
0255: if (index != -1)
0256: tabs.remove(index);
0257: index = tabs.indexOfTab("Class Details");
0258: if (index != -1)
0259: tabs.remove(index);
0260: index = tabs.indexOfTab("Exception Tables");
0261: if (index != -1)
0262: tabs.remove(index);
0263: index = tabs.indexOfTab("Local Variables");
0264: if (index != -1)
0265: tabs.remove(index);
0266: index = tabs
0267: .indexOfTab("System Properties");
0268: if (index != -1)
0269: tabs.remove(index);
0270:
0271: // For LocalVariables...Special Handling
0272: if (UIUtil.getUIUtil().getJDecOption()
0273: .equals("localVariables")) {
0274:
0275: LocalVariableWindow lv = new LocalVariableWindow(
0276: "null");
0277:
0278: JTabbedPane rightTab = getRightTabbedPane();
0279: int i = rightTab
0280: .indexOfTab("Local Variables");
0281: if (i != -1)
0282: rightTab.remove(index);
0283: rightTab.addTab("Local Variables", lv);
0284: rightTab.setSelectedComponent(lv);
0285: Manager.getManager()
0286: .setShowProgressBar(false);
0287: return;
0288: }
0289:
0290: bridge.execute();
0291:
0292: if (c != null) {
0293: JEditorPane rdwr = c.getComponent();
0294: if (!UIUtil.getUIUtil().getJDecOption()
0295: .equals("decompileJar")) {
0296: String s = "Done..Please wait for Jdec UI to display the result...\n";
0297: s = rdwr.getText() + "\n\n" + s;
0298: rdwr.setText(s);
0299: } else {
0300: String s = "Done..Please check up the Jar Tab to verify the result\n";
0301: s = rdwr.getText() + "\n\n" + s;
0302: rdwr.setText(s);
0303: }
0304: }
0305: if (UIUtil.getUIUtil().getJDecOption()
0306: .equals("decompileJar") == false) {
0307: bridge
0308: .setOutputFiles(new File[] { new File(
0309: ConsoleLauncher
0310: .getResultFileName()) });
0311: bridge.showResult(getRightTabbedPane());
0312: ConsoleLauncher
0313: .setCurrentSourceFile(new File(
0314: UILauncher
0315: .getUIutil()
0316: .getJavaClassFile()));
0317: ConsoleLauncher
0318: .setCurrentDecompiledFile(UILauncher
0319: .getUIutil()
0320: .getJavaClassFile()); // Duplicate
0321: // Method...can
0322: // use
0323: // getCurrentSourceFile
0324: // itself...
0325: }
0326:
0327: } catch (Throwable t) // TO shut down the progress bar
0328: {
0329:
0330: Manager.getManager().setShowProgressBar(
0331: false);
0332: try {
0333: t.printStackTrace(Writer
0334: .getWriter("log"));
0335: } catch (IOException e) {
0336:
0337: }
0338: }
0339:
0340: }
0341: };
0342:
0343: Thread t2 = new Thread() {
0344:
0345: public void run() {
0346:
0347: JDialog proframe = UIUtil
0348: .launchProgressBarFrame();
0349: if (proframe != null) {
0350: proframe.setVisible(true);
0351: }
0352:
0353: while (Manager.getManager().isShowProgressBar() == true) {
0354:
0355: gotoSleep();
0356: }
0357:
0358: if (!Manager.getManager().isShowProgressBar()) {
0359: if (proframe != null) {
0360: proframe.setVisible(false);
0361: proframe.dispose();
0362: }
0363: proframe = null;
0364: if (ConsoleLauncher.fatalErrorOccured)
0365: JOptionPane
0366: .showMessageDialog(
0367: UILauncher
0368: .getMainFrame(),
0369: UIConstants.jdecTaskError,
0370: "Jdec Status",
0371: JOptionPane.INFORMATION_MESSAGE);
0372: }
0373:
0374: }
0375:
0376: };
0377:
0378: Manager.getManager().setWorkerThreadRef(t1);
0379: Manager.getManager().setStatusThreadRef(t2);
0380:
0381: if (UIUtil.checkForInvalidEntries("run")) {
0382: t1.setPriority(Thread.MAX_PRIORITY);
0383: t2.setPriority(Thread.NORM_PRIORITY);
0384: t1.start();
0385: t2.start();
0386: } else {
0387: JOptionPane
0388: .showMessageDialog(
0389: UILauncher.getMainFrame(),
0390: "Please Check The Decompiler Configuration Property Values Again",
0391: "JDEC WARNING",
0392: JOptionPane.INFORMATION_MESSAGE);
0393: }
0394:
0395: } else if (e.getActionCommand().equals("Save")) {
0396: JTabbedPane tb = getRightTabbedPane();
0397: int index = tb.getSelectedIndex();
0398: String s = UIUtil.getUIUtil().saveAsOrSave(index);
0399: if (s.equals("saveas"))
0400: saveAsWork();
0401: else {
0402: // saveWork();
0403: String file = UIUtil.getUIUtil().getFileNameForTab(
0404: index);
0405: Component editor = tb.getSelectedComponent();
0406: JScrollPane editorTab = (JScrollPane) editor;
0407: Object o = editorTab.getComponent(0);
0408: JViewport view = (JViewport) o;
0409: Object o2 = view.getView();
0410: if (o2 != null && file != null) {
0411: JEditorPane rdwrPane = (JEditorPane) o2;
0412: String content = rdwrPane.getText();
0413: File f = new File(file);
0414: FileOutputStream fos = new FileOutputStream(f);
0415: fos.write(content.getBytes());
0416: fos.flush();
0417: fos.close();
0418: fos = null;
0419: }
0420:
0421: }
0422:
0423: } else if (e.getActionCommand().equals("Home")) {
0424: // TODO
0425: } else if (e.getActionCommand().equals("Send Mail")) {
0426: // fixmelater : seems far fetched
0427: // original intention was to mail
0428: // decompiled file.Again something
0429: // fancy
0430:
0431: } else if (e.getActionCommand().equals("Search")) {
0432:
0433: FindDialog find = new FindDialog(UILauncher
0434: .getMainFrame());
0435: find.setVisible(true);
0436:
0437: } else if (e.getActionCommand().equals("Open")) {
0438: FileOpener opener = new FileOpener(
0439: new ClassFileFilter());
0440: Manager manager = Manager.getManager();
0441: ArrayList paneList = manager
0442: .getCurrentSplitPaneComponents();
0443: JTabbedPane tabs = UIUtil.getUIUtil()
0444: .getRightTabbedPane();
0445: if (tabs != null) {
0446: tabs.setSelectedIndex(tabs
0447: .indexOfTab("Jdec Editor Window"));
0448: int pos = tabs.getSelectedIndex();
0449: Component editor = tabs.getSelectedComponent();
0450: JScrollPane editorTab = (JScrollPane) editor;
0451: File file = opener.getSelectedFile();
0452: if (file == null)
0453: return;
0454: FileReader reader = null;
0455: try {
0456: reader = new FileReader(file);
0457: Object o = editorTab.getComponent(0);
0458: // System.out.println(o.getClass());
0459: JViewport view = (JViewport) o;
0460: Object o2 = view.getView();
0461: if (o2 != null) {
0462: JEditorPane rdwrPane = (JEditorPane) o2;
0463: UILauncher.getUIutil().addRecentFile(file);
0464: UILauncher.getUIutil().setCurrentOpenFile(
0465: file.getAbsolutePath());
0466: UILauncher.getUIutil().addToTabFileMap(pos,
0467: file.getAbsolutePath());
0468: rdwrPane.read(reader, null);
0469:
0470: }
0471: } catch (IOException ex) {
0472: JOptionPane.showMessageDialog(UILauncher
0473: .getMainFrame(), "File Not Found",
0474: "ERROR", JOptionPane.ERROR_MESSAGE);
0475: } finally {
0476: if (reader != null) {
0477: try {
0478: reader.close();
0479: } catch (IOException x) {
0480: }
0481: }
0482: }
0483:
0484: }
0485: } else if (e.getActionCommand().equals("FileSystem")) {
0486:
0487: // fixmelater No Need to implement actually
0488: } else if (e.getActionCommand().equals("Help")) {
0489:
0490: // fixmelater On Need basis
0491: } else if (e.getActionCommand().equals("NewTask")) {
0492: JPopupMenu popup = new JPopupMenu();
0493: JOptionPane option = new JOptionPane(
0494: "Please Specify Your Choice...",
0495: JOptionPane.INFORMATION_MESSAGE,
0496: JOptionPane.YES_NO_CANCEL_OPTION);
0497: // popup.add(option);
0498: final JFrame in = new JFrame(
0499: "New Decompiler Task Window");
0500: in.setResizable(true);
0501: // in.getContentPane().setLayout(new GridLayout(2, 4));
0502: JCheckBox Class = new JCheckBox("Decompile Class File");
0503: // JCheckBox Jar=new JCheckBox("Decompile Archive File");
0504: // JCheckBox classInJar=new JCheckBox("Class In Archive Task");
0505: JCheckBox Jar = new JCheckBox(
0506: "Decompile Archive File [*** Updated]");
0507: Jar.setActionCommand("Decompile Archive File");
0508: JCheckBox classInJar = new JCheckBox(
0509: "Class In Archive [*** New]");
0510: JCheckBox versions = new JCheckBox(
0511: "Decompile 2 versions of class File [*** New]");
0512: final String hintm = "Click on ....[for updating settings related to archive file task.]\n1>[Edit-->Archive settings...] \n\t2>Jar Level Filters[Configuration-->[Jdec(Decompiler) Filters]";
0513: JLabel archlbl = new JLabel(hintm);// "Click on [Edit-->Archive
0514: // settings...] for updating
0515: // settings related to
0516: // archive file task.");
0517:
0518: archlbl.setForeground(Color.BLUE);
0519: // JCheckBox process=new JCheckBox("Process Class File");
0520: // JCheckBox project=new JCheckBox("Create jdec project");
0521: ButtonGroup bg = new ButtonGroup();
0522: bg.add(Class);
0523: bg.add(Jar);
0524: bg.add(classInJar);
0525: bg.add(versions);
0526: // bg.add(process);
0527: // bg.add(project);
0528: JPanel basic = new JPanel();
0529: BoxLayout box = new BoxLayout(basic, BoxLayout.Y_AXIS);
0530: basic.setLayout(box);
0531: basic.add(Class);
0532: basic.add(Jar);
0533: basic.add(classInJar);
0534: basic.add(versions);
0535: JPanel pp = new JPanel();
0536: BoxLayout box2 = new BoxLayout(pp, BoxLayout.X_AXIS);
0537: pp.setLayout(box2);
0538: // pp.add(process);
0539: JLabel lab = new JLabel(" More Info");
0540: lab.setForeground(Color.BLUE);
0541: // lab.setFont(new Font("MONOSPACE",Font.BOLD,12));
0542: lab.addMouseListener(new MouseAdapter() {
0543: public void mouseClicked(MouseEvent e) {
0544: String desc = "This task will process the class file: \n";
0545: desc += "Namely constant pool and determine all methods,constructors\n";
0546: desc += "But will not decompile any method\n";
0547: desc += "User will be presented the method and field list as a tree structure\n";
0548: desc += "When the user chooses a method, then the method will be decompiled\n";
0549: desc += "\nHow will this option be useful?\n";
0550: desc += "If the user wishes to see the content of only one method\n";
0551: desc += "then it will be waste of time to decompile the entire class\n";
0552: JOptionPane.showMessageDialog(null, desc);
0553: }
0554: });
0555: // pp.add(lab);
0556: // BoxLayout box=new BoxLayout(in,BoxLayout.Y_AXIS);
0557: // in.getContentPane().setLayout(box);
0558: in.getContentPane().add(basic, BorderLayout.NORTH);
0559: in.getContentPane().add(pp, BorderLayout.CENTER);
0560: JPanel pjp = new JPanel();
0561: BoxLayout box3 = new BoxLayout(pjp, BoxLayout.X_AXIS);
0562:
0563: pjp.setLayout(box3);
0564: JLabel lab2 = new JLabel(" More Info");
0565: lab2.setForeground(Color.BLUE);
0566: // lab.setFont(new Font("MONOSPACE",Font.BOLD,12));
0567: lab2.addMouseListener(new MouseAdapter() {
0568: public void mouseClicked(MouseEvent e) {
0569: String desc = "This task will allow the user to select multiple \n";
0570: desc += "jars,zip files,folders containing class files\n";
0571: desc += "All jars will be expanded and presented as a tree structure\n";
0572: desc += "User can then choose any class file to decompile\n";
0573: desc += "\nwhats special about this option?\n";
0574: desc += "User can select the folder depth to choose jars/zips,class files\n";
0575: JOptionPane.showMessageDialog(null, desc);
0576: }
0577: });
0578: pjp.setLayout(box3);
0579: // intm="Click on ....\n1>[Edit-->Archive settings...] for
0580: // updating settings related to archive file task.\n\t2>Jar
0581: // Level Filters[Configuration-->[Jdec(Decompiler) Filters]";
0582: JButton hint = new JButton("Hint");
0583: JButton close = new JButton("Close");
0584: pjp.add(hint);
0585: hint.addActionListener(new ActionListener() {
0586:
0587: public void actionPerformed(ActionEvent ae) {
0588: JOptionPane.showMessageDialog(in, hintm);
0589:
0590: }
0591:
0592: });
0593: close.addActionListener(new ActionListener() {
0594:
0595: public void actionPerformed(ActionEvent ae) {
0596: in.setVisible(false);
0597: in.dispose();
0598: }
0599: });
0600: pjp.add(new JLabel(" "));
0601: pjp.add(close);
0602: // p.add(project);
0603: // pjp.add(lab2);
0604: in.getContentPane().add(pjp, BorderLayout.SOUTH);
0605:
0606: in.pack();
0607: in.setBounds(300, 300, 430, 190);
0608: NewTaskListener nwtsk = NewTaskListener.getInstance();
0609: Class.addActionListener(nwtsk);
0610: Jar.addActionListener(nwtsk);
0611: classInJar.setActionCommand("classinjar");
0612: classInJar.addActionListener(nwtsk);
0613: versions.setActionCommand("versions");
0614: versions.addActionListener(nwtsk);
0615: in.setVisible(true);
0616: UILauncher.addTempFrame(in);
0617: UILauncher.addChildRef(in);
0618:
0619: } else if (e.getActionCommand().equals("tech")) {
0620: // NO need to fix
0621:
0622: } else if (e.getActionCommand().equals("tip")) {
0623: Tips tips = new Tips();
0624:
0625: } else if (e.getActionCommand().equals("Stop")) {
0626: // fixmelater ... Will be good to implement
0627: // Like to stop a jar from being processed
0628: // If it takes too long a time or progressbar
0629: // gets into infinite loop
0630:
0631: } else if (e.getActionCommand().equals("print")) {
0632: // fixmelater .... More of an Editor feature..Depends
0633: } else if (e.getActionCommand().equals("editin")) {
0634: TextEditor texteditor = new TextEditor();
0635: texteditor.setVisible(true);
0636: JTabbedPane tab = getRightTabbedPane();
0637: Component c = tab.getSelectedComponent();
0638: JScrollPane pane = (JScrollPane) c;
0639: JViewport view = (JViewport) pane.getViewport();
0640: JEditorPane editor = (JEditorPane) view.getView();
0641: editor.selectAll();
0642: editor.copy();
0643: editor.paste();
0644:
0645: int counter = 1;
0646: JScrollPane scpane = texteditor.getTextComponent();
0647: JViewport jview = scpane.getViewport();
0648: JTextArea jtext = (JTextArea) jview.getView();
0649: jtext.paste();
0650:
0651: /***************************************************************
0652: * Clipboard clip=editor.getToolkit().getSystemClipboard();
0653: * Transferable tr=clip.getContents(null); if(tr!=null) {
0654: * DataFlavor[] fl=tr.getTransferDataFlavors() ; if(fl!=null) {
0655: * ArrayList temp=new ArrayList();
0656: *
0657: * for(int s=0;s<fl.length;s++) { if(fl[s].isFlavorTextType()) {
0658: * Object o=tr.getTransferData(fl[s]) ; if(o instanceof
0659: * java.lang.String) { // check whether already added
0660: * if(temp.size()==0)temp.add(o.toString()); else {
0661: * java.lang.String str[]=(String[])temp.toArray(new
0662: * String[]{}); Arrays.sort(str); for(int i=1;i<str.length;i++) {
0663: *
0664: * String content=str[i]; String prev=str[i-1];
0665: * if(content.equals(prev)) { } else { temp.add(o.toString());
0666: * //text.append(counter++ +"\t\t"+o.toString()+"\n"); } } }
0667: * } } }
0668: *
0669: * for(int y=0;y<temp.size();y++) {
0670: *
0671: * String s1=(String)temp.get(y); text.append(s1); }
0672: *
0673: * if(text.toString().length() > 0) { JScrollPane
0674: * scpane=texteditor.getTextComponent(); JViewport
0675: * jview=scpane.getViewport(); JEditorPane
0676: * jtext=(JEditorPane)jview.getView();
0677: * jtext.setText(text.toString()); } } }
0678: **************************************************************/
0679:
0680: } else if (e.getActionCommand().equals("close")) {
0681: try {
0682: UIConfig uiconfig = UILauncher.getUIConfigRef();
0683: uiconfig.persistToFile();
0684: UILauncher.closeChildWindows();
0685: } catch (IOException ioe) {
0686: // Ignore
0687: }
0688:
0689: int option = JOptionPane.showConfirmDialog(UILauncher
0690: .getMainFrame(), "Are You Sure?",
0691: "Close Jdec UI", JOptionPane.YES_NO_OPTION);
0692: if (option == JOptionPane.YES_OPTION) {
0693: boolean b = UILauncher.getUIutil()
0694: .hasConfigChanged();
0695: if (b) {
0696: int opt = JOptionPane
0697: .showConfirmDialog(
0698: UILauncher.getMainFrame(),
0699: "The Decompiler Configuration Has been updated...\nWould You Like to save the changes...",
0700: "Update Configuration ...?",
0701: JOptionPane.YES_NO_OPTION);
0702: if (opt == JOptionPane.YES_OPTION) {
0703:
0704: File src = new File(System
0705: .getProperty("user.home")
0706: + File.separator
0707: + "tempconfig.properties");
0708: File dest = new File("config.properties");
0709: UILauncher.getUIutil().copyFile(src, dest);
0710: }
0711: if (opt == JOptionPane.NO_OPTION) {
0712: UILauncher
0713: .getMainFrame()
0714: .setDefaultCloseOperation(
0715: UILauncher.getMainFrame().DO_NOTHING_ON_CLOSE);
0716: }
0717:
0718: if (opt == JOptionPane.CANCEL_OPTION) {
0719:
0720: }
0721:
0722: }
0723:
0724: JFrame frame = UILauncher.getMainFrame();
0725: frame.setVisible(false);
0726: frame.setEnabled(false);
0727: frame.dispose();
0728: }
0729:
0730: LogWriter lw = LogWriter.getInstance();
0731: if (lw != null) {
0732: lw.close();
0733: }
0734: }
0735:
0736: else if (e.getActionCommand().equals("clear")) {
0737: Manager manager = Manager.getManager();
0738: ArrayList paneList = manager
0739: .getCurrentSplitPaneComponents();
0740: Console c = null;
0741: for (int s = 0; s < paneList.size(); s++) {
0742: Object current = paneList.get(s);
0743: if (current instanceof Console) {
0744: c = (Console) current;
0745: break;
0746: }
0747:
0748: }
0749: Object o = c.getComponent();
0750:
0751: if (o != null) {
0752: JEditorPane rdwrPane = (JEditorPane) o;
0753: rdwrPane.setText("");
0754: }
0755:
0756: } else if (e.getActionCommand().equals("editor")) {
0757: TextEditor editor = new TextEditor();
0758: // System.out.println("Simple Editor"+editor);
0759: editor
0760: .setDefaultCloseOperation(editor.DISPOSE_ON_CLOSE);
0761: editor.setVisible(true);
0762:
0763: } else if (e.getActionCommand().equals("sound")) {
0764:
0765: // fixmelater ...Something fancy :) Depends again
0766: // The purpose was to use java sound API
0767: // TO sing out to the user the creators of the tool
0768: // Ha ha ha
0769:
0770: } else if (e.getActionCommand().equals("binary")) {
0771:
0772: FileOpener opener = new FileOpener(
0773: new ClassFileFilter());
0774: Manager manager = Manager.getManager();
0775: ArrayList paneList = manager
0776: .getCurrentSplitPaneComponents();
0777: JTabbedPane tabs = UIUtil.getUIUtil()
0778: .getRightTabbedPane();
0779: if (tabs != null) {
0780: tabs.setSelectedIndex(tabs
0781: .indexOfTab("Jdec Editor Window"));
0782: int pos = tabs.getSelectedIndex();
0783: Component editor = tabs.getSelectedComponent();
0784: // System.out.println(editor.getClass());
0785: JScrollPane editorTab = (JScrollPane) editor;
0786: File file = opener.getSelectedFile();
0787:
0788: if (file == null)
0789: return;
0790: FileReader reader = null;
0791: File temp = getBinaryContentForFile(file);
0792: if (temp == null) {
0793: throw new Exception();
0794: }
0795: try {
0796: reader = new FileReader(temp);
0797: Object o = editorTab.getComponent(0);
0798: // System.out.println(o.getClass());
0799: JViewport view = (JViewport) o;
0800: Object o2 = view.getView();
0801: if (o2 != null) {
0802: JEditorPane rdwrPane = (JEditorPane) o2;
0803: UILauncher.getUIutil().addRecentFile(file);
0804: UILauncher.getUIutil().setCurrentOpenFile(
0805: file.getAbsolutePath());
0806: UILauncher.getUIutil().addToTabFileMap(pos,
0807: file.getAbsolutePath());
0808: rdwrPane.read(reader, null);
0809:
0810: }
0811: } catch (IOException ex) {
0812: JOptionPane.showMessageDialog(UILauncher
0813: .getMainFrame(), "File Not Found",
0814: "ERROR", JOptionPane.ERROR_MESSAGE);
0815: } catch (Exception ex) {
0816: JOptionPane.showMessageDialog(UILauncher
0817: .getMainFrame(), "Could Not Load File",
0818: "ERROR", JOptionPane.ERROR_MESSAGE);
0819: } finally {
0820: if (reader != null) {
0821: try {
0822: reader.close();
0823: } catch (IOException x) {
0824: }
0825: }
0826: }
0827:
0828: }
0829:
0830: }
0831:
0832: else if (e.getActionCommand().equals("go")) {
0833:
0834: File path = UILauncher.getUIutil().getBrowserPath();
0835: try {
0836: Runtime rt = Runtime.getRuntime();
0837: Process p = rt.exec(path.getAbsolutePath() + " "
0838: + "http://jdec.sourceforge.net/");
0839:
0840: } catch (Throwable t) {
0841: JOptionPane
0842: .showMessageDialog(null,
0843: "Please set Browser Path first[Edit-->preferences]");
0844: try {
0845: LogWriter lg = LogWriter.getInstance();
0846: lg
0847: .writeLog("[ERROR]: Method: go\n\tClass: ImageListener.class");
0848: lg
0849: .writeLog("------------------------------------------------");
0850: lg.writeLog("Exception Stack Trace");
0851: t.printStackTrace(lg.getPrintWriter());
0852: lg.flush();
0853:
0854: } catch (Exception ex) {
0855:
0856: }
0857:
0858: }
0859:
0860: } else if (e.getActionCommand().equals("refresh")) {
0861: Thread t1 = new Thread() {
0862: public void run() {
0863: Manager.getManager().setShowProgressBar(true);
0864: Console c = UILauncher.getUIutil()
0865: .getConsoleFrame();
0866: if (c != null) {
0867: JEditorPane rdwr = c.getComponent();
0868: rdwr.setText("");
0869: String s = "Jdec Started..\n";
0870: s = rdwr.getText() + "\n\n" + s;
0871: rdwr.setText(s);
0872: }
0873: java.lang.String currentDecFile = ConsoleLauncher
0874: .getCurrentDecompiledFile();
0875: if (currentDecFile == null
0876: || currentDecFile.trim().length() == 0) {
0877: Manager.getManager().setShowProgressBar(
0878: false);
0879: JOptionPane.showMessageDialog(UILauncher
0880: .getMainFrame(),
0881: "No file To Refresh");
0882: return;
0883: }
0884: DecompilerBridge bridge = DecompilerBridge
0885: .getInstance(UIUtil.getUIUtil());
0886: bridge.setClassFile(currentDecFile);
0887:
0888: if (c != null) {
0889: JEditorPane rdwr = c.getComponent();
0890:
0891: String s = "Current Task : Refresh Class File ->"
0892: + currentDecFile;
0893: s = rdwr.getText() + "\n\n" + s;
0894: rdwr.setText(s);
0895: }
0896:
0897: bridge.execute("decompileClass", "", false,
0898: "null");
0899: if (c != null) {
0900: JEditorPane rdwr = c.getComponent();
0901:
0902: String s = "Done ...Please Wait For Jdec UI to Refresh the output";
0903: s = rdwr.getText() + "\n\n" + s;
0904: rdwr.setText(s);
0905: }
0906: JTabbedPane rightTab = getRightTabbedPane();
0907: bridge.showResult(rightTab);
0908: String simpleName = currentDecFile;
0909: int sep = simpleName
0910: .lastIndexOf(File.separator);
0911: if (sep != -1) {
0912: simpleName = simpleName.substring(sep + 1);
0913: }
0914: JOptionPane.showMessageDialog(UILauncher
0915: .getMainFrame(),
0916: "Refreshed Java Class File : "
0917: + simpleName);
0918:
0919: }
0920: };
0921: Thread t2 = new Thread() {
0922:
0923: public void run() {
0924:
0925: JDialog proframe = UIUtil
0926: .launchProgressBarFrame();
0927: if (proframe != null)
0928: proframe.setVisible(true);
0929: // proframe.requestFocusInWindow();
0930:
0931: while (Manager.getManager().isShowProgressBar() == true) {
0932:
0933: gotoSleep();
0934: }
0935:
0936: if (!Manager.getManager().isShowProgressBar()) {
0937: if (proframe != null) {
0938: proframe.setVisible(false);
0939: proframe.dispose();
0940: }
0941: proframe = null;
0942:
0943: }
0944:
0945: }
0946:
0947: };
0948: t1.setPriority(Thread.MAX_PRIORITY);
0949: Manager.getManager().setStatusThread(t1);
0950: t2.setPriority(Thread.NORM_PRIORITY);
0951: Manager.getManager().setShowProgressBar(true);
0952: t2.start();
0953: t1.start();
0954:
0955: } else if (e.getActionCommand().equals("jdec")) {
0956: // fixmelater..Will be good
0957: // The purpose was to educate the users
0958: // how a jdec works. Did not implement
0959: // for lack of time. THIS WILL BE USP OF THE TOOL
0960: // IF IMPLEMENTED. BUT HAS TO BE INNOVATIVE
0961:
0962: }
0963: } catch (Throwable t) {
0964: try {
0965: LogWriter lg = LogWriter.getInstance();
0966: lg
0967: .writeLog("[ERROR]: Method: actionPerformed\n\tClass:ImageListener.class");
0968: lg
0969: .writeLog("------------------------------------------------");
0970: lg.writeLog("Exception Stack Trace");
0971: t.printStackTrace(lg.getPrintWriter());
0972: lg.flush();
0973:
0974: } catch (Exception exp) {
0975:
0976: }
0977: }
0978:
0979: }
0980:
0981: private JTabbedPane getRightTabbedPane() {
0982: Manager manager = Manager.getManager();
0983: ArrayList paneList = manager.getCurrentSplitPaneComponents();
0984: JTabbedPane tabs = null;
0985: for (int s = 0; s < paneList.size(); s++) {
0986: Object current = paneList.get(s);
0987: if (current instanceof JTabbedPane) {
0988: tabs = (JTabbedPane) current;
0989: Component c = tabs.getComponent(0);
0990: if ((c instanceof JdecTree) == false) {
0991: if (tabs.getTabCount() > 2) {
0992: break;
0993: }
0994: }
0995:
0996: }
0997: }
0998:
0999: return tabs;
1000:
1001: }
1002:
1003: private void saveAsWork() {
1004: Manager manager = Manager.getManager();
1005: ArrayList paneList = manager.getCurrentSplitPaneComponents();
1006: JTabbedPane tabs = getRightTabbedPane();
1007: if (tabs != null) {
1008:
1009: Component editor = tabs.getSelectedComponent();
1010: JScrollPane editorTab = (JScrollPane) editor;
1011: JFileChooser chooser = new JFileChooser();
1012: JavaFileFilter filter = new JavaFileFilter();
1013: // chooser.setFileFilter(filter);
1014: if (chooser.showSaveDialog(UILauncher.getMainFrame()) != JFileChooser.APPROVE_OPTION)
1015: return;
1016: File file = chooser.getSelectedFile();
1017:
1018: if (file == null)
1019: return;
1020: FileWriter writer = null;
1021: try {
1022: String fname = file.getAbsolutePath();
1023: if (fname.endsWith(".java") == false) {
1024: file = new File(fname + ".java");
1025: }
1026: writer = new FileWriter(file);
1027: Object o = editorTab.getComponent(0);
1028: // System.out.println(o.getClass());
1029: JViewport view = (JViewport) o;
1030: Object o2 = view.getView();
1031: if (o2 != null) {
1032: JEditorPane rdwrPane = (JEditorPane) o2;
1033:
1034: rdwrPane.write(writer);
1035: UIUtil.getUIUtil().setFileTabMap(
1036: tabs.getSelectedIndex(),
1037: file.getAbsolutePath());
1038: }
1039: } catch (IOException ex) {
1040: JOptionPane.showMessageDialog(
1041: UILauncher.getMainFrame(), "File Not Saved",
1042: "ERROR", JOptionPane.ERROR_MESSAGE);
1043: } finally {
1044: if (writer != null) {
1045: try {
1046: writer.close();
1047: } catch (IOException x) {
1048: }
1049: }
1050: }
1051:
1052: }
1053:
1054: }
1055:
1056: private void gotoSleep() {
1057: try {
1058: Thread.sleep(1000);
1059: // i++;
1060: } catch (InterruptedException e) {
1061:
1062: }
1063: // System.out.println(i);
1064:
1065: }
1066:
1067: private File getBinaryContentForFile(File file) throws Exception {
1068: if (!file.exists()) {
1069: throw new IOException("File Not Valid");
1070: }
1071:
1072: java.lang.String t = Configuration.getTempDir();
1073: File e = new File(t);
1074: File out;
1075: if (e.exists()) {
1076: out = new File(t + File.separator + "binary.txt");
1077: } else {
1078: out = new File(System.getProperty("user.home")
1079: + File.separator + "binary.txt");
1080: }
1081:
1082: // BufferedReader dis=new BufferedReader(new InputStreamReader(new
1083: // DataInputStream(new FileInputStream(file))));
1084: DataInputStream dis = new DataInputStream(new FileInputStream(
1085: file));
1086: // BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new
1087: // DataOutputStream(new FileOutputStream(out))));
1088: DataOutputStream bw = new DataOutputStream(
1089: new FileOutputStream(out));
1090: String i = dis.readUTF();
1091: while (i != null) {
1092: try {
1093: bw.writeUTF(i);
1094: i = dis.readUTF();
1095: } catch (IOException ioe) {
1096: break;
1097: }
1098: }
1099: bw.flush();
1100: bw.close();
1101: return out;
1102:
1103: }
1104:
1105: private void displayHistoryFile(JTabbedPane tabs, String s) {
1106: try {
1107: tabs.setSelectedIndex(tabs.indexOfTab("Decompiled Ouput"));
1108: Component editor = tabs.getSelectedComponent();
1109: JScrollPane editorTab = (JScrollPane) editor;
1110: Object o = editorTab.getComponent(0);
1111: JViewport view = (JViewport) o;
1112: Object o2 = view.getView();
1113: if (o2 != null) {
1114: JEditorPane rdwrPane = (JEditorPane) o2;
1115:
1116: if (s != null) {
1117: File f = new File(s);
1118: FileReader fr = new FileReader(f);
1119: rdwrPane.read(fr, null);
1120: rdwrPane.setEditable(true);
1121: Manager.auxlabel.setForeground(Color.BLUE);
1122: Manager.auxlabel.setBackground(Color.WHITE);
1123: int sl = s.lastIndexOf("/");
1124: if (sl == -1) {
1125: sl = s.lastIndexOf("\\");
1126: }
1127: if (sl == -1) {
1128: Manager.auxlabel.setForeground(Color.BLUE);
1129: Manager.auxlabel.setBackground(Color.WHITE);
1130: Manager.auxlabel.setText("Loaded File " + s);
1131:
1132: } else {
1133: s = s.substring(sl + 1);
1134: Manager.auxlabel.setForeground(Color.BLUE);
1135: Manager.auxlabel.setBackground(Color.WHITE);
1136: Manager.auxlabel.setText("Loaded File " + s);
1137: }
1138:
1139: }
1140:
1141: }
1142: } catch (FileNotFoundException e) {
1143: JOptionPane.showMessageDialog(null,
1144: "Error while going back...");
1145:
1146: } catch (IOException e) {
1147: JOptionPane.showMessageDialog(null,
1148: "Error while going back...");
1149:
1150: }
1151:
1152: }
1153:
1154: private boolean isValidArchiveFileExt(String filename) {
1155: if (filename == null)
1156: return false;
1157: String types = UILauncher.getUIConfigRef().getArchiveTypes();
1158: StringTokenizer st = new StringTokenizer(types, ",");
1159: while (st.hasMoreTokens()) {
1160: String c = (String) st.nextToken();
1161: if (filename.endsWith(c)) {
1162: return true;
1163: }
1164: }
1165: return false;
1166: }
1167:
1168: }
|