gui: add fields and methods to tree

This commit is contained in:
Skylot
2013-07-23 23:01:48 +04:00
parent 25b2c8fe5b
commit c4cdd8514d
9 changed files with 230 additions and 25 deletions
+21 -19
View File
@@ -1,8 +1,6 @@
package jadx.gui;
import jadx.api.JavaClass;
import jadx.cli.JadxArgs;
import jadx.gui.treemodel.JClass;
import jadx.gui.treemodel.JNode;
import jadx.gui.treemodel.JRoot;
@@ -23,10 +21,10 @@ import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.BadLocationException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import java.awt.BorderLayout;
import java.awt.Color;
@@ -76,7 +74,6 @@ public class MainWindow extends JFrame {
treeModel.setRoot(treeRoot);
treeModel.reload();
tree.expandRow(0);
// expandTree();
}
private void toggleFlattenPackage() {
@@ -89,15 +86,26 @@ public class MainWindow extends JFrame {
}
}
private void expandTree() {
DefaultMutableTreeNode currentNode = ((DefaultMutableTreeNode) tree.getModel().getRoot()).getNextNode();
do {
if (currentNode.getLevel() == 1) {
tree.expandPath(new TreePath(currentNode.getPath()));
private void treeClickAction() {
Object obj = tree.getLastSelectedPathComponent();
if (obj instanceof JNode) {
JNode node = (JNode) obj;
if (node.getJParent() != null) {
textArea.setText(node.getJParent().getCode());
scrollToLine(node.getLine());
}
currentNode = currentNode.getNextNode();
}
while (currentNode != null);
}
private void scrollToLine(int line) {
if (line < 2) {
return;
}
try {
textArea.setCaretPosition(textArea.getLineStartOffset(line - 1));
} catch (BadLocationException e) {
LOG.error("Can't scroll to " + line, e);
}
}
private void initMenuAndToolbar() {
@@ -165,14 +173,8 @@ public class MainWindow extends JFrame {
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
Object obj = tree.getLastSelectedPathComponent();
if (obj instanceof JClass) {
JavaClass jc = ((JClass) obj).getCls();
String code = jc.getCode();
textArea.setText(code);
textArea.setCaretPosition(0);
}
public void valueChanged(TreeSelectionEvent event) {
treeClickAction();
}
});
@@ -1,6 +1,8 @@
package jadx.gui.treemodel;
import jadx.api.JavaClass;
import jadx.api.JavaField;
import jadx.api.JavaMethod;
import jadx.core.dex.info.AccessInfo;
import jadx.gui.Utils;
@@ -19,10 +21,10 @@ public class JClass extends DefaultMutableTreeNode implements JNode {
private static final ImageIcon ICON_ANNOTATION = Utils.openIcon("annotation_obj");
private final JavaClass cls;
private JClass jParrent;
public JClass(JavaClass cls) {
this.cls = cls;
updateChilds();
}
public JavaClass getCls() {
@@ -31,9 +33,23 @@ public class JClass extends DefaultMutableTreeNode implements JNode {
@Override
public void updateChilds() {
// for (JavaClass javaClass : cls.getInnerClasses()) {
// add(new JClass(javaClass));
// }
JClass currentParent = jParrent == null ? this : jParrent;
for (JavaClass javaClass : cls.getInnerClasses()) {
JClass child = new JClass(javaClass);
child.setJParent(currentParent);
child.updateChilds();
add(child);
}
for (JavaField f : cls.getFields()) {
add(new JField(f, currentParent));
}
for (JavaMethod m : cls.getMethods()) {
add(new JMethod(m, currentParent));
}
}
public String getCode(){
return cls.getCode();
}
@Override
@@ -57,6 +73,20 @@ public class JClass extends DefaultMutableTreeNode implements JNode {
}
}
public void setJParent(JClass parent) {
this.jParrent = parent;
}
@Override
public JClass getJParent() {
return jParrent;
}
@Override
public int getLine() {
return cls.getDecompiledLine();
}
@Override
public String toString() {
return cls.getShortName();
@@ -0,0 +1,57 @@
package jadx.gui.treemodel;
import jadx.api.JavaField;
import jadx.core.dex.info.AccessInfo;
import jadx.gui.Utils;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultMutableTreeNode;
public class JField extends DefaultMutableTreeNode implements JNode {
private static final ImageIcon ICON_FLD_DEF = Utils.openIcon("field_default_obj");
private static final ImageIcon ICON_FLD_PRI = Utils.openIcon("field_private_obj");
private static final ImageIcon ICON_FLD_PRO = Utils.openIcon("field_protected_obj");
private static final ImageIcon ICON_FLD_PUB = Utils.openIcon("field_public_obj");
private final JavaField field;
private final JClass jParent;
public JField(JavaField javaField, JClass jClass) {
this.field = javaField;
this.jParent = jClass;
}
@Override
public void updateChilds() {
}
@Override
public JClass getJParent() {
return jParent;
}
@Override
public int getLine() {
return field.getDecompiledLine();
}
@Override
public Icon getIcon() {
AccessInfo af = field.getAccessFlags();
if(af.isPublic()){
return ICON_FLD_PUB;
} else if(af.isPrivate()) {
return ICON_FLD_PRI;
} else if(af.isProtected()) {
return ICON_FLD_PRO;
} else {
return ICON_FLD_DEF;
}
}
@Override
public String toString() {
return field.getName();
}
}
@@ -0,0 +1,57 @@
package jadx.gui.treemodel;
import jadx.api.JavaMethod;
import jadx.core.dex.info.AccessInfo;
import jadx.gui.Utils;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultMutableTreeNode;
public class JMethod extends DefaultMutableTreeNode implements JNode {
private static final ImageIcon ICON_MTH_DEF = Utils.openIcon("methdef_obj");
private static final ImageIcon ICON_MTH_PRI = Utils.openIcon("methpri_obj");
private static final ImageIcon ICON_MTH_PRO = Utils.openIcon("methpro_obj");
private static final ImageIcon ICON_MTH_PUB = Utils.openIcon("methpub_obj");
private final JavaMethod mth;
private final JClass jparent;
public JMethod(JavaMethod javaMethod, JClass jClass) {
this.mth = javaMethod;
this.jparent = jClass;
}
@Override
public void updateChilds() {
}
@Override
public JClass getJParent() {
return jparent;
}
@Override
public int getLine() {
return mth.getDecompiledLine();
}
@Override
public Icon getIcon() {
AccessInfo af = mth.getAccessFlags();
if (af.isPublic()) {
return ICON_MTH_PUB;
} else if (af.isPrivate()) {
return ICON_MTH_PRI;
} else if (af.isProtected()) {
return ICON_MTH_PRO;
} else {
return ICON_MTH_DEF;
}
}
@Override
public String toString() {
return mth.getName();
}
}
@@ -4,6 +4,10 @@ import javax.swing.Icon;
public interface JNode {
JClass getJParent();
int getLine();
void updateChilds();
Icon getIcon();
@@ -40,6 +40,7 @@ public class JPackage extends DefaultMutableTreeNode implements JNode, Comparabl
add(pkg);
}
for (JClass cls : classes) {
cls.updateChilds();
add(cls);
}
}
@@ -65,6 +66,16 @@ public class JPackage extends DefaultMutableTreeNode implements JNode, Comparabl
return PACKAGE_ICON;
}
@Override
public JClass getJParent() {
return null;
}
@Override
public int getLine() {
return 0;
}
@Override
public int compareTo(JPackage o) {
return name.compareTo(o.name);
@@ -118,6 +118,16 @@ public class JRoot extends DefaultMutableTreeNode implements JNode {
return ROOT_ICON;
}
@Override
public JClass getJParent() {
return null;
}
@Override
public int getLine() {
return 0;
}
@Override
public String toString() {
File file = wrapper.getOpenFile();