KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > monitor > model > JobTreeModel


1 package org.oddjob.monitor.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.SwingUtilities JavaDoc;
8 import javax.swing.event.TreeModelEvent JavaDoc;
9 import javax.swing.event.TreeModelListener JavaDoc;
10 import javax.swing.tree.TreeModel JavaDoc;
11 import javax.swing.tree.TreeNode JavaDoc;
12 import javax.swing.tree.TreePath JavaDoc;
13
14
15 /**
16  * A model which provides the swing tree model of the Oddjob structure.
17  *
18  * @author Rob Gordon
19  */

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

33     public void setRootTreeNode(JobTreeNode node) {
34         this.root = node;
35     }
36     
37     /*
38      * (non-Javadoc)
39      * @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.TreeModelListener)
40      */

41     public void addTreeModelListener(TreeModelListener JavaDoc tml) {
42         synchronized (tmListeners) {
43             tmListeners.add(tml);
44         }
45     }
46
47     /*
48      * (non-Javadoc)
49      * @see javax.swing.tree.TreeModel#getChild(java.lang.Object, int)
50      */

51     public Object JavaDoc getChild(Object JavaDoc parent, int index) {
52         return ((JobTreeNode)parent).getChildAt(index);
53     }
54
55     /*
56      * (non-Javadoc)
57      * @see javax.swing.tree.TreeModel#isLeaf(java.lang.Object)
58      */

59     public boolean isLeaf(Object JavaDoc node) {
60         return ((JobTreeNode)node).isLeaf();
61     }
62
63     /*
64      * (non-Javadoc)
65      * @see javax.swing.tree.TreeModel#getChildCount(java.lang.Object)
66      */

67     public int getChildCount(Object JavaDoc parent) {
68         return ((JobTreeNode)parent).getChildCount();
69     }
70
71     /*
72      * (non-Javadoc)
73      * @see javax.swing.tree.TreeModel#getIndexOfChild(java.lang.Object, java.lang.Object)
74      */

75     public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
76         return ((JobTreeNode)parent).getIndex((JobTreeNode)child);
77     }
78     
79     /*
80      * (non-Javadoc)
81      * @see javax.swing.tree.TreeModel#getRoot()
82      */

83     public Object JavaDoc getRoot() {
84         return root;
85     }
86
87     public void removeTreeModelListener(TreeModelListener JavaDoc tml) {
88         tmListeners.remove(tml);
89     }
90
91     /*
92      * (non-Javadoc)
93      * @see javax.swing.tree.TreeModel#valueForPathChanged(javax.swing.tree.TreePath, java.lang.Object)
94      */

95     public void valueForPathChanged(TreePath JavaDoc path, Object JavaDoc newValue) {
96         throw new UnsupportedOperationException JavaDoc("Don't need valueForPathChanged!");
97     }
98
99     public void fireTreeNodesChanged(TreeNode JavaDoc changed) {
100
101         LinkedList JavaDoc list = new LinkedList JavaDoc();
102         for (TreeNode JavaDoc i = changed; i != null; i = i.getParent()) {
103             list.addFirst(i);
104         }
105
106         final TreeModelEvent JavaDoc event = new TreeModelEvent JavaDoc(changed, list.toArray());
107
108         synchronized (tmListeners) {
109             for (Enumeration JavaDoc e = tmListeners.elements();
110                     e.hasMoreElements(); ) {
111                 final TreeModelListener JavaDoc tml = (TreeModelListener JavaDoc)e.nextElement();
112                 Runnable JavaDoc r = new Runnable JavaDoc() {
113                     public void run() {
114                         tml.treeNodesChanged(event);
115                     }
116                 };
117                 if (SwingUtilities.isEventDispatchThread()) {
118                     r.run();
119                 } else {
120                     SwingUtilities.invokeLater(r);
121                 }
122             }
123         }
124     }
125
126     public void fireTreeNodesInserted(TreeNode JavaDoc changed, JobTreeNode child, int index) {
127
128         LinkedList JavaDoc list = new LinkedList JavaDoc();
129         
130         for (TreeNode JavaDoc i = changed; i != null; i = i.getParent()) {
131             list.addFirst(i);
132         }
133
134         int childIndecies[] = {index};
135         Object JavaDoc children [] = {child};
136
137         final TreeModelEvent JavaDoc event = new TreeModelEvent JavaDoc(changed, list.toArray(),
138                 childIndecies, children);
139
140         synchronized (tmListeners) {
141             for (Enumeration JavaDoc e = tmListeners.elements();
142                     e.hasMoreElements(); ) {
143                 final TreeModelListener JavaDoc tml = (TreeModelListener JavaDoc)e.nextElement();
144                 Runnable JavaDoc r = new Runnable JavaDoc() {
145                     public void run() {
146                         tml.treeNodesInserted(event);
147                     }
148                 };
149                 if (SwingUtilities.isEventDispatchThread()) {
150                     r.run();
151                 } else {
152                     SwingUtilities.invokeLater(r);
153                 }
154             }
155         }
156     }
157
158     public void fireTreeNodesRemoved(TreeNode JavaDoc changed, JobTreeNode child, int index) {
159
160         LinkedList JavaDoc list = new LinkedList JavaDoc();
161         
162         for (TreeNode JavaDoc i = changed; i != null; i = i.getParent()) {
163     
164             list.addFirst(i);
165         }
166
167         int childIndecies[] = {index};
168         Object JavaDoc children [] = {child};
169
170         final TreeModelEvent JavaDoc event = new TreeModelEvent JavaDoc(changed, list.toArray(),
171                 childIndecies, children);
172
173         synchronized (tmListeners) {
174             for (Enumeration JavaDoc e = tmListeners.elements();
175                     e.hasMoreElements(); ) {
176                 final TreeModelListener JavaDoc tml = (TreeModelListener JavaDoc)e.nextElement();
177                 Runnable JavaDoc r = new Runnable JavaDoc() {
178                     public void run() {
179                         tml.treeNodesRemoved(event);
180                     }
181                 };
182                 if (SwingUtilities.isEventDispatchThread()) {
183                     r.run();
184                 } else {
185                     SwingUtilities.invokeLater(r);
186                 }
187             }
188         }
189     }
190 }
191
Popular Tags