KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > tree > GeneratorTreeCellRenderer


1 package com.genimen.djeneric.tools.generator.tree;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Component JavaDoc;
5 import java.awt.Font JavaDoc;
6 import java.awt.Graphics JavaDoc;
7
8 import javax.swing.Icon JavaDoc;
9 import javax.swing.JLabel JavaDoc;
10 import javax.swing.JTree JavaDoc;
11 import javax.swing.tree.TreeCellRenderer JavaDoc;
12
13 import com.genimen.djeneric.tools.specifier.Specifier;
14 import com.genimen.djeneric.tools.specifier.tree.DjenericTreeNode;
15
16 public class GeneratorTreeCellRenderer extends JLabel JavaDoc implements TreeCellRenderer JavaDoc
17 {
18   private static final long serialVersionUID = 1L;
19   static protected Font JavaDoc _defaultFont;
20   static protected final Color JavaDoc _selectedBackgroundColor = Color.yellow; //(0, 0, 128);
21

22   static
23   {
24     _defaultFont = new Font JavaDoc("SansSerif", 0, 12);
25   }
26
27   /** Whether or not the item that was last configured is selected. */
28   protected boolean _selected;
29
30   /**
31    * This is messaged from JTree whenever it needs to get the size
32    * of the component or it wants to draw it.
33    * This attempts to set the font based on value, which will be
34    * a GTreeNode.
35    */

36   public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value, boolean selected, boolean expanded,
37                                                 boolean leaf, int row, boolean hasFocus)
38   {
39     if (value == null) return this;
40     String JavaDoc stringValue = value.toString();
41     setText(stringValue);
42     setToolTipText(stringValue);
43
44     /* Set the image. */
45     if (value instanceof DjenericTreeNode)
46     {
47       setIcon(((DjenericTreeNode) (value)).getImageIcon());
48     }
49     else
50     {
51       setIcon(Specifier.getImageIcon("document.gif")); //unknown type, should not occur but nevertheless...
52
}
53
54     /* Update the selected flag for the next paint. */
55     _selected = selected;
56
57     return this;
58   }
59
60   /**
61    * paint is subclassed to draw the background correctly. JLabel
62    * currently does not allow backgrounds other than white, and it
63    * will also fill behind the icon. Something that isn't desirable.
64    */

65   public void paint(Graphics JavaDoc g)
66   {
67     Color JavaDoc bColor;
68     Icon JavaDoc currentI = getIcon();
69
70     if (_selected)
71     {
72       bColor = _selectedBackgroundColor;
73     }
74     else if (getParent() != null)
75     { // Pick background color up from parent (which will come from the JTree we're contained in).
76
bColor = getParent().getBackground();
77     }
78     else
79     {
80       bColor = getBackground();
81     }
82     g.setColor(bColor);
83
84     if (currentI != null && getText() != null)
85     {
86       int offset = (currentI.getIconWidth() + getIconTextGap());
87       g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
88     }
89     else
90     {
91       g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
92     }
93     super.paint(g);
94   }
95 }
Popular Tags