KickJava   Java API By Example, From Geeks To Geeks.

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


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
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * 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, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * 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.DefaultMutableTreeNode JavaDoc;
41 import javax.swing.tree.TreeCellRenderer JavaDoc;
42
43 import com.genimen.djeneric.tools.modeler.ModelEditor;
44 import com.genimen.djeneric.util.DjLogger;
45
46 public class ExtentNodeRenderer extends JLabel JavaDoc implements TreeCellRenderer JavaDoc
47 {
48   private static final long serialVersionUID = 1L;
49   protected static Font JavaDoc defaultFont;
50   protected final static Color JavaDoc SelectedBackgroundColor = Color.yellow;
51   //(0, 0, 128);
52

53   static
54   {
55     try
56     {
57       defaultFont = new Font JavaDoc("SansSerif", 0, 12);
58     }
59     catch (Exception JavaDoc e)
60     {
61       DjLogger.log(e);
62     }
63   }
64
65   /**
66    * Whether or not the item that was last configured is selected.
67    */

68   protected boolean selected;
69
70   /**
71    * This is messaged from JTree whenever it needs to get the size of the
72    * component or it wants to draw it. This attempts to set the font based on
73    * value, which will be a GTreeNode.
74    *
75    * @param tree
76    * Description of the Parameter
77    * @param value
78    * Description of the Parameter
79    * @param selected
80    * Description of the Parameter
81    * @param expanded
82    * Description of the Parameter
83    * @param leaf
84    * Description of the Parameter
85    * @param row
86    * Description of the Parameter
87    * @param hasFocus
88    * Description of the Parameter
89    * @return The treeCellRendererComponent value
90    */

91   public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value, boolean selected, boolean expanded,
92                                                 boolean leaf, int row, boolean hasFocus)
93   {
94     if (value == null) return (this);
95     String JavaDoc stringValue = value.toString();
96     setText(stringValue);
97     setToolTipText(stringValue);
98
99     /* Set the image. */
100     if (value instanceof ExtentNode)
101     {
102       setIcon(((ExtentNode) (value)).getImageIcon());
103     }
104     else if (value instanceof DefaultMutableTreeNode JavaDoc)
105     {
106       setIcon(ModelEditor.getImageIcon("empty.gif"));
107     }
108
109     /* Update the selected flag for the next paint. */
110     this.selected = selected;
111
112     return (this);
113   }
114
115   /**
116    * paint is subclassed to draw the background correctly. JLabel currently
117    * does not allow backgrounds other than white, and it will also fill behind
118    * the icon. Something that isn't desirable.
119    *
120    * @param g
121    * Description of the Parameter
122    */

123   public void paint(Graphics JavaDoc g)
124   {
125     Color JavaDoc bColor;
126     Icon JavaDoc currentI = getIcon();
127
128     if (selected)
129     {
130       bColor = SelectedBackgroundColor;
131     }
132     else if (getParent() != null)
133     {
134       /*
135        * Pick background color up from parent (which will come from
136        */

137       bColor = getParent().getBackground();
138     }
139     else
140     {
141       bColor = getBackground();
142     }
143     g.setColor(bColor);
144
145     if (currentI != null && getText() != null)
146     {
147       int offset = (currentI.getIconWidth() + getIconTextGap());
148       g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
149     }
150     else
151     {
152       g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
153     }
154     super.paint(g);
155   }
156 }
Popular Tags