1 2 17 18 19 package org.apache.poi.contrib.poibrowser; 20 21 import java.awt.*; 22 import javax.swing.*; 23 import javax.swing.tree.*; 24 import java.util.*; 25 26 43 public class ExtendableTreeCellRenderer implements TreeCellRenderer 44 { 45 46 49 protected Map renderers; 50 51 52 53 public ExtendableTreeCellRenderer() 54 { 55 renderers = new HashMap(); 56 register(Object .class, new DefaultTreeCellRenderer() 57 { 58 public Component getTreeCellRendererComponent 59 (JTree tree, Object value, boolean selected, 60 boolean expanded, boolean leaf, int row, boolean hasFocus) 61 { 62 final String s = value.toString(); 63 final JLabel l = new JLabel(s + " "); 64 if (selected) 65 { 66 Util.invert(l); 67 l.setOpaque(true); 68 } 69 return l; 70 } 71 }); 72 } 73 74 75 76 79 public void register(final Class c, final TreeCellRenderer renderer) 80 { 81 renderers.put(c, renderer); 82 } 83 84 85 86 90 public void unregister(final Class c) 91 { 92 if (c == Object .class) 93 throw new IllegalArgumentException 94 ("Renderer for Object cannot be unregistered."); 95 renderers.put(c, null); 96 } 97 98 99 100 106 public Component getTreeCellRendererComponent 107 (final JTree tree, final Object value, final boolean selected, 108 final boolean expanded, final boolean leaf, final int row, 109 final boolean hasFocus) 110 { 111 final String NULL = "null"; 112 TreeCellRenderer r; 113 Object userObject; 114 if (value == null) 115 userObject = NULL; 116 else 117 { 118 userObject = ((DefaultMutableTreeNode) value).getUserObject(); 119 if (userObject == null) 120 userObject = NULL; 121 } 122 r = findRenderer(userObject.getClass()); 123 return r.getTreeCellRendererComponent 124 (tree, value, selected, expanded, leaf, row, 125 hasFocus); 126 } 127 128 129 130 133 protected TreeCellRenderer findRenderer(final Class c) 134 { 135 final TreeCellRenderer r = (TreeCellRenderer) renderers.get(c); 136 if (r != null) 137 138 return r; 139 140 141 final Class superclass = c.getSuperclass(); 142 if (superclass != null) 143 return findRenderer(superclass); 144 else 145 return null; 146 } 147 148 } 149 | Popular Tags |