Optimized resource tree building

This commit is contained in:
András Oravecz 2022-09-30 15:25:36 +02:00
parent d80611abe4
commit eaec423514
2 changed files with 91 additions and 23 deletions

View file

@ -176,29 +176,22 @@ public class ResourceListPane extends TranslatedVisibleComponent implements File
for (String name : container.resourceClasses.keySet()) for (String name : container.resourceClasses.keySet())
{ {
final String[] spl = name.split("/"); final String[] spl = name.split("/");
if (spl.length < 2) int splLength = spl.length;
if (splLength < 2)
{ {
root.add(new ResourceTreeNode(name + ".class")); root.add(new ResourceTreeNode(name + ".class"));
} }
else else
{ {
ResourceTreeNode parent = root; ResourceTreeNode parent = root;
for (int i1 = 0; i1 < spl.length; i1++) for (int i1 = 0; i1 < splLength; i1++)
{ {
String s = spl[i1]; String s = spl[i1];
if (i1 == spl.length - 1) if (i1 == splLength - 1)
s += ".class"; s += ".class";
ResourceTreeNode child = null; ResourceTreeNode child = parent.getChildByUserObject(s);
for (int i = 0; i < parent.getChildCount(); i++)
{
if (((ResourceTreeNode) parent.getChildAt(i)).getUserObject().equals(s))
{
child = (ResourceTreeNode) parent.getChildAt(i);
break;
}
}
if (child == null) if (child == null)
{ {
@ -227,20 +220,14 @@ public class ResourceListPane extends TranslatedVisibleComponent implements File
ResourceTreeNode parent = root; ResourceTreeNode parent = root;
for (final String s : spl) for (final String s : spl)
{ {
ResourceTreeNode child = null; ResourceTreeNode child = parent.getChildByUserObject(s);
for (int i = 0; i < parent.getChildCount(); i++)
{
if (((ResourceTreeNode) parent.getChildAt(i)).getUserObject().equals(s))
{
child = (ResourceTreeNode) parent.getChildAt(i);
break;
}
}
if (child == null) if (child == null)
{ {
child = new ResourceTreeNode(s); child = new ResourceTreeNode(s);
parent.add(child); parent.add(child);
} }
parent = child; parent = child;
} }
} }

View file

@ -1,9 +1,10 @@
package the.bytecode.club.bytecodeviewer.gui.resourcelist; package the.bytecode.club.bytecodeviewer.gui.resourcelist;
import java.util.Comparator;
import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode; import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode; import javax.swing.tree.TreeNode;
import java.util.Comparator;
import java.util.HashMap;
/*************************************************************************** /***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite * * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
@ -31,6 +32,9 @@ public class ResourceTreeNode extends DefaultMutableTreeNode
{ {
private static final long serialVersionUID = -8817777566176729571L; private static final long serialVersionUID = -8817777566176729571L;
private static final int CHILD_MAP_BUILD_THRESHOLD = 3;
private HashMap<Object, ResourceTreeNode> userObjectToChildMap = null;
public ResourceTreeNode(final Object o) public ResourceTreeNode(final Object o)
{ {
@ -41,6 +45,7 @@ public class ResourceTreeNode extends DefaultMutableTreeNode
public void insert(final MutableTreeNode newChild, final int childIndex) public void insert(final MutableTreeNode newChild, final int childIndex)
{ {
super.insert(newChild, childIndex); super.insert(newChild, childIndex);
addToMap((ResourceTreeNode) newChild);
} }
public void sort() public void sort()
@ -60,7 +65,83 @@ public class ResourceTreeNode extends DefaultMutableTreeNode
} }
} }
} }
@Override
public void add(MutableTreeNode newChild) {
super.add(newChild);
addToMap((ResourceTreeNode) newChild);
}
private void addToMap(ResourceTreeNode newChild) {
if (userObjectToChildMap != null)
{
userObjectToChildMap.put(newChild.getUserObject(), newChild);
}
else if (getChildCount() == CHILD_MAP_BUILD_THRESHOLD)
{
buildMap();
}
}
private void buildMap() {
userObjectToChildMap = new HashMap<>();
for (int i = 0, childCount = getChildCount(); i < childCount; i++)
{
ResourceTreeNode item = (ResourceTreeNode) getChildAt(i);
userObjectToChildMap.put(item.getUserObject(), item);
}
}
@Override
public void remove(int childIndex) {
if (userObjectToChildMap != null)
{
TreeNode childAt = getChildAt(childIndex);
userObjectToChildMap.remove(((ResourceTreeNode) childAt).getUserObject());
}
super.remove(childIndex);
}
@Override
public void remove(MutableTreeNode aChild) {
if (userObjectToChildMap != null && aChild != null)
{
userObjectToChildMap.remove(((ResourceTreeNode) aChild).getUserObject());
}
super.remove(aChild);
}
@Override
public void removeAllChildren() {
if (userObjectToChildMap != null)
{
userObjectToChildMap.clear();
}
super.removeAllChildren();
}
public ResourceTreeNode getChildByUserObject(Object userObject) {
if (userObjectToChildMap != null)
{
return userObjectToChildMap.get(userObject);
}
for (int i = 0, childCount = getChildCount(); i < childCount; i++)
{
ResourceTreeNode child = (ResourceTreeNode) getChildAt(i);
if (child.getUserObject().equals(userObject))
{
return child;
}
}
return null;
}
protected Comparator<TreeNode> nodeComparator = new Comparator<TreeNode>() protected Comparator<TreeNode> nodeComparator = new Comparator<TreeNode>()
{ {
@Override @Override