KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > ui > DjSwingWorker


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.ui;
31
32 import javax.swing.SwingUtilities JavaDoc;
33
34 /**
35  * An abstract class that you subclass to perform
36  * GUI-related work in a dedicated thread.
37  * For instructions on using this class, see
38  * http://java.sun.com/products/jfc/swingdoc-current/threads2.html
39  */

40 public abstract class DjSwingWorker
41 {
42   private Object JavaDoc value; // see getValue(), setValue()
43

44   /**
45    * Class to maintain reference to current worker thread
46    * under separate synchronization control.
47    */

48   private static class ThreadVar
49   {
50     private Thread JavaDoc thread;
51
52     ThreadVar(Thread JavaDoc t)
53     {
54       thread = t;
55     }
56
57     synchronized Thread JavaDoc get()
58     {
59       return thread;
60     }
61
62     synchronized void clear()
63     {
64       thread = null;
65     }
66   }
67
68   private ThreadVar threadVar;
69
70   /**
71    * Get the value produced by the worker thread, or null if it
72    * hasn't been constructed yet.
73    */

74   protected synchronized Object JavaDoc getValue()
75   {
76     return value;
77   }
78
79   /**
80    * Set the value produced by worker thread
81    */

82   private synchronized void setValue(Object JavaDoc x)
83   {
84     value = x;
85   }
86
87   /**
88    * Compute the value to be returned by the <code>get</code> method.
89    */

90   public abstract Object JavaDoc construct();
91
92   /**
93    * Called on the event dispatching thread (not on the worker thread)
94    * after the <code>construct</code> method has returned.
95    */

96   public void finished()
97   {
98   }
99
100   /**
101    * A new method that interrupts the worker thread. Call this method
102    * to force the worker to stop what it's doing.
103    */

104   public void interrupt()
105   {
106     Thread JavaDoc t = threadVar.get();
107     if (t != null)
108     {
109       t.interrupt();
110     }
111     threadVar.clear();
112   }
113
114   /**
115    * Return the value created by the <code>construct</code> method.
116    * Returns null if either the constructing thread or the current
117    * thread was interrupted before a value was produced.
118    *
119    * @return the value created by the <code>construct</code> method
120    */

121   public Object JavaDoc get()
122   {
123     while (true)
124     {
125       Thread JavaDoc t = threadVar.get();
126       if (t == null)
127       {
128         return getValue();
129       }
130       try
131       {
132         t.join();
133       }
134       catch (InterruptedException JavaDoc e)
135       {
136         Thread.currentThread().interrupt(); // propagate
137
return null;
138       }
139     }
140   }
141
142   /**
143    * Start a thread that will call the <code>construct</code> method
144    * and then exit.
145    */

146   public DjSwingWorker()
147   {
148     final Runnable JavaDoc doFinished = new Runnable JavaDoc()
149     {
150       public void run()
151       {
152         finished();
153       }
154     };
155
156     Runnable JavaDoc doConstruct = new Runnable JavaDoc()
157     {
158       public void run()
159       {
160         try
161         {
162           setValue(construct());
163         }
164         finally
165         {
166           threadVar.clear();
167         }
168
169         SwingUtilities.invokeLater(doFinished);
170       }
171     };
172
173     Thread JavaDoc t = new Thread JavaDoc(doConstruct);
174     threadVar = new ThreadVar(t);
175   }
176
177   /**
178    * Start the worker thread.
179    */

180   public void start()
181   {
182     Thread JavaDoc t = threadVar.get();
183     if (t != null)
184     {
185       t.start();
186     }
187   }
188 }
Popular Tags