KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > tree > SmartTreeCellRenderer


1 package com.ca.directory.jxplorer.tree;
2
3 import javax.swing.*;
4 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
5 import java.awt.*;
6
7 /**
8  * SmartTreeCellRenderer replaces the DefaultTreeCellRenderer. The display is
9  * modified so that the tree elements are displayed similarly to a windows
10  * env. throughout all UIs (the default Motif display was (April '99)
11  * way broken; black on dark grey display was difficult to read and looked
12  * horrible). This class also handles the objectClass sensitive display
13  * icons. <p>
14  *
15  * As with most swing components, the tree display is horribly broken.
16  * Despite the use of a custom renderer, icons larger than 16x16 appear
17  * to be truncated in any look and feel apart from 'java'. Surprise? I'm
18  * about to fall over and die from not-Surprise.
19  *
20  */

21  
22 public class SmartTreeCellRenderer extends DefaultTreeCellRenderer JavaDoc
23 {
24     boolean useIcons = true;
25  
26     public JLabel displayLabel = new JLabel("label");
27  
28     private static final Color BLUE = new Color(0x000077);
29     private static final Color WHITE = Color.white;
30     private static final Color BLACK = Color.black;
31     
32     private Color currentBackground = null;
33     private Color currentForeground = null;
34     private ImageIcon currentIcon = null;
35     
36     /**
37      * Default constructor. All the intelligence is added by overloading
38      * the methods below.
39      */

40     public SmartTreeCellRenderer()
41     {
42         super();
43     }
44
45     /**
46      *
47      * Constructor setting whether to use icons when rendering cells
48      * (otherwise defaults to true).
49      *
50      * @param usingIcons whether icons are active.
51      */

52             
53     public SmartTreeCellRenderer(boolean usingIcons)
54     {
55         this();
56         useIcons = usingIcons;
57     }
58
59     /**
60      * Overloaded renderer is used to a) get the foreground and background
61      * working sanely on all platforms (was screwed under motif), and
62      * to set up our 'node-type-dependant' icon images, rather than the
63      * default 'open' or 'close' icon images.<p>
64      *
65      * <i>If</i> the passed object implements a 'getIcon()' method returning a
66      * javax.swing.Icon, and useIcons
67      * is true, the getIcon method will be used to display a graphic
68      * (implemented via reflection in order to conform with treeCellRenderer interface... :-) ).
69      *
70      * @param tree the tree to display this component within
71      * @param value the object to display
72      * @param selected has the object been selected by the user?
73      * @param expanded is the object tree node displaying children?
74      * @param leaf is the object without child nodes?
75      * @param row the display row of the object.
76      * @param hasFocus whether the object has GUI focus (which is different from 'selected',
77      * but here is displayed the same, since we don't allow multiple selections)
78      */

79      
80     public Component getTreeCellRendererComponent(JTree tree, Object JavaDoc value,
81         boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
82     {
83         Object JavaDoc icon = null;
84     
85         if (value instanceof SmartNode)
86         {
87             SmartNode node = (SmartNode) value;
88         
89             if (selected)
90             /* only one entry should ever be selected, so we know we
91              * can't reuse the label without changing the colors...
92              */

93             {
94                 currentForeground = WHITE;
95                 currentBackground = BLUE;
96                 displayLabel.setForeground(currentForeground);
97                 displayLabel.setBackground(currentBackground);
98                 displayLabel.setOpaque(true);
99             }
100             else
101             /* This is the most common option - so we can usually
102              * reuse the label as is.
103              */

104             {
105                 if (currentBackground != WHITE)
106                 /*
107                  * We've just come from a selected option - reset
108                  * the colours.
109                  */

110                 {
111                     currentForeground = BLACK;
112                     currentBackground = WHITE;
113                     displayLabel.setForeground(currentForeground);
114                     displayLabel.setBackground(currentBackground);
115                     displayLabel.setOpaque(false);
116                 }
117             }
118
119
120             //TE: what kind of node is it & how to handle the label/text...
121
if (node.isMultiValued() == true) //TE: multivalued RDN.
122
{
123                 StringBuffer JavaDoc buffy = new StringBuffer JavaDoc();
124                 buffy.append(node.rdn.getRawVal(0));
125                 int size = node.rdn.size();
126                 for (int i=1; i<size; i++)
127                 {
128                     buffy.append(" + "); //TE: this + symbol should be red to distinguish it from being part of the naming attribute.
129
buffy.append(node.rdn.getRawVal(i));
130                 }
131         
132                 displayLabel.setText(buffy.toString());
133             }
134             else if (node.isDummy())
135             {
136                 displayLabel.setText(node.getDummyMessage());
137             }
138             else if (node.isBlankRoot())
139             {
140                 displayLabel.setText(node.getBlankRootName());
141             }
142             else //TE: normal node.
143
{
144                 displayLabel.setText(node.rdn.getRawVal(0));
145             }
146             
147             
148             //TE: set the icon...
149
try
150             {
151                 /*
152                  * Only reset the icon if it has actually changed...
153                  */

154                  
155                 if (node.getIcon() != currentIcon)
156                 {
157                     currentIcon = node.getIcon();
158                     displayLabel.setIcon(currentIcon);
159                 }
160             }
161             catch (Exception JavaDoc e) // If this fails, we'll just lose the icon
162
{
163                 //TE: ignore.
164
}
165
166
167             return displayLabel;
168         }
169         else
170         {
171             displayLabel.setText(value.toString()); //TE: this happens (for some reason) on Linux & Solaris if the Java L&F is set!
172
return displayLabel;
173         }
174     }
175 }
Popular Tags