Line | Hits | Source |
---|---|---|
1 | /** | |
2 | ||
3 | * This file was created by Sun Microsystems, Inc. | |
4 | ||
5 | */ | |
6 | ||
7 | /** | |
8 | ||
9 | * This is the 3rd version of SwingWorker (also known as | |
10 | ||
11 | * SwingWorker 3), an abstract class that you subclass to | |
12 | ||
13 | * perform GUI-related work in a dedicated thread. For | |
14 | ||
15 | * instructions on and examples of using this class, see: | |
16 | ||
17 | * | |
18 | ||
19 | * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html | |
20 | ||
21 | * | |
22 | ||
23 | * Note that the API changed slightly in the 3rd version: | |
24 | ||
25 | * You must now invoke start() on the SwingWorker after | |
26 | ||
27 | * creating it. | |
28 | ||
29 | */ | |
30 | ||
31 | ||
32 | ||
33 | ||
34 | ||
35 | package net.sourceforge.demetrix.util; | |
36 | ||
37 | ||
38 | ||
39 | ||
40 | ||
41 | import javax.swing.SwingUtilities; | |
42 | ||
43 | ||
44 | ||
45 | 0 | public abstract class SwingWorker { |
46 | ||
47 | private Object value; // see getValue(), setValue() | |
48 | ||
49 | ||
50 | ||
51 | /** | |
52 | ||
53 | * Class to maintain reference to current worker thread | |
54 | ||
55 | * under separate synchronization control. | |
56 | ||
57 | */ | |
58 | ||
59 | private static class ThreadVar { | |
60 | ||
61 | private Thread thread; | |
62 | ||
63 | ThreadVar(Thread t) { thread = t; } | |
64 | ||
65 | synchronized Thread get() { return thread; } | |
66 | ||
67 | synchronized void clear() { thread = null; } | |
68 | ||
69 | } | |
70 | ||
71 | ||
72 | ||
73 | private ThreadVar threadVar; | |
74 | ||
75 | ||
76 | ||
77 | /** | |
78 | ||
79 | * Get the value produced by the worker thread, or null if it | |
80 | ||
81 | * hasn't been constructed yet. | |
82 | ||
83 | */ | |
84 | ||
85 | protected synchronized Object getValue() { | |
86 | ||
87 | 0 | return value; |
88 | ||
89 | } | |
90 | ||
91 | ||
92 | ||
93 | /** | |
94 | ||
95 | * Set the value produced by worker thread | |
96 | ||
97 | */ | |
98 | ||
99 | private synchronized void setValue(Object x) { | |
100 | ||
101 | 0 | value = x; |
102 | ||
103 | 0 | } |
104 | ||
105 | ||
106 | ||
107 | /** | |
108 | ||
109 | * Compute the value to be returned by the <code>get</code> method. | |
110 | ||
111 | */ | |
112 | ||
113 | public abstract Object construct(); | |
114 | ||
115 | ||
116 | ||
117 | /** | |
118 | ||
119 | * Called on the event dispatching thread (not on the worker thread) | |
120 | ||
121 | * after the <code>construct</code> method has returned. | |
122 | ||
123 | */ | |
124 | ||
125 | public void finished() { | |
126 | ||
127 | 0 | } |
128 | ||
129 | ||
130 | ||
131 | /** | |
132 | ||
133 | * A new method that interrupts the worker thread. Call this method | |
134 | ||
135 | * to force the worker to stop what it's doing. | |
136 | ||
137 | */ | |
138 | ||
139 | public void interrupt() { | |
140 | ||
141 | 0 | Thread t = threadVar.get(); |
142 | ||
143 | 0 | if (t != null) { |
144 | ||
145 | 0 | t.interrupt(); |
146 | ||
147 | } | |
148 | ||
149 | 0 | threadVar.clear(); |
150 | ||
151 | 0 | } |
152 | ||
153 | ||
154 | ||
155 | /** | |
156 | ||
157 | * Return the value created by the <code>construct</code> method. | |
158 | ||
159 | * Returns null if either the constructing thread or the current | |
160 | ||
161 | * thread was interrupted before a value was produced. | |
162 | ||
163 | * | |
164 | ||
165 | * @return the value created by the <code>construct</code> method | |
166 | ||
167 | */ | |
168 | ||
169 | public Object get() { | |
170 | ||
171 | while (true) { | |
172 | ||
173 | 0 | Thread t = threadVar.get(); |
174 | ||
175 | 0 | if (t == null) { |
176 | ||
177 | 0 | return getValue(); |
178 | ||
179 | } | |
180 | ||
181 | try { | |
182 | ||
183 | 0 | t.join(); |
184 | ||
185 | } | |
186 | ||
187 | 0 | catch (InterruptedException e) { |
188 | ||
189 | 0 | Thread.currentThread().interrupt(); // propagate |
190 | ||
191 | 0 | return null; |
192 | ||
193 | 0 | } |
194 | ||
195 | } | |
196 | ||
197 | } | |
198 | ||
199 | ||
200 | ||
201 | ||
202 | ||
203 | /** | |
204 | ||
205 | * Start a thread that will call the <code>construct</code> method | |
206 | ||
207 | * and then exit. | |
208 | ||
209 | */ | |
210 | ||
211 | 5 | public SwingWorker() { |
212 | ||
213 | 5 | final Runnable doFinished = new Runnable() { |
214 | ||
215 | public void run() { finished(); } | |
216 | ||
217 | }; | |
218 | ||
219 | ||
220 | ||
221 | 5 | Runnable doConstruct = new Runnable() { |
222 | ||
223 | public void run() { | |
224 | ||
225 | try { | |
226 | ||
227 | setValue(construct()); | |
228 | ||
229 | } | |
230 | ||
231 | finally { | |
232 | ||
233 | threadVar.clear(); | |
234 | ||
235 | } | |
236 | ||
237 | ||
238 | ||
239 | SwingUtilities.invokeLater(doFinished); | |
240 | ||
241 | } | |
242 | ||
243 | }; | |
244 | ||
245 | ||
246 | ||
247 | 5 | Thread t = new Thread(doConstruct); |
248 | ||
249 | 5 | threadVar = new ThreadVar(t); |
250 | ||
251 | 5 | } |
252 | ||
253 | ||
254 | ||
255 | /** | |
256 | ||
257 | * Start the worker thread. | |
258 | ||
259 | */ | |
260 | ||
261 | public void start() { | |
262 | ||
263 | 0 | Thread t = threadVar.get(); |
264 | ||
265 | 0 | if (t != null) { |
266 | ||
267 | 0 | t.start(); |
268 | ||
269 | } | |
270 | ||
271 | 0 | } |
272 | ||
273 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |