net.alantea.swing.tree.ClassBasedTreeCellRenderer Maven / Gradle / Ivy
package net.alantea.swing.tree;
import java.awt.Component;
import java.awt.Image;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import net.alantea.swing.misc.ResourceManager;
@SuppressWarnings("serial")
public class ClassBasedTreeCellRenderer extends DefaultTreeCellRenderer
{
private static Map, ImageIcon> iconsMap = new HashMap<>();
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
{
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row,
hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
Object userObject = node.getUserObject();
if (userObject instanceof DefaultMutableTreeNode)
{
userObject = ((DefaultMutableTreeNode) userObject).getUserObject();
}
if (userObject != null)
{
Icon icon = iconsMap.get(userObject.getClass());
if (icon != null)
{
setIcon(icon);
}
}
return this;
}
public static void setIcon(Class> theClass, String imageKey)
{
Image image = ResourceManager.getImage(imageKey);
if (image != null)
{
iconsMap.put(theClass, ResourceManager.getIcon(image));
}
}
public static Icon getIcon(Class> theClass)
{
return iconsMap.get(theClass);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy