KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > userperspective > ResourceFolderRenderer


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.modeler.userperspective;
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.modeler.ModelEditor;
43
44 public class ResourceFolderRenderer 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     setIcon(ModelEditor.getImageIcon("folder.gif"));
84
85     /* Update the selected flag for the next paint. */
86     _selected = selected;
87
88     return this;
89   }
90
91   /**
92    * paint is subclassed to draw the background correctly. JLabel currently
93    * does not allow backgrounds other than white, and it will also fill behind
94    * the icon. Something that isn't desirable.
95    *
96    *@param g Description of the Parameter
97    */

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