KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > oql > test > ParseTreeNode


1 package com.genimen.djeneric.repository.oql.test;
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.ImageIcon JavaDoc;
10 import javax.swing.JLabel JavaDoc;
11 import javax.swing.JTree JavaDoc;
12 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
13 import javax.swing.tree.DefaultTreeModel JavaDoc;
14 import javax.swing.tree.TreeCellRenderer JavaDoc;
15
16 import com.genimen.djeneric.repository.oql.core.Node;
17 import com.genimen.djeneric.repository.oql.core.SimpleNode;
18
19 public class ParseTreeNode extends DefaultMutableTreeNode JavaDoc
20 {
21   private static final long serialVersionUID = 3256444707163813683L;
22   protected JTree JavaDoc _tree;
23   protected SimpleNode _node;
24
25   public ParseTreeNode(JTree JavaDoc tree, SimpleNode node)
26   {
27     _tree = tree;
28     _node = node;
29   }
30
31   public ParseTreeNode()
32   {
33     _tree = null;
34     _node = null;
35   }
36
37   public void setTree(JTree JavaDoc tree)
38   {
39     _tree = tree;
40   }
41
42   public String JavaDoc toString()
43   {
44     if (_node == null) return "";
45     return _node.toString();
46   }
47
48   public DefaultTreeModel JavaDoc getModel()
49   {
50     return (DefaultTreeModel JavaDoc) _tree.getModel();
51   }
52
53   public void insertAsChild(ParseTreeNode node)
54   {
55     getModel().insertNodeInto(node, this, getChildCount());
56   }
57
58   public Node JavaDoc getNode()
59   {
60     return _node;
61   }
62 }
63
64 class ParseTreeCellRenderer extends JLabel JavaDoc implements TreeCellRenderer JavaDoc
65 {
66   private static final long serialVersionUID = 3833466210397271607L;
67   static protected Font JavaDoc _defaultFont;
68   static protected final Color JavaDoc _selectedBackgroundColor = Color.yellow; //(0, 0, 128);
69
static ImageIcon JavaDoc _icon = null;
70
71   static
72   {
73     _defaultFont = new Font JavaDoc("SansSerif", 0, 12);
74   }
75
76   /** Whether or not the item that was last configured is selected. */
77   protected boolean _selected;
78
79   /**
80    * This is messaged from JTree whenever it needs to get the size
81    * of the component or it wants to draw it.
82    * This attempts to set the font based on value, which will be
83    * a GTreeNode.
84    */

85   public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value, boolean selected, boolean expanded,
86                                                 boolean leaf, int row, boolean hasFocus)
87   {
88     if (value == null) return this;
89     String JavaDoc stringValue = value.toString();
90     setText(stringValue);
91     setToolTipText(stringValue);
92
93     if (_icon == null) _icon = DjOqlParserTest.getImageIcon("node.gif");
94     setIcon(_icon);
95
96     /* Update the selected flag for the next paint. */
97     _selected = selected;
98
99     return this;
100   }
101
102   /**
103    * paint is subclassed to draw the background correctly. JLabel
104    * currently does not allow backgrounds other than white, and it
105    * will also fill behind the icon. Something that isn't desirable.
106    */

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