Mirror of the JODE repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
jode/jode/jode/swingui/Main.java.in

309 lines
8.6 KiB

/* Main Copyright (C) 1999 Jochen Hoenicke.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id$
*/
package jode.swingui;
import jode.GlobalOptions;
import jode.decompiler.*;
import jode.bytecode.ClassInfo;
import jode.bytecode.SearchPath;
import @JAVAX_SWING@.*;
import @JAVAX_SWING@.event.*;
import @JAVAX_SWING@.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Main
implements ActionListener, Runnable, TreeSelectionListener {
JFrame frame;
JTree classTree;
JPanel statusLine;
PackagesTreeModel packModel;
HierarchyTreeModel hierModel;
JTextArea sourcecodeArea, errorArea;
Thread decompileThread;
String currentClassPath, lastClassName;
JProgressBar progressBar;
boolean hierarchyTree;
public Main(String classpath) {
setClasspath(classpath);
frame = new JFrame(GlobalOptions.copyright);
fillContentPane(frame.getContentPane());
addMenu(frame);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void show() {
frame.pack();
frame.show();
}
public void fillContentPane(Container contentPane) {
statusLine = new JPanel();
hierarchyTree = false;
packModel = new PackagesTreeModel(this);
hierModel = null;
Font monospaced = new Font("monospaced", Font.PLAIN, 12);
classTree = new JTree(packModel);
classTree.setRootVisible(false);
DefaultTreeSelectionModel selModel = new DefaultTreeSelectionModel();
selModel.setSelectionMode(selModel.SINGLE_TREE_SELECTION);
classTree.setSelectionModel(selModel);
classTree.addTreeSelectionListener(this);
JScrollPane spClassTree = new JScrollPane(classTree);
sourcecodeArea = new JTextArea(20, 80);
sourcecodeArea.setFont(monospaced);
JScrollPane spText = new JScrollPane(sourcecodeArea);
errorArea = new JTextArea(3, 80);
errorArea.setFont(monospaced);
JScrollPane spError = new JScrollPane(errorArea);
JSplitPane rightPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
spText, spError);
JSplitPane allPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
spClassTree, rightPane);
contentPane.setLayout(new BorderLayout());
contentPane.add(allPane, BorderLayout.CENTER);
contentPane.add(statusLine, BorderLayout.SOUTH);
progressBar = new JProgressBar();
statusLine.add(progressBar);
rightPane.setDividerLocation(300);
rightPane.setDividerSize(4);
allPane.setDividerLocation(200);
allPane.setDividerSize(4);
GlobalOptions.err = new PrintWriter
(new BufferedWriter(new AreaWriter(errorArea)), true);
}
public synchronized void valueChanged(TreeSelectionEvent e) {
if (decompileThread != null)
return;
TreePath path = e.getNewLeadSelectionPath();
if (path == null)
return;
Object node = path.getLastPathComponent();
if (node != null) {
if (hierarchyTree && hierModel.isValidClass(node))
lastClassName = hierModel.getFullName(node);
else if (!hierarchyTree && packModel.isValidClass(node))
lastClassName = packModel.getFullName(node);
else
return;
decompileThread = new Thread(this);
decompileThread.setPriority(Thread.MIN_PRIORITY);
sourcecodeArea.setText("Please wait, while decompiling...\n");
decompileThread.start();
}
}
public synchronized void actionPerformed(ActionEvent e) {
if (e.getSource() == classTree && decompileThread == null) {
decompileThread = new Thread(this);
sourcecodeArea.setText("Please wait, while decompiling...\n");
decompileThread.start();
}
}
public class AreaWriter extends Writer {
boolean initialized = false;
private JTextArea area;
public AreaWriter(JTextArea a) {
area = a;
}
public void write(char[] b, int off, int len) throws IOException {
if (!initialized) {
area.setText("");
initialized = true;
}
area.append(new String(b, off, len));
}
public void flush() {
}
public void close() {
}
}
public void run() {
errorArea.setText("");
ImportHandler imports = new ImportHandler();
try {
ClassInfo clazz = ClassInfo.forName(lastClassName);
TabbedPrintWriter writer =
new TabbedPrintWriter
(new BufferedWriter
(new AreaWriter(sourcecodeArea), 1024), imports, false);
ClassAnalyzer clazzAna = new ClassAnalyzer(null, clazz, imports);
clazzAna.dumpJavaFile(writer);
writer.close();
} catch (Throwable t) {
sourcecodeArea.setText("Didn't succeed.\n"
+"Check the below area for more info.");
t.printStackTrace(GlobalOptions.err);
} finally {
synchronized(this) {
decompileThread = null;
}
}
}
public void addMenu(JFrame frame) {
JMenuBar bar = new JMenuBar();
JMenu menu;
JMenuItem item;
menu = new JMenu("File");
item = new JMenuItem("Garbage collect");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
System.gc();
System.runFinalization();
}
});
menu.add(item);
item = new JMenuItem("Exit");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
System.exit(0);
}
});
menu.add(item);
bar.add(menu);
menu = new JMenu("Options");
final JCheckBoxMenuItem hierItem
= new JCheckBoxMenuItem("Class hierarchy", hierarchyTree);
hierItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
hierarchyTree = hierItem.isSelected();
if (hierarchyTree && hierModel == null) {
hierModel = new HierarchyTreeModel(Main.this, progressBar);
reselect();
}
classTree.setModel(hierarchyTree
? (TreeModel) hierModel : packModel);
if (lastClassName != null) {
TreePath lastPath = (hierarchyTree
? hierModel.getPath(lastClassName)
: packModel.getPath(lastClassName));
classTree.setSelectionPath(lastPath);
classTree.scrollPathToVisible(lastPath);
}
}
});
menu.add(hierItem);
menu.add(new JSeparator());
item = new JMenuItem("Set classpath...");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
String newClassPath = (String) JOptionPane.showInputDialog
(null, "New classpath:", null,
JOptionPane.QUESTION_MESSAGE, null,
null, currentClassPath);
if (newClassPath != null
&& !newClassPath.equals(currentClassPath))
setClasspath(newClassPath);
}
});
menu.add(item);
item = new JMenuItem("Reload classpath");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
setClasspath(currentClassPath);
}
});
menu.add(item);
bar.add(menu);
frame.setJMenuBar(bar);
}
public void setClasspath(String classpath) {
if (classpath == null || classpath.length() == 0)
classpath = ".";
currentClassPath = classpath;
ClassInfo.setClassPath(classpath);
if (classTree != null)
classTree.clearSelection();
if (packModel != null)
packModel.rebuild();
if (hierModel != null && hierarchyTree) {
hierModel.rebuild();
} else {
hierModel = null;
}
}
public void treeNodesChanged(TreeModelEvent e) {
reselect();
}
public void treeNodesInserted(TreeModelEvent e) {
reselect();
}
public void treeNodesRemoved(TreeModelEvent e) {
reselect();
}
public void treeStructureChanged(TreeModelEvent e) {
reselect();
}
public void reselect() {
if (lastClassName != null) {
TreePath lastPath = (hierarchyTree
? hierModel.getPath(lastClassName)
: packModel.getPath(lastClassName));
if (lastPath != null) {
classTree.setSelectionPath(lastPath);
classTree.scrollPathToVisible(lastPath);
}
}
}
public static void main(String[] params) {
String cp = System.getProperty("java.class.path", "");
cp = cp.replace(File.pathSeparatorChar,
SearchPath.altPathSeparatorChar);
for (int i=0; i<params.length; i++) {
if (params[i].equals("--classpath"))
cp = params[++i];
else
return;
}
GlobalOptions.verboseLevel = 1;
Main win = new Main(cp);
win.show();
}
}