KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > swing > SwingWorker


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util.swing;
35
36 import javax.swing.SwingUtilities JavaDoc;
37
38 /**
39  * SwingWorker, adapted from Sun's Java Tutorial.
40  *
41  * This is the 3rd version of SwingWorker (also known as
42  * SwingWorker 3), an abstract class that you subclass to
43  * perform GUI-related work in a dedicated thread. For
44  * instructions on using this class, see:
45  *
46  * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
47  *
48  * Note that the API changed slightly in the 3rd version:
49  * You must now invoke start() on the SwingWorker after
50  * creating it.
51  *
52  * @version $Id: SwingWorker.java 4033 2006-11-16 20:16:51Z rcartwright $
53  */

54 public abstract class SwingWorker {
55   private volatile Object JavaDoc _value; // see getValue(), setValue()
56
// private Thread _thread;
57

58   /**
59    * Class to maintain reference to current worker thread
60    * under separate synchronization control.
61    */

62   private static class ThreadVar {
63     private volatile Thread JavaDoc _thread;
64     ThreadVar(Thread JavaDoc t) { _thread = t; }
65     Thread JavaDoc get() { return _thread; }
66     void clear() { _thread = null; }
67   }
68
69   private volatile ThreadVar _threadVar;
70
71   /** Gets the value produced by the worker thread, or null if it
72    * hasn't been constructed yet.
73    */

74   protected Object JavaDoc getValue() { return _value; }
75
76   /** Sets the value produced by worker thread. */
77   private void setValue(Object JavaDoc x) { _value = x; }
78
79   /** Compute the value to be returned by the <code>get</code> method. */
80   public abstract Object JavaDoc construct();
81
82   /** Called on the event dispatching thread (not on the worker thread)
83    * after the <code>construct</code> method has returned.
84    */

85   public void finished() { }
86
87   /** A new method that interrupts the worker thread. Call this method to force the worker to stop what it's doing. */
88   public void interrupt() {
89     Thread JavaDoc t = _threadVar.get();
90     if (t != null) t.interrupt();
91     _threadVar.clear();
92   }
93
94   /** Return the value created by the <code>construct</code> method. Returns null if either the constructing thread
95    * or the current thread was interrupted before a value was produced.
96    *
97    * @return the value created by the <code>construct</code> method
98    */

99   public Object JavaDoc get() {
100     while (true) {
101       Thread JavaDoc t = _threadVar.get();
102       if (t == null) return getValue();
103       try { t.join(); }
104       catch (InterruptedException JavaDoc e) {
105         Thread.currentThread().interrupt(); // propagate
106
return null;
107       }
108     }
109   }
110
111   /** Start a thread that will call the <code>construct</code> method and then exit. */
112   public SwingWorker() {
113     final Runnable JavaDoc doFinished = new Runnable JavaDoc() {
114       public void run() { finished(); }
115     };
116
117     Runnable JavaDoc doConstruct = new Runnable JavaDoc() {
118       public void run() {
119         try { setValue(construct()); }
120         catch (final RuntimeException JavaDoc e) {
121           // Throw the exception in the event dispatching thread.
122
SwingUtilities.invokeLater(new Runnable JavaDoc() { public void run() { throw e; } });
123           throw e;
124         }
125         catch (final Error JavaDoc e) {
126           // Throw the error in the event dispatching thread.
127
SwingUtilities.invokeLater(new Runnable JavaDoc() { public void run() { throw e; } });
128           throw e;
129         }
130         finally { _threadVar.clear(); }
131
132         SwingUtilities.invokeLater(doFinished);
133       }
134     };
135
136     Thread JavaDoc t = new Thread JavaDoc(doConstruct);
137     _threadVar = new ThreadVar(t);
138   }
139
140   /** Start the worker thread. */
141   public void start() {
142     Thread JavaDoc t = _threadVar.get();
143     if (t != null) t.start();
144   }
145 }
146
Popular Tags