1
/** BEGIN COPYRIGHT BLOCK
2
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
3
* Copyright (C) 2005 Red Hat, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* END COPYRIGHT BLOCK **/
20
* This is the 3rd version of SwingWorker (also known as
21
* SwingWorker 3), an abstract class that you subclass to
22
* perform GUI-related work in a dedicated thread. For
23
* instructions on using this class, see:
25
* http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
27
* Note that the API changed slightly in the 3rd version:
28
* You must now invoke start() on the SwingWorker after
32
package com.netscape.admin.dirserv;
34
import javax.swing.SwingUtilities;
36
public abstract class SwingWorker {
37
private Object value; // see getValue(), setValue()
38
private Thread thread;
41
* Class to maintain reference to current worker thread
42
* under separate synchronization control.
44
private static class ThreadVar {
45
private Thread thread;
46
ThreadVar(Thread t) { thread = t; }
47
synchronized Thread get() { return thread; }
48
synchronized void clear() { thread = null; }
51
private ThreadVar threadVar;
54
* Get the value produced by the worker thread, or null if it
55
* hasn't been constructed yet.
57
protected synchronized Object getValue() {
62
* Set the value produced by worker thread
64
private synchronized void setValue(Object x) {
69
* Compute the value to be returned by the <code>get</code> method.
71
public abstract Object construct();
74
* Called on the event dispatching thread (not on the worker thread)
75
* after the <code>construct</code> method has returned.
77
public void finished() {
81
* A new method that interrupts the worker thread. Call this method
82
* to force the worker to stop what it's doing.
84
public void interrupt() {
85
Thread t = threadVar.get();
93
* Return the value created by the <code>construct</code> method.
94
* Returns null if either the constructing thread or the current
95
* thread was interrupted before a value was produced.
97
* @return the value created by the <code>construct</code> method
101
Thread t = threadVar.get();
108
catch (InterruptedException e) {
109
Thread.currentThread().interrupt(); // propagate
117
* Start a thread that will call the <code>construct</code> method
120
public SwingWorker() {
121
final Runnable doFinished = new Runnable() {
122
public void run() { finished(); }
125
Runnable doConstruct = new Runnable() {
128
setValue(construct());
134
SwingUtilities.invokeLater(doFinished);
138
Thread t = new Thread(doConstruct);
139
threadVar = new ThreadVar(t);
143
* Start the worker thread.
145
public void start() {
146
Thread t = threadVar.get();