KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > monitor > action > StopAction


1 package org.oddjob.monitor.action;
2
3 import org.oddjob.Stateful;
4 import org.oddjob.Stoppable;
5 import org.oddjob.designer.model.DesignDefinition;
6 import org.oddjob.monitor.model.ExplorerContext;
7 import org.oddjob.monitor.model.JobAction;
8 import org.oddjob.state.JobState;
9 import org.oddjob.state.JobStateEvent;
10 import org.oddjob.state.JobStateListener;
11 import org.oddjob.util.ThreadManager;
12
13 /**
14  * Implement a stop action.
15  *
16  * @author Rob Gordon
17  */

18
19 public class StopAction extends JobAction
20 implements JobStateListener {
21
22     /** The job. */
23     private Object JavaDoc job = null;
24     
25     /** The ThreadManager that will run the stop. */
26     private ThreadManager threadManager;
27     
28     /** The enabled. */
29     private boolean enabled;
30     
31     /*
32      * (non-Javadoc)
33      * @see org.oddjob.monitor.model.JobOption#getName()
34      */

35     public String JavaDoc getName() {
36         return "Stop";
37     }
38
39     /*
40      * (non-Javadoc)
41      * @see org.oddjob.monitor.model.JobOption#select(java.lang.Object, org.oddjob.monitor.model.ExplorerContext)
42      */

43     protected void select(Object JavaDoc component, ExplorerContext eContext) {
44         if (!(component instanceof Stoppable)) {
45             this.job = null;
46             this.enabled = false;
47             return;
48         }
49         
50         this.job = component;
51         this.enabled = true;
52         this.threadManager = eContext.getThreadManager();
53         
54         if (job instanceof Stateful) {
55             ((Stateful) job).addJobStateListener(this);
56         }
57     }
58     
59     /*
60      * (non-Javadoc)
61      * @see org.oddjob.monitor.model.JobOption#deSelect()
62      */

63     protected void deSelect() {
64         if (job instanceof Stateful) {
65             ((Stateful) job).removeJobStateListener(this);
66         }
67         job = null;
68     }
69     
70     /*
71      * (non-Javadoc)
72      * @see org.oddjob.monitor.model.JobOption#enabled()
73      */

74     public boolean enabled() {
75         return enabled;
76     }
77     
78     /*
79      * (non-Javadoc)
80      * @see org.oddjob.monitor.model.JobAction#form()
81      */

82     public DesignDefinition form() {
83         return null;
84     }
85     
86     /*
87      * (non-Javadoc)
88      * @see org.oddjob.monitor.model.JobAction#action()
89      */

90     public void action() throws Exception JavaDoc {
91         threadManager.run(new Runnable JavaDoc() {
92             public void run() {
93                 ((Stoppable) job).stop();
94             }
95         }, "Stopping " + job);
96     }
97     
98     /*
99      * (non-Javadoc)
100      * @see org.oddjob.state.JobStateListener#jobStateChange(org.oddjob.state.JobStateEvent)
101      */

102     public void jobStateChange(JobStateEvent event) {
103         if (event.getJobState() == JobState.EXECUTING
104                 && event.getSource() instanceof Stoppable) {
105             enabled = true;
106         } else {
107             enabled = false;
108         }
109         setChanged();
110         notifyObservers();
111     }
112 }
113
Popular Tags