Skip to content

Commit

Permalink
Resource List Icon Cache
Browse files Browse the repository at this point in the history
This is still a work in progress and may cause bugs, specifically when you remove an imported resource without resetting the workspace
  • Loading branch information
Konloch committed Aug 10, 2021
1 parent 4d87d2e commit 0bc6d4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -83,7 +84,6 @@
* + 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:
Expand Down Expand Up @@ -709,6 +709,7 @@ public static void resetWorkspace()
BytecodeViewer.viewer.workPane.resetWorkspace();
BytecodeViewer.viewer.searchBoxPane.resetWorkspace();
BCV.getClassNodeLoader().clear();
ResourceListIconRenderer.iconCache.clear();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.swing.tree.TreeNode;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;

/**
* @author http://stackoverflow.com/questions/14968005
Expand All @@ -17,6 +18,9 @@

public class ResourceListIconRenderer extends DefaultTreeCellRenderer
{
//TODO the icon cache needs to be cleared on treenode removal
public static HashMap<ResourceTreeNode, ImageIcon> iconCache = new HashMap<>();

//called every time there is a pane update
@Override
public Component getTreeCellRendererComponent(
Expand All @@ -28,7 +32,14 @@ public Component getTreeCellRendererComponent(

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);
Expand All @@ -44,21 +55,21 @@ public Component getTreeCellRendererComponent(
&& (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
&& (nameOG.equals("README")
|| nameOG.equals("LICENSE")
|| nameOG.equals("NOTICE")))
{
setIcon(IconResources.textIcon);
cacheNodeIcon(node, IconResources.textIcon);
iconSet = true;
}

Expand Down Expand Up @@ -107,17 +118,23 @@ else if (node.getChildCount() == 0
{
//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);
}
}

0 comments on commit 0bc6d4a

Please sign in to comment.