KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > launcher > SwingWorker


1 /**
2  * $RCSfile: SwingWorker.java,v $
3  * $Revision: 1.3 $
4  * $Date: 2005/03/07 07:31:50 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.launcher;
13
14 import javax.swing.SwingUtilities JavaDoc;
15
16 /**
17  * Modified SwingWorker that actually works.
18  * @author Derek DeMoro
19  */

20 public abstract class SwingWorker {
21     private Object JavaDoc value; // see getValue(), setValue()
22
private Thread JavaDoc thread;
23
24     /**
25      * Class to maintain reference to current worker thread
26      * under separate synchronization control.
27      */

28     private static class ThreadVar {
29         private Thread JavaDoc thread;
30
31         ThreadVar(Thread JavaDoc t) {
32             thread = t;
33         }
34
35         synchronized Thread JavaDoc get() {
36             return thread;
37         }
38
39         synchronized void clear() {
40             thread = null;
41         }
42     }
43
44     private ThreadVar threadVar;
45
46     /**
47      * Get the value produced by the worker thread, or null if it
48      * hasn't been constructed yet.
49      */

50     protected synchronized Object JavaDoc getValue() {
51         return value;
52     }
53
54     /**
55      * Set the value produced by worker thread
56      */

57     private synchronized void setValue(Object JavaDoc x) {
58         value = x;
59     }
60
61     /**
62      * Compute the value to be returned by the <code>get</code> method.
63      */

64     public abstract Object JavaDoc construct();
65
66     /**
67      * Called on the event dispatching thread (not on the worker thread)
68      * after the <code>construct</code> method has returned.
69      */

70     public void finished() {
71
72     }
73
74     /**
75      * A new method that interrupts the worker thread. Call this method
76      * to force the worker to stop what it's doing.
77      */

78     public void interrupt() {
79         Thread JavaDoc t = threadVar.get();
80         if (t != null) {
81             t.interrupt();
82         }
83         threadVar.clear();
84     }
85
86
87     /**
88      * Return the value created by the <code>construct</code> method.
89      * Returns null if either the constructing thread or the current
90      * thread was interrupted before a value was produced.
91      *
92      * @return the value created by the <code>construct</code> method
93      */

94     public Object JavaDoc get() {
95         while (true) {
96             Thread JavaDoc t = threadVar.get();
97             if (t == null) {
98                 return getValue();
99             }
100             try {
101                 t.join();
102             }
103             catch (InterruptedException JavaDoc e) {
104                 Thread.currentThread().interrupt(); // propagate
105
return null;
106             }
107         }
108     }
109
110
111     /**
112      * Start a thread that will call the <code>construct</code> method
113      * and then exit.
114      */

115     public SwingWorker() {
116         final Runnable JavaDoc doFinished = new Runnable JavaDoc() {
117             public void run() {
118                 finished();
119             }
120         };
121
122         Runnable JavaDoc doConstruct = new Runnable JavaDoc() {
123             public void run() {
124                 try {
125                     setValue(construct());
126                 }
127                 finally {
128                     threadVar.clear();
129                 }
130                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
131                     public void run() {
132                         finished();
133                     }
134                 });
135
136             }
137         };
138
139         Thread JavaDoc t = new Thread JavaDoc(doConstruct);
140         threadVar = new ThreadVar(t);
141     }
142
143     /**
144      * Start the worker thread.
145      */

146     public void start() {
147         Thread JavaDoc t = threadVar.get();
148         if (t != null) {
149             t.start();
150         }
151     }
152     
153
154 }
155
156
Popular Tags