KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.genimen.djeneric.tools.modeler.userperspective;
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.tools.modeler.ModelEditor;
17 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode;
18
19 public class ScriptStructureTreeNode extends DefaultMutableTreeNode JavaDoc
20 {
21   private static final long serialVersionUID = 1L;
22   protected JTree JavaDoc _tree;
23   protected SimpleNode _node;
24   protected String JavaDoc _title = "";
25
26   public ScriptStructureTreeNode(JTree JavaDoc tree, SimpleNode node)
27   {
28     _tree = tree;
29     _node = node;
30   }
31
32   public ScriptStructureTreeNode()
33   {
34     _tree = null;
35     _node = null;
36   }
37
38   public ScriptStructureTreeNode(String JavaDoc title)
39   {
40     this();
41     _title = title;
42   }
43
44   public void setTree(JTree JavaDoc tree)
45   {
46     _tree = tree;
47   }
48
49   public String JavaDoc toString()
50   {
51     if (_node == null) return _title;
52     return _node.getNodeTitle();
53   }
54
55   public DefaultTreeModel JavaDoc getModel()
56   {
57     return (DefaultTreeModel JavaDoc) _tree.getModel();
58   }
59
60   public void insertAsChild(ScriptStructureTreeNode node)
61   {
62     getModel().insertNodeInto(node, this, getChildCount());
63   }
64
65   public SimpleNode getNode()
66   {
67     return _node;
68   }
69
70   public String JavaDoc getTitle()
71   {
72     return _title;
73   }
74
75   public void setTitle(String JavaDoc string)
76   {
77     _title = string;
78   }
79
80 }
81
82 class ParseTreeCellRenderer extends JLabel JavaDoc implements TreeCellRenderer JavaDoc
83 {
84   private static final long serialVersionUID = 1L;
85   static protected Font JavaDoc _defaultFont;
86   static protected final Color JavaDoc _selectedBackgroundColor = Color.yellow; //(0, 0, 128);
87
static ImageIcon JavaDoc _icon = null;
88
89   static
90   {
91     _defaultFont = new Font JavaDoc("SansSerif", 0, 12);
92   }
93
94   /** Whether or not the item that was last configured is selected. */
95   protected boolean _selected;
96
97   /**
98    * This is messaged from JTree whenever it needs to get the size
99    * of the component or it wants to draw it.
100    * This attempts to set the font based on value, which will be
101    * a GTreeNode.
102    */

103   public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value, boolean selected, boolean expanded,
104                                                 boolean leaf, int row, boolean hasFocus)
105   {
106     if (value == null) return this;
107     String JavaDoc stringValue = value.toString();
108     setText(stringValue);
109     setToolTipText(stringValue);
110
111     if (_icon == null) _icon = ModelEditor.getImageIcon("node.gif");
112     setIcon(_icon);
113
114     /* Update the selected flag for the next paint. */
115     _selected = selected;
116
117     return this;
118   }
119
120   /**
121    * paint is subclassed to draw the background correctly. JLabel
122    * currently does not allow backgrounds other than white, and it
123    * will also fill behind the icon. Something that isn't desirable.
124    */

125   public void paint(Graphics JavaDoc g)
126   {
127     Color JavaDoc bColor;
128     Icon JavaDoc currentI = getIcon();
129
130     if (_selected)
131     {
132       bColor = _selectedBackgroundColor;
133     }
134     else if (getParent() != null)
135     { // Pick background color up from parent (which will come from the JTree we're contained in).
136
bColor = getParent().getBackground();
137     }
138     else
139     {
140       bColor = getBackground();
141     }
142     g.setColor(bColor);
143
144     if (currentI != null && getText() != null)
145     {
146       int offset = (currentI.getIconWidth() + getIconTextGap());
147       g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
148     }
149     else
150     {
151       g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
152     }
153     super.paint(g);
154   }
155 }
Popular Tags