source: git/Singular/LIB/surfex/SwingWorker.java @ 2ab830

spielwiese
Last change on this file since 2ab830 was 3de2ca, checked in by Hans Schönemann <hannes@…>, 16 years ago
*hannes: surfex_0_90_00 git-svn-id: file:///usr/local/Singular/svn/trunk@11070 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.3 KB
Line 
1////////////////////////////////////////////////////////////////////////
2//
3// This file SwingWorker.java is part of SURFEX.
4//
5////////////////////////////////////////////////////////////////////////
6
7////////////////////////////////////////////////////////////////////////
8//
9// SURFEX version 0.90.00
10// =================
11//
12// Saarland University at Saarbruecken, Germany
13// Department of Mathematics and Computer Science
14//
15// SURFEX on the web: www.surfex.AlgebraicSurface.net
16//
17// Authors: Oliver Labs (2001-2008), Stephan Holzer (2004-2005)
18//
19// Copyright (C) 2001-2008
20//
21//
22// *NOTICE*
23// ========
24// 
25// This program is free software; you can redistribute it and/or modify it
26// under the terms of the GNU General Public License as published by the
27// Free Software Foundation ( version 3 or later of the License ).
28//
29// See LICENCE.TXT for details.
30//
31/////////////////////////////////////////////////////////////////////////
32
33import javax.swing.SwingUtilities;
34
35/**
36 * This file is an adaption of the following:
37 *
38 * This is the 3rd version of SwingWorker (also known as
39 * SwingWorker 3), an abstract class that you subclass to
40 * perform GUI-related work in a dedicated thread.  For
41 * instructions on and examples of using this class, see:
42 *
43 * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
44 *
45 * Note that the API changed slightly in the 3rd version:
46 * You must now invoke start() on the SwingWorker after
47 * creating it.
48 */
49public abstract class SwingWorker {
50    private Object value;  // see getValue(), setValue()
51
52    /**
53     * Class to maintain reference to current worker thread
54     * under separate synchronization control.
55     */
56    private static class ThreadVar {
57        private Thread thread;
58        ThreadVar(Thread t) { thread = t; }
59        synchronized Thread get() { return thread; }
60        synchronized void clear() { thread = null; }
61    }
62
63    private ThreadVar threadVar;
64
65    /**
66     * Get the value produced by the worker thread, or null if it
67     * hasn't been constructed yet.
68     */
69    protected synchronized Object getValue() { 
70        return value; 
71    }
72
73    /**
74     * Set the value produced by worker thread
75     */
76    private synchronized void setValue(Object x) { 
77        value = x; 
78    }
79
80    /**
81     * Compute the value to be returned by the <code>get</code> method.
82     */
83    public abstract Object construct();
84
85    /**
86     * Called on the event dispatching thread (not on the worker thread)
87     * after the <code>construct</code> method has returned.
88     */
89    public void finished() {
90    }
91
92    /**
93     * A new method that interrupts the worker thread.  Call this method
94     * to force the worker to stop what it's doing.
95     */
96    public void interrupt() {
97        Thread t = threadVar.get();
98        if (t != null) {
99            t.interrupt();
100        }
101        threadVar.clear();
102    }
103
104    /**
105     * Return the value created by the <code>construct</code> method. 
106     * Returns null if either the constructing thread or the current
107     * thread was interrupted before a value was produced.
108     *
109     * @return the value created by the <code>construct</code> method
110     */
111    public Object get() {
112        while (true) { 
113            Thread t = threadVar.get();
114            if (t == null) {
115                return getValue();
116            }
117            try {
118                t.join();
119            }
120            catch (InterruptedException e) {
121                Thread.currentThread().interrupt(); // propagate
122                return null;
123            }
124        }
125    }
126
127
128    /**
129     * Start a thread that will call the <code>construct</code> method
130     * and then exit.
131     */
132    public SwingWorker() {
133        final Runnable doFinished = new Runnable() {
134           public void run() { finished(); }
135        };
136
137        Runnable doConstruct = new Runnable() { 
138            public void run() {
139                try {
140                    setValue(construct());
141                }
142                finally {
143                    threadVar.clear();
144                }
145
146                SwingUtilities.invokeLater(doFinished);
147            }
148        };
149
150        Thread t = new Thread(doConstruct);
151        threadVar = new ThreadVar(t);
152    }
153
154    /**
155     * Start the worker thread.
156     */
157    public void start() {
158        Thread t = threadVar.get();
159        if (t != null) {
160            t.start();
161        }
162    }
163}
164
Note: See TracBrowser for help on using the repository browser.