KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > contrib > poibrowser > ExtendableTreeCellRenderer


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

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 /**
27  * <p>This is a {@link TreeCellRenderer} implementation which is able
28  * to render arbitrary objects. The {@link ExtendableTreeCellRenderer}
29  * does not do the rendering itself but instead dispatches to
30  * class-specific renderers. A class/renderer pair must be registered
31  * using the {@link #register} method. If a class has no registered
32  * renderer, the renderer of its closest superclass is used. Since the
33  * {@link ExtendableTreeCellRenderer} always has a default renderer
34  * for the {@link Object} class, rendering is always possible. The
35  * default {@link Object} renderer can be replaced by another renderer
36  * but it cannot be unregistered.</p>
37  *
38  * @author Rainer Klute <a
39  * HREF="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
40  * @version $Id: ExtendableTreeCellRenderer.java,v 1.3 2004/04/09 13:05:08 glens Exp $
41  * @since 2002-01-22
42  */

43 public class ExtendableTreeCellRenderer implements TreeCellRenderer
44 {
45
46     /**
47      * <p>Maps classes to renderers.</p>
48      */

49     protected Map renderers;
50
51
52
53     public ExtendableTreeCellRenderer()
54     {
55         renderers = new HashMap();
56         register(Object JavaDoc.class, new DefaultTreeCellRenderer()
57             {
58                 public Component getTreeCellRendererComponent
59                     (JTree tree, Object JavaDoc value, boolean selected,
60                      boolean expanded, boolean leaf, int row, boolean hasFocus)
61                 {
62                     final String JavaDoc 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     /**
77      * <p>Registers a renderer for a class.</p>
78      **/

79     public void register(final Class JavaDoc c, final TreeCellRenderer renderer)
80     {
81         renderers.put(c, renderer);
82     }
83
84
85
86     /**
87      * <p>Unregisters a renderer for a class. The renderer for the
88      * {@link Object} class cannot be unregistered.</p>
89      */

90     public void unregister(final Class JavaDoc c)
91     {
92         if (c == Object JavaDoc.class)
93             throw new IllegalArgumentException JavaDoc
94                 ("Renderer for Object cannot be unregistered.");
95         renderers.put(c, null);
96     }
97
98
99
100     /**
101      * <p>Renders an object in a tree cell depending of the object's
102      * class.</p>
103      *
104      * @see TreeCellRenderer#getTreeCellRendererComponent
105      */

106     public Component getTreeCellRendererComponent
107         (final JTree tree, final Object JavaDoc value, final boolean selected,
108          final boolean expanded, final boolean leaf, final int row,
109          final boolean hasFocus)
110     {
111         final String JavaDoc NULL = "null";
112         TreeCellRenderer r;
113         Object JavaDoc 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     /**
131      * <p>Find the renderer for the specified class.</p>
132      */

133     protected TreeCellRenderer findRenderer(final Class JavaDoc c)
134     {
135         final TreeCellRenderer r = (TreeCellRenderer) renderers.get(c);
136         if (r != null)
137             /* The class has a renderer. */
138             return r;
139
140         /* The class has no renderer, try the superclass, if any. */
141         final Class JavaDoc superclass = c.getSuperclass();
142         if (superclass != null)
143             return findRenderer(superclass);
144         else
145             return null;
146     }
147
148 }
149
Popular Tags