KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > test > ParseTreeNode


1 package com.genimen.djeneric.tools.generator.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.JLabel JavaDoc;
10 import javax.swing.JTree JavaDoc;
11 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
12 import javax.swing.tree.DefaultTreeModel JavaDoc;
13 import javax.swing.tree.TreeCellRenderer JavaDoc;
14
15 import com.genimen.djeneric.tools.generator.core.Node;
16 import com.genimen.djeneric.tools.generator.core.SimpleNode;
17
18 public class ParseTreeNode extends DefaultMutableTreeNode JavaDoc
19 {
20   private static final long serialVersionUID = 1L;
21   protected JTree JavaDoc _tree;
22   protected SimpleNode _node;
23
24   public ParseTreeNode(JTree JavaDoc tree, SimpleNode node)
25   {
26     _tree = tree;
27     _node = node;
28   }
29
30   public ParseTreeNode()
31   {
32     _tree = null;
33     _node = null;
34   }
35
36   public void setTree(JTree JavaDoc tree)
37   {
38     _tree = tree;
39   }
40
41   public String JavaDoc toString()
42   {
43     if (_node == null) return "";
44     return _node.getName();
45   }
46
47   public DefaultTreeModel JavaDoc getModel()
48   {
49     return (DefaultTreeModel JavaDoc) _tree.getModel();
50   }
51
52   public void insertAsChild(ParseTreeNode node)
53   {
54     getModel().insertNodeInto(node, this, getChildCount());
55   }
56
57   public Node JavaDoc getNode()
58   {
59     return _node;
60   }
61 }
62
63 class ParseTreeCellRenderer extends JLabel JavaDoc implements TreeCellRenderer JavaDoc
64 {
65   private static final long serialVersionUID = 1L;
66   static protected Font JavaDoc _defaultFont;
67   static protected final Color JavaDoc _selectedBackgroundColor = Color.yellow; //(0, 0, 128);
68

69   static
70   {
71     _defaultFont = new Font JavaDoc("SansSerif", 0, 12);
72   }
73
74   /** Whether or not the item that was last configured is selected. */
75   protected boolean _selected;
76
77   /**
78    * This is messaged from JTree whenever it needs to get the size
79    * of the component or it wants to draw it.
80    * This attempts to set the font based on value, which will be
81    * a GTreeNode.
82    */

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

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