KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jobs > structural > FolderJob


1 package org.oddjob.jobs.structural;
2
3
4 import org.oddjob.Iconic;
5 import org.oddjob.Stoppable;
6 import org.oddjob.Structural;
7 import org.oddjob.arooa.Lifecycle;
8 import org.oddjob.images.IconEvent;
9 import org.oddjob.images.IconHelper;
10 import org.oddjob.images.IconListener;
11 import org.oddjob.images.IconTip;
12 import org.oddjob.structural.ChildHelper;
13 import org.oddjob.structural.StructuralListener;
14
15 /**
16  * @oddjob.description Holds a colection of child job but does not
17  * execute them. Used to collect and organise jobs. The jobs can either
18  * be scheduled by a scheduler or run manually.
19  * <p>
20  * A folder has no state, it can't be run and it can't be stopped.
21  *
22  * @oddjob.example
23  *
24  * <pre>
25  * &lt;folder name="My Jobs"&gt;
26  * &lt;exec name="Morning Job" command="etc..." &gt;/>
27  * &lt;exec name="Afternoon Job" command="etc..." &gt;/>
28  * &lt;exec name="Manual Cleanup" command="etc..." &gt;/>
29  * &lt;/folder&gt;
30  * </pre>
31  *
32  * @author Rob Gordon
33  */

34
35 public class FolderJob
36             implements Structural, Iconic, Stoppable {
37
38     /** Child helper.
39      */

40     protected transient ChildHelper childHelper
41             = new ChildHelper(this);
42             
43     /**
44      * @oddojb.property
45      * @oddjob.description The name of the folder.
46      * @oddjob.required No.
47      */

48     private String JavaDoc name;
49
50     /**
51      * This flag is set once the object is destroyed
52      * Methods in subclass should check this flag.
53      */

54     protected transient volatile boolean destroyed;
55     
56     /**
57      * Set the job name. Used by subclasses to set the job name.
58      *
59      * @param name The name of the job.
60      */

61     synchronized public void setName(String JavaDoc name) {
62         if (destroyed) {
63             throw new IllegalStateException JavaDoc("[" + this + "] destroyed");
64         }
65         this.name = name;
66     }
67
68     /**
69      * Get the job name.
70      *
71      * @return The job name.
72      */

73     synchronized public String JavaDoc getName() {
74         return name;
75     }
76         
77     /**
78      * Add a child.
79      *
80      * @oddjob.element <i>Any</i>
81      * @oddjob.description The child jobs.
82      * @oddjob.required No, but pointless if missing.
83      *
84      * @param child A child
85      */

86     public void addComponent(Object JavaDoc child) {
87         childHelper.addChild(child);
88     }
89
90     /**
91      * Add a listener. The listener will immediately recieve add
92      * notifications for all existing children.
93      *
94      * @param listener The listener.
95      */

96     public void addStructuralListener(StructuralListener listener) {
97         if (destroyed) {
98             throw new IllegalStateException JavaDoc("[" + this + "] destroyed");
99         }
100         childHelper.addStructuralListener(listener);
101     }
102     
103     /**
104      * Remove a listener.
105      *
106      * @param listener The listner.
107      */

108     public void removeStructuralListener(StructuralListener listener) {
109         childHelper.removeStructuralListener(listener);
110     }
111     
112     /**
113      * Override toString.
114      */

115     public String JavaDoc toString() {
116         if (name == null) {
117             return getClass().getName();
118         }
119         else {
120             return name;
121         }
122     }
123
124     /**
125      * Return an icon tip for a given id. Part
126      * of the Iconic interface.
127      */

128     public IconTip iconForId(String JavaDoc iconId) {
129         if (iconId.equals("folder")) {
130             return new IconTip(
131                     IconHelper.class.getResource("Open16.gif"),
132                     "folder");
133         }
134         else {
135             return null;
136         }
137     }
138
139     /**
140      * Add an icon listener. Part of the Iconic
141      * interface.
142      *
143      * @param listener The listener.
144      */

145     public void addIconListener(IconListener listener) {
146         if (destroyed) {
147             throw new IllegalStateException JavaDoc("[" + this + "] destroyed");
148         }
149         listener.iconEvent(new IconEvent(this, "folder"));
150     }
151
152     /**
153      * Remove an icon listener. Part of the Iconic
154      * interface.
155      *
156      * @param listener The listener.
157      */

158     public void removeIconListener(IconListener listener) {
159     }
160
161     /*
162      *
163      */

164     public void stop() {
165         childHelper.stopChildren();
166     }
167     
168     /**
169      * Destroy this component.
170      */

171     public void destroy() {
172         if (destroyed) {
173             throw new IllegalStateException JavaDoc("[" + this + "] destroyed");
174         }
175         childHelper.stopChildren();
176         Lifecycle.destroy(childHelper.getChildren());
177         childHelper.removeAllChildren();
178         destroyed = true;
179     }
180     
181 }
182
Popular Tags