Coverage details for net.sourceforge.demetrix.util.SwingWorker

LineHitsSource
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  
450public 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  
870        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  
1010        value = x;
102  
1030    }
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  
1270    }
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  
1410        Thread t = threadVar.get();
142  
1430        if (t != null) {
144  
1450            t.interrupt();
146  
147         }
148  
1490        threadVar.clear();
150  
1510    }
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  
1730            Thread t = threadVar.get();
174  
1750            if (t == null) {
176  
1770                return getValue();
178  
179             }
180  
181             try {
182  
1830                t.join();
184  
185             }
186  
1870            catch (InterruptedException e) {
188  
1890                Thread.currentThread().interrupt(); // propagate
190  
1910                return null;
192  
1930            }
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  
2115    public SwingWorker() {
212  
2135        final Runnable doFinished = new Runnable() {
214  
215            public void run() { finished(); }
216  
217         };
218  
219  
220  
2215        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  
2475        Thread t = new Thread(doConstruct);
248  
2495        threadVar = new ThreadVar(t);
250  
2515    }
252  
253  
254  
255     /**
256  
257      * Start the worker thread.
258  
259      */
260  
261     public void start() {
262  
2630        Thread t = threadVar.get();
264  
2650        if (t != null) {
266  
2670            t.start();
268  
269         }
270  
2710    }
272  
273 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.