KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > command > Worker


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18

19 package org.columba.core.command;
20
21 import java.util.List JavaDoc;
22 import java.util.Vector JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24
25 import javax.swing.event.EventListenerList JavaDoc;
26
27 import org.columba.api.command.IWorkerStatusChangeListener;
28 import org.columba.api.command.IWorkerStatusController;
29 import org.columba.api.command.WorkerStatusChangedEvent;
30 import org.columba.api.exception.IExceptionListener;
31 import org.columba.core.base.SwingWorker;
32
33 /**
34  * Worker additionally sends status information updates to the
35  * {@link TaskManager}.
36  * <p>
37  * This updates get displayed in the StatusBar.
38  * <p>
39  * Note that {@link Command}objects get {@link Worker}objects only when
40  * executed.
41  *
42  * @author fdietz
43  */

44 public class Worker extends SwingWorker implements IWorkerStatusController {
45
46     private static final Logger JavaDoc LOG = Logger
47         .getLogger("org.columba.core.command"); //$NON-NLS-1$
48

49     /**
50          * Constant definining the delay used when using
51          * clearDisplayTextWithDelay(). Defined to be 500 millisec.
52          */

53     private static final int CLEAR_DELAY = 500;
54
55     protected Command op;
56
57     protected int operationMode;
58
59     protected CommandProcessor boss;
60
61     protected String JavaDoc displayText;
62
63     protected int progressBarMax;
64
65     protected int progressBarValue;
66
67     protected boolean cancelled;
68
69     protected List JavaDoc<IWorkerStatusChangeListener> workerStatusChangeListeners;
70
71     private int timeStamp;
72
73     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
74
75     public Worker(CommandProcessor parent) {
76     super();
77
78     this.boss = parent;
79
80     displayText = ""; //$NON-NLS-1$
81
progressBarValue = 0;
82     progressBarMax = 0;
83
84     cancelled = false;
85
86     workerStatusChangeListeners = new Vector JavaDoc<IWorkerStatusChangeListener>();
87     }
88
89     public void process(Command theCommand, int theOperationMode,
90         int theTimeStamp) {
91     this.op = theCommand;
92     this.operationMode = theOperationMode;
93     this.timeStamp = theTimeStamp;
94     }
95
96     public int getPriority() {
97     return op.getPriority();
98     }
99
100     private void returnLocks(int opMode) {
101     op.releaseAllFolderLocks();
102     }
103
104     /**
105          * Method runs in background. Every Command.execute method is wrapped
106          * here.
107          * <p>
108          * All general exceptions are caught here, nice error dialogs shown to
109          * the users.
110          *
111          * @see org.columba.core.base.SwingWorker#construct()
112          */

113     @Override JavaDoc
114     public Object JavaDoc construct() {
115
116     try {
117         op.process(this);
118
119     } catch (CommandCancelledException e) {
120         LOG.info("Command cancelled: " + this); //$NON-NLS-1$
121
} catch (Exception JavaDoc e) {
122
123         // exception handler should handle all error handling stuff
124
fireExceptionOccured(e);
125     }
126
127     returnLocks(operationMode);
128
129     return null;
130     }
131
132     @Override JavaDoc
133     public void finished() {
134     try {
135         op.finish();
136     } catch (Exception JavaDoc e) {
137         // Must create a ExceptionProcessor
138
e.printStackTrace();
139     }
140
141     unregister();
142     boss.operationFinished(op, this);
143     }
144
145     private void unregister() {
146     TaskManager.getInstance().unregister(threadVar);
147
148     WorkerStatusChangedEvent e = new WorkerStatusChangedEvent(this,
149         getTimeStamp());
150     e.setType(WorkerStatusChangedEvent.FINISHED);
151     fireWorkerStatusChanged(e);
152     workerStatusChangeListeners.clear();
153     displayText = ""; //$NON-NLS-1$
154
progressBarValue = 0;
155     progressBarMax = 0;
156     }
157
158     /**
159          * Sets the maximum value for the progress bar.
160          *
161          * @param max
162          * New max. value for progress bar
163          */

164     public void setProgressBarMaximum(int max) {
165     WorkerStatusChangedEvent e = new WorkerStatusChangedEvent(this,
166         getTimeStamp());
167     e.setType(WorkerStatusChangedEvent.PROGRESSBAR_MAX_CHANGED);
168     e.setOldValue(new Integer JavaDoc(progressBarMax));
169
170     progressBarMax = max;
171
172     e.setNewValue(new Integer JavaDoc(progressBarMax));
173     fireWorkerStatusChanged(e);
174     }
175
176     /**
177          * Sets the current value of the progress bar.
178          *
179          * @param aValue
180          * New current value of progress bar
181          */

182     public void setProgressBarValue(int aValue) {
183     WorkerStatusChangedEvent e = new WorkerStatusChangedEvent(this,
184         getTimeStamp());
185     e.setType(WorkerStatusChangedEvent.PROGRESSBAR_VALUE_CHANGED);
186     e.setOldValue(new Integer JavaDoc(progressBarValue));
187
188     progressBarValue = aValue;
189
190     e.setNewValue(new Integer JavaDoc(progressBarValue));
191     fireWorkerStatusChanged(e);
192     }
193
194     /**
195          * Sets the progress bar value to zero, i.e. clears the progress bar.
196          * This is the same as calling setProgressBarValue(0)
197          */

198     public void resetProgressBar() {
199     setProgressBarValue(0);
200     }
201
202     /**
203          * Returns the max. value for the progress bar
204          */

205     public int getProgessBarMaximum() {
206     return progressBarMax;
207     }
208
209     /**
210          * Returns the current value for the progress bar
211          */

212     public int getProgressBarValue() {
213     return progressBarValue;
214     }
215
216     /**
217          * Returns the text currently displayed in the status bar
218          */

219     public String JavaDoc getDisplayText() {
220     return displayText;
221     }
222
223     /**
224          * Set the text to be displayed in the status bar
225          *
226          * @param text
227          * Text to display in status bar
228          */

229     public void setDisplayText(String JavaDoc text) {
230     WorkerStatusChangedEvent e = new WorkerStatusChangedEvent(this,
231         getTimeStamp());
232     e.setType(WorkerStatusChangedEvent.DISPLAY_TEXT_CHANGED);
233     e.setOldValue(displayText);
234
235     displayText = text;
236
237     e.setNewValue(displayText);
238     fireWorkerStatusChanged(e);
239     }
240
241     /**
242          * Clears the text displayed in the status bar - without any delay
243          */

244     public void clearDisplayText() {
245     clearDisplayText(0);
246     }
247
248     /**
249          * Clears the text displayed in the status bar - with a given delay. The
250          * delay used is 500 ms. <br>
251          * If a new text is set within this delay, the text is not cleared.
252          */

253     public void clearDisplayTextWithDelay() {
254     clearDisplayText(CLEAR_DELAY);
255     }
256
257     /**
258          * Clears the text displayed in the status bar - with a given delay. If
259          * a new text is set within this delay, the text is not cleared.
260          *
261          * @param delay
262          * Delay in milliseconds before clearing the text
263          */

264     private void clearDisplayText(int delay) {
265     // init event
266
WorkerStatusChangedEvent e = new WorkerStatusChangedEvent(this,
267         getTimeStamp());
268     e.setType(WorkerStatusChangedEvent.DISPLAY_TEXT_CLEARED);
269
270     // "new value" is used to pass on the delay
271
e.setNewValue(new Integer JavaDoc(delay));
272
273     // set display text stored here to an empty string (~ cleared)
274
displayText = ""; //$NON-NLS-1$
275

276     // fire event
277
fireWorkerStatusChanged(e);
278     }
279
280     public void addWorkerStatusChangeListener(IWorkerStatusChangeListener l) {
281     workerStatusChangeListeners.add(l);
282     }
283
284     public void removeWorkerStatusChangeListener(IWorkerStatusChangeListener l) {
285     workerStatusChangeListeners.remove(l);
286     }
287
288     protected void fireWorkerStatusChanged(WorkerStatusChangedEvent e) {
289     // if we use the commented statement, the exceptio
290
// java.util.ConcurrentModificationException
291
// is thrown ... is the worker not thread save?
292
// for (Iterator it = workerStatusChangeListeners.iterator();
293
// it.hasNext();) {
294
// ((WorkerStatusChangeListener) it.next()).workerStatusChanged(e);
295
for (int i = 0; i < workerStatusChangeListeners.size(); i++) {
296         workerStatusChangeListeners.get(i).workerStatusChanged(e);
297     }
298     }
299
300     public void cancel() {
301     cancelled = true;
302     }
303
304     public boolean cancelled() {
305     return cancelled;
306     }
307
308     /**
309          * Returns the timeStamp.
310          *
311          * @return int
312          */

313     public int getTimeStamp() {
314     return timeStamp;
315     }
316
317     /**
318          * Adds a listener.
319          */

320     public void addExceptionListener(IExceptionListener l) {
321     listenerList.add(IExceptionListener.class, l);
322     }
323
324     /**
325          * Removes a previously registered listener.
326          */

327     public void removeExceptionListener(IExceptionListener l) {
328     listenerList.remove(IExceptionListener.class, l);
329     }
330
331     /**
332          * Notify all listeners of the exception.
333          *
334          * @param e
335          * exception
336          */

337     private void fireExceptionOccured(Exception JavaDoc e) {
338     // Guaranteed to return a non-null array
339
Object JavaDoc[] listeners = listenerList.getListenerList();
340
341     // Process the listeners last to first, notifying
342
// those that are interested in this event
343
for (int i = listeners.length - 2; i >= 0; i -= 2) {
344         if (listeners[i] == IExceptionListener.class) {
345         ((IExceptionListener) listeners[i + 1]).exceptionOccured(e);
346         }
347     }
348     }
349
350     /**
351          * @see org.columba.core.base.SwingWorker#start()
352          */

353     @Override JavaDoc
354     public Thread JavaDoc start() {
355     TaskManager.getInstance().register(this);
356
357     return super.start();
358     }
359 }
Popular Tags