Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / swing / threads / SwingWorker.java @ 40561

History | View | Annotate | Download (4.35 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils.swing.threads;
25

    
26
import javax.swing.SwingUtilities;
27

    
28
/**
29
 * This is the 3rd version of SwingWorker (also known as
30
 * SwingWorker 3), an abstract class that you subclass to
31
 * perform GUI-related work in a dedicated thread.  For
32
 * instructions on and examples of using this class, see:
33
 * 
34
 * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
35
 *
36
 * Note that the API changed slightly in the 3rd version:
37
 * You must now invoke start() on the SwingWorker after
38
 * creating it.
39
 */
40
public abstract class SwingWorker {
41
    private Object value;  // see getValue(), setValue()
42

    
43
    /** 
44
     * Class to maintain reference to current worker thread
45
     * under separate synchronization control.
46
     */
47
    private static class ThreadVar {
48
        private Thread thread;
49
        ThreadVar(Thread t) { thread = t; }
50
        synchronized Thread get() { return thread; }
51
        synchronized void clear() { thread = null; }
52
    }
53

    
54
    private ThreadVar threadVar;
55

    
56
    /** 
57
     * Get the value produced by the worker thread, or null if it 
58
     * hasn't been constructed yet.
59
     */
60
    protected synchronized Object getValue() { 
61
        return value; 
62
    }
63

    
64
    /** 
65
     * Set the value produced by worker thread 
66
     */
67
    private synchronized void setValue(Object x) { 
68
        value = x; 
69
    }
70

    
71
    /** 
72
     * Compute the value to be returned by the <code>get</code> method. 
73
     */
74
    public abstract Object construct();
75

    
76
    /**
77
     * Called on the event dispatching thread (not on the worker thread)
78
     * after the <code>construct</code> method has returned.
79
     */
80
    public void finished() {
81
    }
82

    
83
    /**
84
     * A new method that interrupts the worker thread.  Call this method
85
     * to force the worker to stop what it's doing.
86
     */
87
    public void interrupt() {
88
        Thread t = threadVar.get();
89
        if (t != null) {
90
            t.interrupt();
91
        }
92
        threadVar.clear();
93
    }
94

    
95
    /**
96
     * Return the value created by the <code>construct</code> method.  
97
     * Returns null if either the constructing thread or the current
98
     * thread was interrupted before a value was produced.
99
     * 
100
     * @return the value created by the <code>construct</code> method
101
     */
102
    public Object get() {
103
        while (true) {  
104
            Thread t = threadVar.get();
105
            if (t == null) {
106
                return getValue();
107
            }
108
            try {
109
                t.join();
110
            }
111
            catch (InterruptedException e) {
112
                Thread.currentThread().interrupt(); // propagate
113
                return null;
114
            }
115
        }
116
    }
117

    
118

    
119
    /**
120
     * Start a thread that will call the <code>construct</code> method
121
     * and then exit.
122
     */
123
    public SwingWorker() {
124
        final Runnable doFinished = new Runnable() {
125
           public void run() { finished(); }
126
        };
127

    
128
        Runnable doConstruct = new Runnable() { 
129
            public void run() {
130
                try {
131
                    setValue(construct());
132
                }
133
                finally {
134
                    threadVar.clear();
135
                }
136

    
137
                SwingUtilities.invokeLater(doFinished);
138
            }
139
        };
140

    
141
        Thread t = new Thread(doConstruct);
142
        threadVar = new ThreadVar(t);
143
    }
144

    
145
    /**
146
     * Start the worker thread.
147
     */
148
    public void start() {
149
        Thread t = threadVar.get();
150
        if (t != null) {
151
            t.start();
152
        }
153
    }
154
}
155