~ubuntu-branches/ubuntu/wily/389-ds-console/wily-proposed

« back to all changes in this revision

Viewing changes to src/com/netscape/admin/dirserv/SwingWorker.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-03-15 19:58:37 UTC
  • Revision ID: package-import@ubuntu.com-20120315195837-296zyft51thld8q7
Tags: upstream-1.2.6
ImportĀ upstreamĀ versionĀ 1.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 
3
 * Copyright (C) 2005 Red Hat, Inc.
 
4
 * All rights reserved.
 
5
 * 
 
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.
 
9
 * 
 
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.
 
14
 * 
 
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 **/
 
19
/**
 
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:
 
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:
 
28
 * You must now invoke start() on the SwingWorker after
 
29
 * creating it.
 
30
 */
 
31
 
 
32
package com.netscape.admin.dirserv;
 
33
 
 
34
import javax.swing.SwingUtilities;
 
35
 
 
36
public abstract class SwingWorker {
 
37
    private Object value;  // see getValue(), setValue()
 
38
    private Thread thread;
 
39
 
 
40
    /** 
 
41
     * Class to maintain reference to current worker thread
 
42
     * under separate synchronization control.
 
43
     */
 
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; }
 
49
    }
 
50
 
 
51
    private ThreadVar threadVar;
 
52
 
 
53
    /** 
 
54
     * Get the value produced by the worker thread, or null if it 
 
55
     * hasn't been constructed yet.
 
56
     */
 
57
    protected synchronized Object getValue() { 
 
58
        return value; 
 
59
    }
 
60
 
 
61
    /** 
 
62
     * Set the value produced by worker thread 
 
63
     */
 
64
    private synchronized void setValue(Object x) { 
 
65
        value = x; 
 
66
    }
 
67
 
 
68
    /** 
 
69
     * Compute the value to be returned by the <code>get</code> method. 
 
70
     */
 
71
    public abstract Object construct();
 
72
 
 
73
    /**
 
74
     * Called on the event dispatching thread (not on the worker thread)
 
75
     * after the <code>construct</code> method has returned.
 
76
     */
 
77
    public void finished() {
 
78
    }
 
79
 
 
80
    /**
 
81
     * A new method that interrupts the worker thread.  Call this method
 
82
     * to force the worker to stop what it's doing.
 
83
     */
 
84
    public void interrupt() {
 
85
        Thread t = threadVar.get();
 
86
        if (t != null) {
 
87
            t.interrupt();
 
88
        }
 
89
        threadVar.clear();
 
90
    }
 
91
 
 
92
    /**
 
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.
 
96
     * 
 
97
     * @return the value created by the <code>construct</code> method
 
98
     */
 
99
    public Object get() {
 
100
        while (true) {  
 
101
            Thread t = threadVar.get();
 
102
            if (t == null) {
 
103
                return getValue();
 
104
            }
 
105
            try {
 
106
                t.join();
 
107
            }
 
108
            catch (InterruptedException e) {
 
109
                Thread.currentThread().interrupt(); // propagate
 
110
                return null;
 
111
            }
 
112
        }
 
113
    }
 
114
 
 
115
 
 
116
    /**
 
117
     * Start a thread that will call the <code>construct</code> method
 
118
     * and then exit.
 
119
     */
 
120
    public SwingWorker() {
 
121
        final Runnable doFinished = new Runnable() {
 
122
           public void run() { finished(); }
 
123
        };
 
124
 
 
125
        Runnable doConstruct = new Runnable() { 
 
126
            public void run() {
 
127
                try {
 
128
                    setValue(construct());
 
129
                }
 
130
                finally {
 
131
                    threadVar.clear();
 
132
                }
 
133
 
 
134
                SwingUtilities.invokeLater(doFinished);
 
135
            }
 
136
        };
 
137
 
 
138
        Thread t = new Thread(doConstruct);
 
139
        threadVar = new ThreadVar(t);
 
140
    }
 
141
 
 
142
    /**
 
143
     * Start the worker thread.
 
144
     */
 
145
    public void start() {
 
146
        Thread t = threadVar.get();
 
147
        if (t != null) {
 
148
            t.start();
 
149
        }
 
150
    }
 
151
}