KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > model > DesignTreeModel


1 package org.oddjob.designer.model;
2
3 import java.util.Enumeration JavaDoc;
4 import java.util.LinkedList JavaDoc;
5 import java.util.Vector JavaDoc;
6
7 import javax.swing.event.TreeModelEvent JavaDoc;
8 import javax.swing.event.TreeModelListener JavaDoc;
9 import javax.swing.tree.TreeModel JavaDoc;
10 import javax.swing.tree.TreeNode JavaDoc;
11 import javax.swing.tree.TreePath JavaDoc;
12
13
14 /**
15  * A model which provide the sing tree model of the Oddjob structure.
16  *
17  * @author Rob Gordon
18  */

19
20 public class DesignTreeModel implements TreeModel JavaDoc {
21
22     /** Tree model listeners. */
23     private final Vector JavaDoc tmListeners = new Vector JavaDoc();
24     
25     /** The root node. */
26     private DesignTreeNode root;
27     
28     /**
29      * Constructor.
30      *
31      * @param node The top of the tree.
32      */

33     public DesignTreeModel(DesignComponent node) {
34         root = new DesignTreeNode(this, null, node);
35     }
36     
37     public void build() {
38         root.build();
39     }
40     
41     public void addTreeModelListener(TreeModelListener JavaDoc tml) {
42         synchronized (tmListeners) {
43             tmListeners.add(tml);
44         }
45     }
46
47     
48     public Object JavaDoc getChild(Object JavaDoc parent, int index) {
49         return ((DesignTreeNode)parent).getChildAt(index);
50     }
51
52     public boolean isLeaf(Object JavaDoc node) {
53         return ((DesignTreeNode)node).isLeaf();
54     }
55
56     public int getChildCount(Object JavaDoc parent) {
57         return ((DesignTreeNode)parent).getChildCount();
58     }
59
60     public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
61         return ((DesignTreeNode)parent).getIndex((DesignTreeNode)child);
62     }
63     
64     public Object JavaDoc getRoot() {
65         return root;
66     }
67
68     public void removeTreeModelListener(TreeModelListener JavaDoc tml) {
69         tmListeners.remove(tml);
70     }
71
72     public void valueForPathChanged(TreePath JavaDoc path, Object JavaDoc newValue) {
73         System.err.println("valueForPathChanged");
74     }
75
76     public void fireTreeNodesChanged(TreeNode JavaDoc changed) {
77         LinkedList JavaDoc list = new LinkedList JavaDoc();
78         for (TreeNode JavaDoc i = changed; i != null; i = i.getParent()) {
79             list.addFirst(i);
80         }
81         TreeModelEvent JavaDoc event = new TreeModelEvent JavaDoc(changed, list.toArray());
82
83         synchronized (tmListeners) {
84             for (Enumeration JavaDoc e = tmListeners.elements();
85                     e.hasMoreElements(); ) {
86                 TreeModelListener JavaDoc tml = (TreeModelListener JavaDoc)e.nextElement();
87                         tml.treeNodesChanged(event);
88             }
89         }
90     }
91
92     public void fireTreeNodesInserted(TreeNode JavaDoc changed, DesignTreeNode child, int index) {
93         LinkedList JavaDoc list = new LinkedList JavaDoc();
94         
95         for (TreeNode JavaDoc i = changed; i != null; i = i.getParent()) {
96             list.addFirst(i);
97         }
98
99         int childIndecies[] = {index};
100         Object JavaDoc children [] = {child};
101
102         TreeModelEvent JavaDoc event = new TreeModelEvent JavaDoc(changed, list.toArray(),
103                 childIndecies, children);
104
105         synchronized (tmListeners) {
106             for (Enumeration JavaDoc e = tmListeners.elements();
107                     e.hasMoreElements(); ) {
108                 TreeModelListener JavaDoc tml = (TreeModelListener JavaDoc)e.nextElement();
109                 tml.treeNodesInserted(event);
110             }
111         }
112     }
113
114     public void fireTreeNodesRemoved(TreeNode JavaDoc changed, DesignTreeNode child, int index) {
115         LinkedList JavaDoc list = new LinkedList JavaDoc();
116         
117         for (TreeNode JavaDoc i = changed; i != null; i = i.getParent()) {
118             list.addFirst(i);
119         }
120
121         int childIndecies[] = {index};
122         Object JavaDoc children [] = {child};
123
124         TreeModelEvent JavaDoc event = new TreeModelEvent JavaDoc(changed, list.toArray(),
125                 childIndecies, children);
126
127         synchronized (tmListeners) {
128             for (Enumeration JavaDoc e = tmListeners.elements();
129                     e.hasMoreElements(); ) {
130                 TreeModelListener JavaDoc tml = (TreeModelListener JavaDoc)e.nextElement();
131                 tml.treeNodesRemoved(event);
132             }
133         }
134     }
135 }
136
Popular Tags