View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2024.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.ui;
39  
40  import javax.swing.SwingUtilities;
41  
42  /**
43   * This is the 3rd version of SwingWorker (also known as SwingWorker 3), an abstract class that you
44   * subclass to perform GUI-related work in a dedicated thread. For instructions on using this class,
45   * see: http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html Note that the API changed
46   * slightly in the 3rd version: You must now invoke start() on the SwingWorker after creating it.
47   *
48   * @author Michael J. Schnieders
49   */
50  public abstract class SwingWorker {
51  
52    private Object value; // see getValue(), setValue()
53    private ThreadVar threadVar;
54    /** Start a thread that will call the <code>construct</code> method and then exit. */
55    public SwingWorker() {
56      final Runnable doFinished =
57          new Runnable() {
58            public void run() {
59              finished();
60            }
61          };
62      Runnable doConstruct =
63          new Runnable() {
64            public void run() {
65              try {
66                setValue(construct());
67              } finally {
68                threadVar.clear();
69              }
70              SwingUtilities.invokeLater(doFinished);
71            }
72          };
73      Thread t = new Thread(doConstruct);
74      threadVar = new ThreadVar(t);
75    }
76  
77    /**
78     * Compute the value to be returned by the <code>get</code> method.
79     *
80     * @return a {@link java.lang.Object} object.
81     */
82    public abstract Object construct();
83  
84    /**
85     * Called on the event dispatching thread (not on the worker thread) after the <code>construct
86     * </code> method has returned.
87     */
88    public void finished() {}
89  
90    /**
91     * Return the value created by the <code>construct</code> method. Returns null if either the
92     * constructing thread or the current thread was interrupted before a value was produced.
93     *
94     * @return the value created by the <code>construct</code> method
95     */
96    public Object get() {
97      while (true) {
98        Thread t = threadVar.get();
99        if (t == null) {
100         return getValue();
101       }
102       try {
103         t.join();
104       } catch (InterruptedException e) {
105         Thread.currentThread().interrupt(); // propagate
106         return null;
107       }
108     }
109   }
110 
111   /**
112    * A new method that interrupts the worker thread. Call this method to force the worker to stop
113    * what it's doing.
114    */
115   public void interrupt() {
116     Thread t = threadVar.get();
117     if (t != null) {
118       t.interrupt();
119     }
120     threadVar.clear();
121   }
122 
123   /** Start the worker thread. */
124   public void start() {
125     Thread t = threadVar.get();
126     if (t != null) {
127       t.start();
128     }
129   }
130 
131   /**
132    * Get the value produced by the worker thread, or null if it hasn't been constructed yet.
133    *
134    * @return a {@link java.lang.Object} object.
135    */
136   protected synchronized Object getValue() {
137     return value;
138   }
139 
140   /** Set the value produced by worker thread */
141   private synchronized void setValue(Object x) {
142     value = x;
143   }
144 
145   /**
146    * Class to maintain reference to current worker thread under separate synchronization control.
147    */
148   private static class ThreadVar {
149 
150     private Thread thread;
151 
152     ThreadVar(Thread t) {
153       thread = t;
154     }
155 
156     synchronized void clear() {
157       thread = null;
158     }
159 
160     synchronized Thread get() {
161       return thread;
162     }
163   }
164 }