001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util.swing;
038:
039: import javax.swing.SwingUtilities;
040:
041: /** SwingWorker, adapted from Sun's Java Tutorial. This is the 3rd version of SwingWorker (also known as
042: * SwingWorker 3), an abstract class that you subclass to perform GUI-related work in a dedicated thread. For
043: * instructions on using this class, see: http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
044: *
045: * Note that the API changed slightly in the 3rd version: you must now invoke start() on the SwingWorker after
046: * creating it.
047: * @version $Id: SwingWorker.java 4255 2007-08-28 19:17:37Z mgricken $
048: */
049: public abstract class SwingWorker {
050: private volatile Object _value; // see getValue(), setValue()
051: // private Thread _thread;
052:
053: /** Class to maintain reference to current worker thread under separate synchronization control. */
054: private static class ThreadVar {
055: private volatile Thread _thread;
056:
057: ThreadVar(Thread t) {
058: _thread = t;
059: }
060:
061: Thread get() {
062: return _thread;
063: }
064:
065: void clear() {
066: _thread = null;
067: }
068: }
069:
070: private volatile ThreadVar _threadVar;
071:
072: /** Gets the value produced by the worker thread, or null if it hasn't been constructed yet. */
073: protected Object getValue() {
074: return _value;
075: }
076:
077: /** Sets the value produced by worker thread. */
078: private void setValue(Object x) {
079: _value = x;
080: }
081:
082: /** Compute the value to be returned by the <code>get</code> method. */
083: public abstract Object construct();
084:
085: /** Called from the event dispatching thread (not on the worker thread) after the <code>construct</code> method
086: * has returned.
087: */
088: public void finished() {
089: }
090:
091: /** A new method that interrupts the worker thread. Call this method to force the worker to stop what it's doing. */
092: public void interrupt() {
093: Thread t = _threadVar.get();
094: if (t != null)
095: t.interrupt();
096: _threadVar.clear();
097: }
098:
099: /** Return the value created by the <code>construct</code> method. Returns null if either the constructing thread
100: * or the current thread was interrupted before a value was produced.
101: * @return the value created by the <code>construct</code> method
102: */
103: public Object get() {
104: while (true) {
105: Thread t = _threadVar.get();
106: if (t == null)
107: return getValue();
108: try {
109: t.join();
110: } catch (InterruptedException e) {
111: Thread.currentThread().interrupt(); // propagate
112: return null;
113: }
114: }
115: }
116:
117: /** Start a thread that will call the <code>construct</code> method and then exit. */
118: public SwingWorker() {
119: final Runnable doFinished = new Runnable() {
120: public void run() {
121: finished();
122: }
123: };
124:
125: Runnable doConstruct = new Runnable() {
126: public void run() {
127: try {
128: setValue(construct());
129: } catch (final RuntimeException e) {
130: // Throw the exception in the event dispatching thread.
131: SwingUtilities.invokeLater(new Runnable() {
132: public void run() {
133: throw e;
134: }
135: });
136: throw e;
137: } catch (final Error e) {
138: // Throw the error in the event dispatching thread.
139: SwingUtilities.invokeLater(new Runnable() {
140: public void run() {
141: throw e;
142: }
143: });
144: throw e;
145: } finally {
146: _threadVar.clear();
147: }
148:
149: SwingUtilities.invokeLater(doFinished);
150: }
151: };
152:
153: Thread t = new Thread(doConstruct);
154: _threadVar = new ThreadVar(t);
155: }
156:
157: /** Start the worker thread. */
158: public void start() {
159: Thread t = _threadVar.get();
160: if (t != null)
161: t.start();
162: }
163: }
|