KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > templateeditor > tree > TemplateTreeCellRenderer


1 package com.genimen.djeneric.tools.templateeditor.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 TemplateTreeCellRenderer 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 DefaultTemplateTreeNode)
46     {
47       setIcon(((DefaultTemplateTreeNode) (value)).getImageIcon());
48     }
49     else if (value instanceof DjenericTreeNode)
50     {
51       setIcon(((DjenericTreeNode) (value)).getImageIcon());
52     }
53     else
54     {
55       setIcon(Specifier.getImageIcon("document.gif")); //unknown type, should not occur burt nevertheless...
56
}
57
58     /* Update the selected flag for the next paint. */
59     _selected = selected;
60
61     return this;
62   }
63
64   /**
65    * paint is subclassed to draw the background correctly. JLabel
66    * currently does not allow backgrounds other than white, and it
67    * will also fill behind the icon. Something that isn't desirable.
68    */

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