KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > administrator > nodes > AdministratorTreeCellRenderer


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.administrator.nodes;
31
32 import java.awt.Color JavaDoc;
33 import java.awt.Component JavaDoc;
34 import java.awt.Font JavaDoc;
35 import java.awt.Graphics JavaDoc;
36
37 import javax.swing.Icon JavaDoc;
38 import javax.swing.JLabel JavaDoc;
39 import javax.swing.JTree JavaDoc;
40 import javax.swing.tree.TreeCellRenderer JavaDoc;
41
42 import com.genimen.djeneric.tools.administrator.Administrator;
43
44 public class AdministratorTreeCellRenderer extends JLabel JavaDoc implements TreeCellRenderer JavaDoc
45 {
46   private static final long serialVersionUID = 1L;
47   protected static Font JavaDoc _defaultFont;
48   protected final static Color JavaDoc _selectedBackgroundColor = Color.yellow;
49   //(0, 0, 128);
50

51   static
52   {
53     _defaultFont = new Font JavaDoc("SansSerif", 0, 12);
54   }
55
56   /**
57    * Whether or not the item that was last configured is selected.
58    */

59   protected boolean _selected;
60
61   /**
62    * This is messaged from JTree whenever it needs to get the size of the
63    * component or it wants to draw it. This attempts to set the font based on
64    * value, which will be a GTreeNode.
65    *
66    *@param tree Description of the Parameter
67    *@param value Description of the Parameter
68    *@param selected Description of the Parameter
69    *@param expanded Description of the Parameter
70    *@param leaf Description of the Parameter
71    *@param row Description of the Parameter
72    *@param hasFocus Description of the Parameter
73    *@return The treeCellRendererComponent value
74    */

75   public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value, boolean selected, boolean expanded,
76                                                 boolean leaf, int row, boolean hasFocus)
77   {
78     if (value == null) return this;
79     String JavaDoc stringValue = value.toString();
80     setText(stringValue);
81     setToolTipText(stringValue);
82
83     /* Set the image. */
84     if (value instanceof AdministratorTreeNode)
85     {
86       setIcon(((AdministratorTreeNode) (value)).getImageIcon());
87     }
88     else
89     {
90       setIcon(Administrator.getImageIcon("document.gif"));
91       //unknown type, should not occur burt nevertheless...
92
}
93
94     /* Update the selected flag for the next paint. */
95     _selected = selected;
96
97     return this;
98   }
99
100   /**
101    * paint is subclassed to draw the background correctly. JLabel currently
102    * does not allow backgrounds other than white, and it will also fill behind
103    * the icon. Something that isn't desirable.
104    *
105    *@param g Description of the Parameter
106    */

107   public void paint(Graphics JavaDoc g)
108   {
109     Color JavaDoc bColor;
110     Icon JavaDoc currentI = getIcon();
111
112     if (_selected)
113     {
114       bColor = _selectedBackgroundColor;
115     }
116     else if (getParent() != null)
117     {
118       // Pick background color up from parent (which will come from the JTree we're contained in).
119
bColor = getParent().getBackground();
120     }
121     else
122     {
123       bColor = getBackground();
124     }
125     g.setColor(bColor);
126
127     if (currentI != null && getText() != null)
128     {
129       int offset = (currentI.getIconWidth() + getIconTextGap());
130       g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
131     }
132     else
133     {
134       g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
135     }
136     super.paint(g);
137   }
138 }
Popular Tags