From 0bc6d4a01b5c83de2f4849a949da29cba2fd73af Mon Sep 17 00:00:00 2001 From: Konloch Date: Tue, 10 Aug 2021 01:45:25 -0700 Subject: [PATCH] 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 --- .../club/bytecodeviewer/BytecodeViewer.java | 3 +- .../ResourceListIconRenderer.java | 29 +++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/BytecodeViewer.java b/src/main/java/the/bytecode/club/bytecodeviewer/BytecodeViewer.java index 8e15cf6b..be69fe71 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/BytecodeViewer.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/BytecodeViewer.java @@ -20,6 +20,7 @@ import the.bytecode.club.bytecodeviewer.bootloader.InstallFatJar; import the.bytecode.club.bytecodeviewer.bootloader.UpdateCheck; import the.bytecode.club.bytecodeviewer.gui.MainViewerGUI; import the.bytecode.club.bytecodeviewer.gui.components.*; +import the.bytecode.club.bytecodeviewer.gui.resourcelist.ResourceListIconRenderer; import the.bytecode.club.bytecodeviewer.gui.resourceviewer.TabbedPane; import the.bytecode.club.bytecodeviewer.gui.resourceviewer.viewer.ClassViewer; import the.bytecode.club.bytecodeviewer.gui.resourceviewer.viewer.ResourceViewer; @@ -83,7 +84,6 @@ import static the.bytecode.club.bytecodeviewer.Constants.*; * + BCV's classLoader should be destroyed each time a resource is added or removed * * TODO DarkLAF Specific Bugs: - * + Resource List creates swing lag with large project * + JMenuBar can only be displayed on a JFrame, a work around is needed for this (Partially solved) * * TODO IN-PROGRESS: @@ -709,6 +709,7 @@ public class BytecodeViewer BytecodeViewer.viewer.workPane.resetWorkspace(); BytecodeViewer.viewer.searchBoxPane.resetWorkspace(); BCV.getClassNodeLoader().clear(); + ResourceListIconRenderer.iconCache.clear(); } /** diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListIconRenderer.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListIconRenderer.java index 0b23c6ff..c88e21f1 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListIconRenderer.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListIconRenderer.java @@ -9,6 +9,7 @@ import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.TreeNode; import java.awt.*; import java.util.ArrayList; +import java.util.HashMap; /** * @author http://stackoverflow.com/questions/14968005 @@ -17,6 +18,9 @@ import java.util.ArrayList; public class ResourceListIconRenderer extends DefaultTreeCellRenderer { + //TODO the icon cache needs to be cleared on treenode removal + public static HashMap iconCache = new HashMap<>(); + //called every time there is a pane update @Override public Component getTreeCellRendererComponent( @@ -28,7 +32,14 @@ public class ResourceListIconRenderer extends DefaultTreeCellRenderer if (value instanceof ResourceTreeNode) { + if (iconCache.containsKey(value)) + { + setIcon(iconCache.get(value)); + return ret; + } + ResourceTreeNode node = (ResourceTreeNode) value; + String nameOG = node.toString(); String name = nameOG.toLowerCase(); String onlyName = FilenameUtils.getName(name); @@ -44,13 +55,13 @@ public class ResourceListIconRenderer extends DefaultTreeCellRenderer && (node.getParent() == node.getRoot() || node.getChildCount() == 0)) { - setIcon(knownResourceType.getIcon()); + cacheNodeIcon(node, knownResourceType.getIcon()); iconSet = true; } //hardcoded resource icons go here else if (nameOG.equals("Decoded Resources") && node.getChildCount() > 0) { - setIcon(IconResources.decodedIcon); + cacheNodeIcon(node, IconResources.decodedIcon); iconSet = true; } else if (node.getChildCount() == 0 @@ -58,7 +69,7 @@ public class ResourceListIconRenderer extends DefaultTreeCellRenderer || nameOG.equals("LICENSE") || nameOG.equals("NOTICE"))) { - setIcon(IconResources.textIcon); + cacheNodeIcon(node, IconResources.textIcon); iconSet = true; } @@ -107,17 +118,23 @@ public class ResourceListIconRenderer extends DefaultTreeCellRenderer { //java packages if (isJava) - setIcon(IconResources.packagesIcon); + cacheNodeIcon(node, IconResources.packagesIcon); else //regular folders - setIcon(IconResources.folderIcon); + cacheNodeIcon(node, IconResources.folderIcon); } } //unknown files else if (knownResourceType == null && !iconSet) - setIcon(IconResources.unknownFileIcon); + cacheNodeIcon(node, IconResources.unknownFileIcon); } return ret; } + + public void cacheNodeIcon(ResourceTreeNode node, ImageIcon icon) + { + iconCache.put(node, icon); + setIcon(icon); + } }