KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > base > SwingWorker


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.base;
17
18 import javax.swing.SwingUtilities JavaDoc;
19
20 /**
21  * This is the 3rd version of SwingWorker (also known as SwingWorker 3), an
22  * abstract class that you subclass to perform GUI-related work in a dedicated
23  * thread. For instructions on using this class, see:
24  *
25  * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
26  *
27  * Note that the API changed slightly in the 3rd version: You must now invoke
28  * start() on the SwingWorker after creating it.
29  */

30 public abstract class SwingWorker {
31     protected Object JavaDoc value; // see getValue(), setValue()
32

33     protected Thread JavaDoc thread;
34
35     protected boolean cancel;
36
37     protected ThreadVar threadVar;
38
39     /**
40      * Start a thread that will call the <code>construct</code> method and
41      * then exit.
42      */

43     public SwingWorker() {
44         cancel = false;
45
46         final Runnable JavaDoc doFinished = new Runnable JavaDoc() {
47             public void run() {
48                 finished();
49             }
50         };
51
52         Runnable JavaDoc doConstruct = new Runnable JavaDoc() {
53             public void run() {
54                 try {
55                     setValue(construct());
56                 } finally {
57                     // threadVar;
58
}
59
60                 SwingUtilities.invokeLater(doFinished);
61             }
62         };
63
64         Thread JavaDoc t = new Thread JavaDoc(doConstruct);
65
66         // following java guidelines I'm setting this to low priority
67
// -> this should make the gui more responsive, because the
68
// -> background worker has low priority whereas the gui worker
69
// -> has normal priority
70
t.setPriority(Thread.MIN_PRIORITY);
71
72         threadVar = new ThreadVar(t);
73     }
74
75     /**
76      * Get the value produced by the worker thread, or null if it hasn't been
77      * constructed yet.
78      */

79     protected synchronized Object JavaDoc getValue() {
80         return value;
81     }
82
83     /**
84      * Set the value produced by worker thread
85      */

86     synchronized void setValue(Object JavaDoc x) {
87         value = x;
88     }
89
90     public Thread JavaDoc getThread() {
91         return threadVar.get();
92     }
93
94     public ThreadVar getThreadVar() {
95         return threadVar;
96     }
97
98     public boolean getCancel() {
99         return cancel;
100     }
101
102     public void setCancel(boolean b) {
103         cancel = b;
104     }
105
106     protected boolean isCanceled() {
107         return cancel;
108     }
109
110     /**
111      * Compute the value to be returned by the <code>get</code> method.
112      */

113     public abstract Object JavaDoc construct();
114
115     /**
116      * Called on the event dispatching thread (not on the worker thread) after
117      * the <code>construct</code> method has returned.
118      */

119     public void finished() {
120         // nothing to do
121
}
122
123     /**
124      * A new method that interrupts the worker thread. Call this method to force
125      * the worker to stop what it's doing.
126      */

127     public void interrupt() {
128         Thread JavaDoc t = threadVar.get();
129
130         if (t != null) {
131             t.interrupt();
132         }
133
134         threadVar.clear();
135     }
136
137     /**
138      * Return the value created by the <code>construct</code> method. Returns
139      * null if either the constructing thread or the current thread was
140      * interrupted before a value was produced.
141      *
142      * @return the value created by the <code>construct</code> method
143      */

144     public Object JavaDoc get() {
145         while (true) {
146             Thread JavaDoc t = threadVar.get();
147
148             if (t == null) {
149                 return getValue();
150             }
151
152             try {
153                 t.join();
154             } catch (InterruptedException JavaDoc e) {
155                 Thread.currentThread().interrupt(); // propagate
156

157                 return null;
158             }
159         }
160     }
161
162     /**
163      * Start the worker thread.
164      */

165     public Thread JavaDoc start() {
166
167         thread = threadVar.get();
168
169         if (thread != null) {
170             thread.start();
171
172             return thread;
173         }
174
175         return null;
176     }
177
178     /**
179      * Class to maintain reference to current worker thread under separate
180      * synchronization control.
181      */

182     public static class ThreadVar {
183         private Thread JavaDoc thread;
184
185         ThreadVar(Thread JavaDoc t) {
186             thread = t;
187         }
188
189         synchronized Thread JavaDoc get() {
190             return new Thread JavaDoc(thread);
191         }
192
193         synchronized void clear() {
194             thread = null;
195         }
196     }
197 }
198
Popular Tags