KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > SwingWorker


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps;
15
16 import javax.swing.SwingUtilities JavaDoc;
17
18 /**
19  * SwingWorker (based on SwingWorker 3).
20  * To use the SwingWorker class, you create a subclass of it.
21  * In the subclass, you must implement the construct() method contains
22  * the code to perform your lengthy operation.
23  * You invoke start() on your SwingWorker object to start the thread,
24  * which then calls your construct() method.
25  * When you need the object returned by the construct() method,
26  * you call the SwingWorker's get() method.
27  * <pre>
28  * SwingWorker worker = new SwingWorker()
29  * {
30  * public Object construct()
31  * {
32  * return new expensiveOperation();
33  * }
34  * };
35  * worker.start();
36  * // do something
37  * // when you need the result:
38  * x = worker.get(); // this blocks the UI !!
39  * </pre>
40  */

41 public abstract class SwingWorker
42 {
43     /**
44      * Start a thread that will call the <code>construct</code> method
45      * and then exit.
46      */

47     public SwingWorker()
48     {
49         final Runnable JavaDoc doFinished = new Runnable JavaDoc()
50         {
51             public void run()
52             {
53                 finished();
54             }
55         };
56
57         Runnable JavaDoc doConstruct = new Runnable JavaDoc()
58         {
59             public void run()
60             {
61                 try
62                 {
63                     setValue(construct());
64                 }
65                 finally
66                 {
67                     m_threadVar.clear();
68                 }
69                 SwingUtilities.invokeLater(doFinished);
70             }
71         };
72
73         Thread JavaDoc t = new Thread JavaDoc (doConstruct);
74         m_threadVar = new ThreadVar(t);
75     } // SwingWorker
76

77     private ThreadVar m_threadVar;
78     private Object JavaDoc m_value; // see getValue(), setValue()
79

80     /**
81      * Compute the value to be returned by the <code>get</code> method.
82      * @return value
83      */

84     public abstract Object JavaDoc construct();
85
86     /**
87      * Called on the event dispatching thread (not on the worker thread)
88      * after the <code>construct</code> method has returned.
89      */

90     public void finished()
91     {
92     }
93
94     /**
95      * Get the value produced by the worker thread,
96      * or null if it hasn't been constructed yet.
97      * @return value of worker
98      */

99     protected synchronized Object JavaDoc getValue()
100     {
101         return m_value;
102     } // getValue
103

104     /**
105      * Set the value produced by worker thread
106      * @param x worker value
107      */

108     private synchronized void setValue(Object JavaDoc x)
109     {
110         m_value = x;
111     } // setValue
112

113     /************************************************************************/
114
115     /**
116      * Start the worker thread.
117      */

118     public void start()
119     {
120         Thread JavaDoc t = m_threadVar.get();
121         if (t != null)
122             t.start();
123     } // start
124

125     /**
126      * Return the value created by the <code>construct</code> method.
127      * Returns null if either the constructing thread or the current
128      * thread was interrupted before a value was produced.
129      *
130      * @return the value created by the <code>construct</code> method
131      */

132     public Object JavaDoc get()
133     {
134         while (true)
135         {
136             Thread JavaDoc t = m_threadVar.get();
137             if (t == null)
138             {
139                 return getValue();
140             }
141             try
142             {
143                 t.join();
144             }
145             catch (InterruptedException JavaDoc e)
146             {
147                 Thread.currentThread().interrupt(); // propagate
148
return null;
149             }
150         }
151     } // get
152

153     /**
154      * A new method that interrupts the worker thread. Call this method
155      * to force the worker to stop what it's doing.
156      */

157     public void interrupt()
158     {
159         Thread JavaDoc t = m_threadVar.get();
160         if (t != null)
161         {
162             t.interrupt();
163         }
164         m_threadVar.clear();
165     } // interrupt
166

167     /**
168      * Is worker Alive
169      * @return true if alive
170      */

171     public boolean isAlive()
172     {
173         Thread JavaDoc t = m_threadVar.get();
174         if (t == null)
175             return false;
176         return t.isAlive();
177     } // isAlive
178

179     /*************************************************************************/
180
181     /**
182      * Class to maintain reference to current worker thread
183      * under separate synchronization control.
184      */

185     private static class ThreadVar
186     {
187         ThreadVar(Thread JavaDoc t)
188         {
189             thread = t;
190         }
191
192         private Thread JavaDoc thread;
193
194         synchronized Thread JavaDoc get()
195         {
196             return thread;
197         }
198         synchronized void clear()
199         {
200             thread = null;
201         }
202     } // ThreadVar
203

204 } // SwingWorker
205
Popular Tags