KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > console > nodes > CustomTreeCellRenderer


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.console.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.ImageIcon JavaDoc;
39 import javax.swing.JLabel JavaDoc;
40 import javax.swing.JTree JavaDoc;
41 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
42 import javax.swing.tree.TreeCellRenderer JavaDoc;
43
44 import com.genimen.djeneric.language.Messages;
45 import com.genimen.djeneric.util.DjLogger;
46
47 public class CustomTreeCellRenderer extends JLabel JavaDoc implements TreeCellRenderer JavaDoc
48 {
49   private static final long serialVersionUID = 1L;
50   protected static Font JavaDoc defaultFont;
51   protected static ImageIcon JavaDoc collapsedIcon;
52   protected static ImageIcon JavaDoc expandedIcon;
53   protected static ImageIcon JavaDoc emptyFolder;
54   protected final static Color JavaDoc SelectedBackgroundColor = Color.yellow;
55   //(0, 0, 128);
56

57   static
58   {
59     try
60     {
61       defaultFont = new Font JavaDoc("SansSerif", 0, 12);
62     }
63     catch (Exception JavaDoc e)
64     {
65       DjLogger.log(e);
66     }
67     try
68     {
69       collapsedIcon = CustomTreeNode.getImageIcon("images/folder.gif");
70       expandedIcon = CustomTreeNode.getImageIcon("images/document.gif");
71       emptyFolder = CustomTreeNode.getImageIcon("images/emptyfolder.gif");
72     }
73     catch (Exception JavaDoc e)
74     {
75       DjLogger.log(e);
76       System.out.println(Messages.getString("CustomTreeCellRenderer.CouldNotLoad", e.getMessage()));
77     }
78   }
79
80   /**
81    * Whether or not the item that was last configured is selected.
82    */

83   protected boolean selected;
84
85   /**
86    * This is messaged from JTree whenever it needs to get the size of the
87    * component or it wants to draw it. This attempts to set the font based on
88    * value, which will be a GTreeNode.
89    *
90    *@param tree Description of the Parameter
91    *@param value Description of the Parameter
92    *@param selected Description of the Parameter
93    *@param expanded Description of the Parameter
94    *@param leaf Description of the Parameter
95    *@param row Description of the Parameter
96    *@param hasFocus Description of the Parameter
97    *@return The treeCellRendererComponent value
98    */

99   public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value, boolean selected, boolean expanded,
100                                                 boolean leaf, int row, boolean hasFocus)
101   {
102     if (value == null) return (this);
103     String JavaDoc stringValue = value.toString();
104     setText(stringValue);
105     setToolTipText(stringValue);
106
107     /* Set the image. */
108     if (value instanceof CustomTreeNode)
109     {
110       setIcon(((CustomTreeNode) (value)).getImageIcon());
111     }
112     else if (value instanceof DefaultMutableTreeNode JavaDoc)
113     {
114       // Must be the root then..
115
setIcon(CustomTreeNode.getImageIcon("images/schema.gif"));
116     }
117     else
118     {
119       setIcon(CustomTreeNode.getImageIcon("images/document.gif"));
120       //unknown type, should not occur burt nevertheless...
121
}
122
123     /* Update the selected flag for the next paint. */
124     this.selected = selected;
125
126     return (this);
127   }
128
129   /**
130    * paint is subclassed to draw the background correctly. JLabel currently
131    * does not allow backgrounds other than white, and it will also fill behind
132    * the icon. Something that isn't desirable.
133    *
134    *@param g Description of the Parameter
135    */

136   public void paint(Graphics JavaDoc g)
137   {
138     Color JavaDoc bColor;
139     Icon JavaDoc currentI = getIcon();
140
141     if (selected)
142     {
143       bColor = SelectedBackgroundColor;
144     }
145     else if (getParent() != null)
146     {
147       /* Pick background color up from parent (which will come from
148        the JTree we're contained in). */

149       bColor = getParent().getBackground();
150     }
151     else
152     {
153       bColor = getBackground();
154     }
155     g.setColor(bColor);
156
157     if (currentI != null && getText() != null)
158     {
159       int offset = (currentI.getIconWidth() + getIconTextGap());
160       g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
161     }
162     else
163     {
164       g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
165     }
166     super.paint(g);
167   }
168 }
Popular Tags