KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.monitor.model;
2
3 import java.util.Enumeration JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Vector JavaDoc;
7
8 import javax.swing.tree.TreeNode JavaDoc;
9
10 import org.oddjob.Iconic;
11 import org.oddjob.Structural;
12 import org.oddjob.images.IconEvent;
13 import org.oddjob.images.IconHelper;
14 import org.oddjob.images.IconListener;
15 import org.oddjob.images.IconTip;
16 import org.oddjob.images.NoSuchIconException;
17 import org.oddjob.scheduling.OddjobScheduler;
18 import org.oddjob.structural.StructuralEvent;
19 import org.oddjob.structural.StructuralListener;
20
21 /**
22  * This class encapsualtes the model of a job to be
23  * used in the monitor.
24  *
25  * @author Rob Gordon
26  */

27
28
29 public class JobTreeNode
30         implements TreeNode JavaDoc, StructuralListener {
31
32     /** For list of children */
33     private final Vector JavaDoc nodeList = new Vector JavaDoc();
34
35     /** Parent node */
36     final private JobTreeNode parent;
37
38     /** Save the JobTreeModel. */
39     final private JobTreeModel model;
40
41     /** Save icon information */
42     private final OurIconListener iconListener = new OurIconListener();
43     private volatile IconTip iconTip = IconHelper.nullIcon;
44
45     /** The job this is modelling. */
46     final private Object JavaDoc component;
47     
48     final private ExplorerContext explorerContext;
49     
50     /**
51      * Constructor for the root node.
52      *
53      * @param explorerModel The ExplorerModel.
54      * @param model The JobTreeModel.
55      */

56     public JobTreeNode(ExplorerModel explorerModel, JobTreeModel model) {
57         this.parent = null;
58         this.model = model;
59         this.component = explorerModel.getRoot();
60         this.explorerContext = new ExplorerContext(explorerModel);
61     }
62     
63     /**
64      * Constructor for child nodes.
65      *
66      * @param parent The parent node.
67      * @param node The structure node this is modelling.
68      */

69     public JobTreeNode(JobTreeNode parent, Object JavaDoc node) {
70         if (parent == null) {
71             throw new NullPointerException JavaDoc("Parent must not be null!");
72         }
73         this.parent = parent;
74         this.model = parent.model;
75         this.component = node;
76         
77         this.explorerContext = new ExplorerContext(node, parent.getExplorerContext());
78     }
79     
80     
81     /**
82      * Build the tree by adding a structural listener to this node.
83      *
84      */

85     public void build() {
86         if (component instanceof Structural) {
87             ((Structural)component).addStructuralListener(this);
88         }
89     }
90
91     public void visible() {
92         iconListener.listen();
93     }
94     
95     public void inVisible() {
96         iconListener.dont();
97     }
98     
99     public void setIcon(IconTip iconTip) {
100         synchronized (this) {
101             this.iconTip = iconTip;
102         }
103         model.fireTreeNodesChanged(this);
104     }
105     
106     public Object JavaDoc getComponent() {
107         return component;
108     }
109     
110     // TreeNode methods
111

112     public Enumeration JavaDoc children() {
113         return nodeList.elements();
114     }
115
116     public boolean getAllowsChildren() {
117         return true;
118     }
119         
120     public TreeNode JavaDoc getChildAt(int index) {
121         return (JobTreeNode)nodeList.elementAt(index);
122     }
123
124     public int getChildCount() {
125         return nodeList.size();
126     }
127
128     public boolean isLeaf() {
129         return nodeList.size() == 0 ? true : false;
130     }
131
132     public int getIndex(TreeNode JavaDoc child) {
133
134         return nodeList.indexOf(child);
135     }
136
137     public TreeNode JavaDoc getParent() {
138         
139         return parent;
140     }
141
142     public String JavaDoc toString() {
143         return component.toString();
144     }
145
146     synchronized public IconTip getIcon() {
147         return iconTip;
148     }
149
150     public JobTreeNode[] getChildren() {
151         synchronized (nodeList) {
152             return (JobTreeNode[]) nodeList.toArray(new JobTreeNode[0]);
153         }
154     }
155         
156     /*
157      * (non-Javadoc)
158      * @see org.oddjob.structural.StructuralListener#childAdded(org.oddjob.structural.StructuralEvent)
159      */

160     public void childAdded(StructuralEvent e) {
161
162         int index = e.getIndex();
163         JobTreeNode child = new JobTreeNode(this, e.getChild());
164
165         synchronized (nodeList) {
166             nodeList.insertElementAt(child, index);
167         }
168
169         model.fireTreeNodesInserted(this, child, index);
170         child.build();
171     }
172     
173     /*
174      * (non-Javadoc)
175      * @see org.oddjob.structural.StructuralListener#childRemoved(org.oddjob.structural.StructuralEvent)
176      */

177     public void childRemoved(StructuralEvent e) {
178         
179         int index = e.getIndex();
180         JobTreeNode child = null;
181         synchronized (nodeList) {
182             child = (JobTreeNode)nodeList.elementAt(index);
183             nodeList.removeElementAt(index);
184         }
185         child.destroy();
186
187         model.fireTreeNodesRemoved(this, child, index);
188     }
189
190     public void destroy() {
191         while (nodeList.size() > 0) {
192             int index= nodeList.size() - 1;
193             JobTreeNode child = (JobTreeNode)nodeList.remove(index);
194             child.destroy();
195             model.fireTreeNodesRemoved(this, child, index);
196         }
197     
198         if (component instanceof Structural) {
199             ((Structural)component).removeStructuralListener(this);
200         }
201         if (component instanceof OddjobScheduler) {
202             explorerContext.getSchedulerTraker().removeSchedule(
203                     (OddjobScheduler) component);
204         }
205     }
206
207     class OurIconListener implements IconListener {
208         private final Map JavaDoc icons = new HashMap JavaDoc();
209         
210         void listen() {
211             if (component instanceof Iconic) {
212                 ((Iconic)component).addIconListener(this);
213             }
214         }
215
216         public void iconEvent(IconEvent event) {
217             String JavaDoc iconId = event.getIconId();
218             IconTip it = (IconTip) icons.get(iconId);
219             if (it == null) {
220                 try {
221                     it = ((Iconic)component).iconForId(iconId);
222                 } catch (NoSuchIconException e) {
223                     e.printStackTrace();
224                     it = IconHelper.nullIcon;
225                 }
226                 icons.put(iconId, it);
227             }
228             setIcon(it);
229         }
230         
231         public void dont() {
232             if (component instanceof Iconic) {
233                 ((Iconic)component).removeIconListener(this);
234             }
235         }
236     }
237     
238     /**
239      * @return Returns the context.
240      */

241     public ExplorerContext getExplorerContext() {
242         return explorerContext;
243     }
244 }
245
Popular Tags