KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ist > coach > coachEmfClientComponents > gui > MOTree


1 package ist.coach.coachEmfClientComponents.gui;
2
3
4 import java.awt.*;
5 import javax.swing.*;
6 import javax.swing.tree.*;
7 import javax.swing.event.*;
8
9 public class MOTree extends JPanel implements Runnable JavaDoc {
10
11     protected DefaultMutableTreeNode rootNode;
12     protected DefaultTreeModel treeModel;
13     protected JTree tree;
14     private Toolkit toolkit = Toolkit.getDefaultToolkit();
15
16     private GuiClient gui_parent;
17     public MOTree(GuiClient gui_parent) {
18
19         rootNode = new DefaultMutableTreeNode(
20                 new MOTreeObject("coach.ist"));
21                 //new MOTreeObject("Root Node", "0.0"));
22
this.gui_parent = gui_parent;
23
24         treeModel = new DefaultTreeModel(rootNode);
25         treeModel.addTreeModelListener(new MOTreeModelListener());
26         tree = new JTree(treeModel);
27
28         tree.getSelectionModel().setSelectionMode
29                (TreeSelectionModel.SINGLE_TREE_SELECTION);
30         tree.setShowsRootHandles(true);
31         tree.addTreeSelectionListener(new MOTreeSelectionListener());
32
33                 //customising tree display
34
tree.putClientProperty("JTree.lineStyle", "Angled");
35         ToolTipManager.sharedInstance().registerComponent(tree);
36
37         tree.setCellRenderer(new MORenderer());
38
39         JScrollPane scrollPane = new JScrollPane(tree);
40         scrollPane.setPreferredSize(new Dimension(100, 200));
41         scrollPane.setMinimumSize(new Dimension(10, 10));
42         setLayout(new GridLayout(1,0));
43         add(scrollPane);
44     }
45
46     public void update() {
47     }
48 //HACK THREAD
49
public void run() {}
50
51     /** Remove all nodes except the root node. */
52     public void clear() {
53         rootNode.removeAllChildren();
54         treeModel.reload();
55     }
56
57     /** Remove the currently selected node. */
58     public void removeCurrentNode() {
59         TreePath currentSelection = tree.getSelectionPath();
60         if (currentSelection != null) {
61             DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)
62                          (currentSelection.getLastPathComponent());
63             MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
64             if (parent != null) {
65                 treeModel.removeNodeFromParent(currentNode);
66                 return;
67             }
68         }
69
70         // Either there was no selection, or the root was selected.
71
toolkit.beep();
72     }
73
74     public DefaultMutableTreeNode getCurrentTreeSelection() {
75
76         DefaultMutableTreeNode parentNode = null;
77         TreePath parentPath = tree.getSelectionPath();
78
79         if (parentPath == null) {
80             parentNode = rootNode;
81         } else {
82             parentNode = (DefaultMutableTreeNode)
83                          (parentPath.getLastPathComponent());
84         }
85
86         return parentNode;
87     }
88
89     /** Add child to the currently selected node. */
90     public DefaultMutableTreeNode addObject(Object JavaDoc child) {
91
92         return addObject(getCurrentTreeSelection(), child, true);
93     }
94
95     public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
96                                             Object JavaDoc child) {
97         return addObject(parent, child, false);
98     }
99
100     public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
101                                             Object JavaDoc child,
102                                             boolean shouldBeVisible) {
103         DefaultMutableTreeNode childNode =
104                 new DefaultMutableTreeNode(child);
105
106         if (parent == null) {
107             parent = rootNode;
108         }
109
110         treeModel.insertNodeInto(childNode, parent,
111                                  parent.getChildCount());
112
113         // Make sure the user can see the lovely new node.
114
if (shouldBeVisible) {
115             tree.scrollPathToVisible(new TreePath(childNode.getPath()));
116         }
117         return childNode;
118     }
119
120     class MOTreeModelListener implements TreeModelListener {
121         public void treeNodesChanged(TreeModelEvent e) {
122             DefaultMutableTreeNode node;
123             node = (DefaultMutableTreeNode)
124                      (e.getTreePath().getLastPathComponent());
125
126             /*
127              * If the event lists children, then the changed
128              * node is the child of the node we've already
129              * gotten. Otherwise, the changed node and the
130              * specified node are the same.
131              */

132             try {
133                 int index = e.getChildIndices()[0];
134                 node = (DefaultMutableTreeNode)
135                        (node.getChildAt(index));
136             } catch (NullPointerException JavaDoc exc) {}
137
138             System.out.println("The user has finished editing the node.");
139             System.out.println("New value: " + node.getUserObject());
140         }
141
142         public void treeNodesInserted(TreeModelEvent e) {
143
144             System.out.println("Tree node inserted!!!");
145             DefaultMutableTreeNode node;
146             node = (DefaultMutableTreeNode)
147                 (e.getTreePath().getLastPathComponent());
148
149             int index = e.getChildIndices()[0];
150                node = (DefaultMutableTreeNode)
151                          (node.getChildAt(index));
152             System.out.println("New value: " + ((MOTreeObject) node.getUserObject()).toString());
153
154         }
155         public void treeNodesRemoved(TreeModelEvent e) {
156         }
157
158         public void treeStructureChanged(TreeModelEvent e) {
159             System.out.println("Tree structure changed " + e);
160         }
161     }
162
163     class MOTreeSelectionListener implements TreeSelectionListener {
164
165         public void valueChanged(TreeSelectionEvent e) {
166
167             DefaultMutableTreeNode node = (DefaultMutableTreeNode)
168                       tree.getLastSelectedPathComponent();
169
170             if (node == null) return;
171
172             TreePath parentPath = tree.getSelectionPath();
173
174             MOTreeObject nodeInfo = (MOTreeObject) node.getUserObject();
175         //GuiClient.println("Node class = " + nodeInfo.getClass().getName());
176
//
177
// possibly the tree needs to be updated
178
//
179

180            boolean node_is_leaf = node.isLeaf();
181             String JavaDoc[] children = gui_parent.getContained(nodeInfo.key());
182
183             node.removeAllChildren();
184             if (!node_is_leaf)
185                 treeModel.reload(node);
186
187             if (children != null && children.length > 0) {
188                 for(int i = 0; i < children.length; i++)
189                     gui_parent.create(node, children[i]);
190             }
191
192         }
193
194     }
195
196     class MORenderer extends DefaultTreeCellRenderer {
197
198         public MORenderer() {
199         }
200
201         public Component getTreeCellRendererComponent(
202             JTree tree,
203             Object JavaDoc value,
204             boolean sel,
205             boolean expanded,
206             boolean leaf,
207             int row,
208             boolean hasFocus) {
209
210             super.getTreeCellRendererComponent(tree, value, sel,
211                 expanded, leaf, row,
212                 hasFocus);
213
214             DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
215             MOTreeObject mo = (MOTreeObject)(node.getUserObject());
216
217             if (leaf) {
218                 setIcon(mo.leafIcon);
219             }
220             else {
221                 setIcon(mo.nodeIcon);
222             }
223
224             setToolTipText(mo.toolTipText); //no tool tip
225
return this;
226
227         }
228     }
229 }
230
Popular Tags