1 package com.explosion.utilities.process;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import javax.swing.SwingUtilities;
24
25 import com.explosion.utilities.exception.ExceptionManager;
26 import com.explosion.utilities.process.threads.Finishable;
27 import com.explosion.utilities.process.threads.ProcessThread;
28 import com.explosion.utilities.process.threads.SimpleProcess;
29 import com.explosion.utilities.process.threads.SimpleProcessThread;
30 import com.explosion.utilities.process.threads.Updateable;
31
32 /***
33 * @author Stephen Cowx
34 *
35 * Subclass this fellow if you want a simple threadbased process. If the
36 * Finishable you pass it is not null, it will call the finish() method of that
37 * finishable once it is done.
38 *
39 * Thisis a Stackable process. This means that one instance of these can create
40 * and call the process method of any other.
41 *
42 * In this case, they would all share the same parentProcessControl and so would
43 * be able to check the status of the parentProcessControl. The
44 * parentProcessControl and other interested parties are however only aware of
45 * the top process and so only listen to the percent complete of that process.
46 *
47 * To implement one of these classes, one must implement the process method and
48 * ensure that you constantly check the isStopped() method to see if the
49 * parentprocess has been stopped. If it has, return immediately.
50 *
51 * Date created:@13-Feb-2003
52 */
53 public abstract class StackableSimpleProcess extends ExceptionManager implements SimpleProcess
54 {
55
56 private String statusText = "Processing";
57
58 private boolean processUnitComplete = false;
59
60 private ProcessThread parentProcessControl;
61
62 private Finishable finishable;
63
64 private Updateable updateable;
65
66 private boolean initialised = false;
67
68 private int percentComplete = 0;
69
70 private boolean isUserProcess = false;
71
72 public StackableSimpleProcess(Finishable finishable, StackableSimpleProcess parentProcess)
73 {
74 this(parentProcess);
75 this.finishable = finishable;
76 }
77
78 public StackableSimpleProcess(Updateable updateable, StackableSimpleProcess parentProcess)
79 {
80 this(parentProcess);
81 this.updateable = updateable;
82 }
83
84 public StackableSimpleProcess(Finishable finishable, Updateable updateable, StackableSimpleProcess parentProcess)
85 {
86 this(parentProcess);
87 this.finishable = finishable;
88 this.updateable = updateable;
89 }
90
91 public StackableSimpleProcess(StackableSimpleProcess parentProcess)
92 {
93 if (parentProcess == null)
94 {
95 parentProcessControl = new SimpleProcessThread();
96 parentProcessControl.setProcess(this);
97 } else
98 {
99 parentProcessControl = parentProcess.getProcessControl();
100 }
101 }
102
103 public abstract void process() throws Exception;
104
105 public void initialise() throws Exception
106 {}
107
108 public final void finalise() throws Exception
109 {
110 if (finishable != null) finishIt();
111 }
112
113 public final void finalise(Exception e)
114 {
115 if (finishable != null) finishIt();
116 }
117
118 private final void finishIt()
119 {
120 if (finishable != null)
121 {
122 Runnable doFinished = new Runnable()
123 {
124
125 public void run()
126 {
127 finishable.finish();
128 }
129 };
130 SwingUtilities.invokeLater(doFinished);
131 }
132 }
133
134 public final int getPercentComplete()
135 {
136 return percentComplete;
137 }
138
139 public final void setPercentComplete(int percentComplete)
140 {
141 this.percentComplete = percentComplete;
142 }
143
144 public final String getStatusText()
145 {
146 return statusText;
147 }
148
149 public final void setStatusText(String statusText)
150 {
151 this.statusText = statusText;
152 }
153
154 public final ProcessThread getProcessControl()
155 {
156 return parentProcessControl;
157 }
158
159 public final void setProcessControl(ProcessThread processThread)
160 {
161 parentProcessControl = processThread;
162 }
163
164 public final boolean isStopped()
165 {
166 parentProcessControl.checkSuspended();
167 return parentProcessControl.getStatus() == ProcessThread.THREAD_STOPPED;
168 }
169
170 /***
171 * @return Returns the updateable.
172 */
173 public Updateable getUpdateable()
174 {
175 return updateable;
176 }
177
178 /***
179 * Returns a boolean value indicating whther this is a user process or not
180 * @return
181 */
182 public boolean isUserProcess()
183 {
184 return this.isUserProcess;
185 }
186
187 /***
188 * Sets whtether or not this is a user process. True means that it is
189 * @param truth
190 */
191 public void setIsUserProcess(boolean truth)
192 {
193 this.isUserProcess = true;
194 }
195 }
196