All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.alantea.swing.tree.SelectableTreeCellRenderer Maven / Gradle / Ivy

The newest version!
package net.alantea.swing.tree;

import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;

import sun.swing.DefaultLookup;

@SuppressWarnings({ "serial", "restriction" })
public class SelectableTreeCellRenderer extends JCheckBox implements TreeCellRenderer
{
   @Override
   public Component getTreeCellRendererComponent(JTree tree, Object value,
         boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
   {
      String stringValue = tree.convertValueToText(value, sel, expanded, leaf, row, hasFocus);
      setText(stringValue);
      setEnabled(false);
      setSelected(false);
      
      Object userObject = value;
      if (value instanceof DefaultMutableTreeNode)
      {
         DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
         userObject = node.getUserObject();
         if (userObject instanceof DefaultMutableTreeNode)
         {
            userObject = ((DefaultMutableTreeNode) userObject).getUserObject();
         }
      }
      if ((userObject != null) && (userObject instanceof Selectable))
      {
         if (((Selectable) userObject).isSelectable())
         {
            setEnabled(true);
            if (((Selectable) userObject).isSelected())
            {
               setSelected(((Selectable) userObject).isSelected());
            }
         }
         else
         {
            setSelected(false);
            setEnabled(false);
         }
      }
      setBackground(DefaultLookup.getColor(this, ui, "Tree.textBackground"));
      return this;
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy