Skip to content

Commit

Permalink
feat(gui): save tree expansions in project (PR #605)
Browse files Browse the repository at this point in the history
  • Loading branch information
asashour authored and skylot committed Apr 20, 2019
1 parent 87ca14a commit f803973
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
2 changes: 1 addition & 1 deletion jadx-core/src/main/java/jadx/api/JavaClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public synchronized void unload() {
cls.unload();
}

ClassNode getClassNode() {
public ClassNode getClassNode() {
return cls;
}

Expand Down
22 changes: 22 additions & 0 deletions jadx-gui/src/main/java/jadx/gui/settings/JadxProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.io.BufferedWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
Expand All @@ -29,6 +31,8 @@ public class JadxProject {
private transient String name = "New Project";
private transient Path projectPath;
private List<Path> filesPath;
private List<String[]> treeExpansions = new ArrayList<>();

private transient boolean saved;
private transient boolean initial = true;

Expand Down Expand Up @@ -63,6 +67,24 @@ public void setFilePath(Path filePath) {
}
}

public List<String[]> getTreeExpansions() {
return treeExpansions;
}

public void addTreeExpansion(String[] expansion) {
treeExpansions.add(expansion);
changed();
}

public void removeTreeExpansion(String[] expansion) {
for (Iterator<String[]> it = treeExpansions.iterator(); it.hasNext(); ) {
if (Arrays.equals(it.next(), expansion)) {
it.remove();
}
}
changed();
}

private void changed() {
if (settings.isAutoSaveProject()) {
save();
Expand Down
70 changes: 68 additions & 2 deletions jadx-gui/src/main/java/jadx/gui/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
Expand Down Expand Up @@ -130,6 +132,7 @@ public class MainWindow extends JFrame {
private JRoot treeRoot;
private TabbedPane tabbedPane;
private HeapUsageBar heapUsageBar;
private transient boolean treeReloading;

private boolean isFlattenPackage;
private JToggleButton flatPkgButton;
Expand Down Expand Up @@ -431,8 +434,34 @@ private void initTree() {
}

private void reloadTree() {
treeReloading = true;

treeModel.reload();
tree.expandRow(1);
List<String[]> treeExpansions = project.getTreeExpansions();
if (!treeExpansions.isEmpty()) {
expand(treeRoot, treeExpansions);
} else {
tree.expandRow(1);
}

treeReloading = false;
}

private void expand(TreeNode node, List<String[]> treeExpansions) {
TreeNode[] pathNodes = treeModel.getPathToRoot(node);
if (pathNodes == null) {
return;
}
TreePath path = new TreePath(pathNodes);
for (String[] expansion : treeExpansions) {
if (Arrays.equals(expansion, getPathExpansion(path))) {
tree.expandPath(path);
break;
}
}
for (int i = node.getChildCount() - 1; i >= 0; i--) {
expand(node.getChildAt(i), treeExpansions);
}
}

private void toggleFlattenPackage() {
Expand Down Expand Up @@ -824,11 +853,18 @@ public void treeWillExpand(TreeExpansionEvent event) {
if (node instanceof JLoadableNode) {
((JLoadableNode) node).loadNode();
}
if (!treeReloading) {
project.addTreeExpansion(getPathExpansion(event.getPath()));
update();
}
}

@Override
public void treeWillCollapse(TreeExpansionEvent event) {
// ignore
if (!treeReloading) {
project.removeTreeExpansion(getPathExpansion(event.getPath()));
update();
}
}
});

Expand All @@ -851,6 +887,36 @@ public void treeWillCollapse(TreeExpansionEvent event) {
setTitle(DEFAULT_TITLE);
}

private static String[] getPathExpansion(TreePath path) {
List<String> pathList = new ArrayList<>();
while (path != null) {
Object node = path.getLastPathComponent();
String name;
if (node instanceof JClass) {
name = ((JClass) node).getCls().getClassNode().getClassInfo().getFullName();
}
else {
name = node.toString();
}
pathList.add(name);
path = path.getParentPath();
}
return pathList.toArray(new String[pathList.size()]);
}

public static void getExpandedPaths(JTree tree, TreePath path, List<TreePath> list) {
if (tree.isExpanded(path)) {
list.add(path);

TreeNode node = (TreeNode) path.getLastPathComponent();
for (int i = node.getChildCount() - 1; i >= 0; i--) {
TreeNode n = node.getChildAt(i);
TreePath child = path.pathByAddingChild(n);
getExpandedPaths(tree, child, list);
}
}
}

public void setLocationAndPosition() {
if (this.settings.loadWindowPos(this)) {
return;
Expand Down
2 changes: 1 addition & 1 deletion jadx-gui/src/main/java/jadx/gui/utils/PathTypeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

public class PathTypeAdapter {
public class PathTypeAdapter {

private static TypeAdapter<Path> SINGLETON;

Expand Down

0 comments on commit f803973

Please sign in to comment.