ExtendableTreeCellRendererpublic class ExtendableTreeCellRenderer extends Object implements TreeCellRendererThis is a {@link TreeCellRenderer} implementation which is able
to render arbitrary objects. The {@link ExtendableTreeCellRenderer}
does not do the rendering itself but instead dispatches to
class-specific renderers. A class/renderer pair must be registered
using the {@link #register} method. If a class has no registered
renderer, the renderer of its closest superclass is used. Since the
{@link ExtendableTreeCellRenderer} always has a default renderer
for the {@link Object} class, rendering is always possible. The
default {@link Object} renderer can be replaced by another renderer
but it cannot be unregistered. |
Fields Summary |
---|
protected Map | renderers Maps classes to renderers. |
Constructors Summary |
---|
public ExtendableTreeCellRenderer()
renderers = new HashMap();
register(Object.class, new DefaultTreeCellRenderer()
{
public Component getTreeCellRendererComponent
(JTree tree, Object value, boolean selected,
boolean expanded, boolean leaf, int row, boolean hasFocus)
{
final String s = value.toString();
final JLabel l = new JLabel(s + " ");
if (selected)
{
Util.invert(l);
l.setOpaque(true);
}
return l;
}
});
|
Methods Summary |
---|
protected javax.swing.tree.TreeCellRenderer | findRenderer(java.lang.Class c)Find the renderer for the specified class.
final TreeCellRenderer r = (TreeCellRenderer) renderers.get(c);
if (r != null)
/* The class has a renderer. */
return r;
/* The class has no renderer, try the superclass, if any. */
final Class superclass = c.getSuperclass();
if (superclass != null)
return findRenderer(superclass);
else
return null;
| public java.awt.Component | getTreeCellRendererComponent(javax.swing.JTree tree, java.lang.Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)Renders an object in a tree cell depending of the object's
class.
final String NULL = "null";
TreeCellRenderer r;
Object userObject;
if (value == null)
userObject = NULL;
else
{
userObject = ((DefaultMutableTreeNode) value).getUserObject();
if (userObject == null)
userObject = NULL;
}
r = findRenderer(userObject.getClass());
return r.getTreeCellRendererComponent
(tree, value, selected, expanded, leaf, row,
hasFocus);
| public void | register(java.lang.Class c, javax.swing.tree.TreeCellRenderer renderer)Registers a renderer for a class.
renderers.put(c, renderer);
| public void | unregister(java.lang.Class c)Unregisters a renderer for a class. The renderer for the
{@link Object} class cannot be unregistered.
if (c == Object.class)
throw new IllegalArgumentException
("Renderer for Object cannot be unregistered.");
renderers.put(c, null);
|
|