Skip to content

Commit 0bc6d4a

Browse files
committed
Resource List Icon Cache
This is still a work in progress and may cause bugs, specifically when you remove an imported resource without resetting the workspace
1 parent 4d87d2e commit 0bc6d4a

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/main/java/the/bytecode/club/bytecodeviewer/BytecodeViewer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import the.bytecode.club.bytecodeviewer.bootloader.UpdateCheck;
2121
import the.bytecode.club.bytecodeviewer.gui.MainViewerGUI;
2222
import the.bytecode.club.bytecodeviewer.gui.components.*;
23+
import the.bytecode.club.bytecodeviewer.gui.resourcelist.ResourceListIconRenderer;
2324
import the.bytecode.club.bytecodeviewer.gui.resourceviewer.TabbedPane;
2425
import the.bytecode.club.bytecodeviewer.gui.resourceviewer.viewer.ClassViewer;
2526
import the.bytecode.club.bytecodeviewer.gui.resourceviewer.viewer.ResourceViewer;
@@ -83,7 +84,6 @@
8384
* + BCV's classLoader should be destroyed each time a resource is added or removed
8485
*
8586
* TODO DarkLAF Specific Bugs:
86-
* + Resource List creates swing lag with large project
8787
* + JMenuBar can only be displayed on a JFrame, a work around is needed for this (Partially solved)
8888
*
8989
* TODO IN-PROGRESS:
@@ -709,6 +709,7 @@ public static void resetWorkspace()
709709
BytecodeViewer.viewer.workPane.resetWorkspace();
710710
BytecodeViewer.viewer.searchBoxPane.resetWorkspace();
711711
BCV.getClassNodeLoader().clear();
712+
ResourceListIconRenderer.iconCache.clear();
712713
}
713714

714715
/**

src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListIconRenderer.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import javax.swing.tree.TreeNode;
1010
import java.awt.*;
1111
import java.util.ArrayList;
12+
import java.util.HashMap;
1213

1314
/**
1415
* @author http://stackoverflow.com/questions/14968005
@@ -17,6 +18,9 @@
1718

1819
public class ResourceListIconRenderer extends DefaultTreeCellRenderer
1920
{
21+
//TODO the icon cache needs to be cleared on treenode removal
22+
public static HashMap<ResourceTreeNode, ImageIcon> iconCache = new HashMap<>();
23+
2024
//called every time there is a pane update
2125
@Override
2226
public Component getTreeCellRendererComponent(
@@ -28,7 +32,14 @@ public Component getTreeCellRendererComponent(
2832

2933
if (value instanceof ResourceTreeNode)
3034
{
35+
if (iconCache.containsKey(value))
36+
{
37+
setIcon(iconCache.get(value));
38+
return ret;
39+
}
40+
3141
ResourceTreeNode node = (ResourceTreeNode) value;
42+
3243
String nameOG = node.toString();
3344
String name = nameOG.toLowerCase();
3445
String onlyName = FilenameUtils.getName(name);
@@ -44,21 +55,21 @@ public Component getTreeCellRendererComponent(
4455
&& (node.getParent() == node.getRoot()
4556
|| node.getChildCount() == 0))
4657
{
47-
setIcon(knownResourceType.getIcon());
58+
cacheNodeIcon(node, knownResourceType.getIcon());
4859
iconSet = true;
4960
}
5061
//hardcoded resource icons go here
5162
else if (nameOG.equals("Decoded Resources") && node.getChildCount() > 0)
5263
{
53-
setIcon(IconResources.decodedIcon);
64+
cacheNodeIcon(node, IconResources.decodedIcon);
5465
iconSet = true;
5566
}
5667
else if (node.getChildCount() == 0
5768
&& (nameOG.equals("README")
5869
|| nameOG.equals("LICENSE")
5970
|| nameOG.equals("NOTICE")))
6071
{
61-
setIcon(IconResources.textIcon);
72+
cacheNodeIcon(node, IconResources.textIcon);
6273
iconSet = true;
6374
}
6475

@@ -107,17 +118,23 @@ else if (node.getChildCount() == 0
107118
{
108119
//java packages
109120
if (isJava)
110-
setIcon(IconResources.packagesIcon);
121+
cacheNodeIcon(node, IconResources.packagesIcon);
111122
else //regular folders
112-
setIcon(IconResources.folderIcon);
123+
cacheNodeIcon(node, IconResources.folderIcon);
113124
}
114125
}
115126

116127
//unknown files
117128
else if (knownResourceType == null && !iconSet)
118-
setIcon(IconResources.unknownFileIcon);
129+
cacheNodeIcon(node, IconResources.unknownFileIcon);
119130
}
120131

121132
return ret;
122133
}
134+
135+
public void cacheNodeIcon(ResourceTreeNode node, ImageIcon icon)
136+
{
137+
iconCache.put(node, icon);
138+
setIcon(icon);
139+
}
123140
}

0 commit comments

Comments
 (0)