| 1 | /* |
| 2 | * SwingWorker.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.app.gui.util; |
| 27 | |
| 28 | import javax.swing.*; |
| 29 | |
| 30 | /** |
| 31 | * This is the 3rd version of SwingWorker (also known as |
| 32 | * SwingWorker 3), an abstract class that you subclass to |
| 33 | * perform GUI-related work in a dedicated thread. For |
| 34 | * instructions on using this class, see: |
| 35 | * <p/> |
| 36 | * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html |
| 37 | * <p/> |
| 38 | * Note that the API changed slightly in the 3rd version: |
| 39 | * You must now invoke start() on the SwingWorker after |
| 40 | * creating it. |
| 41 | */ |
| 42 | public abstract class SwingWorker { |
| 43 | private Object value; // see getValue(), setValue() |
| 44 | private Thread thread; |
| 45 | |
| 46 | /** |
| 47 | * Class to maintain reference to current worker thread |
| 48 | * under separate synchronization control. |
| 49 | */ |
| 50 | private static class ThreadVar { |
| 51 | private Thread thread; |
| 52 | |
| 53 | ThreadVar(Thread t) { |
| 54 | thread = t; |
| 55 | } |
| 56 | |
| 57 | synchronized Thread get() { |
| 58 | return thread; |
| 59 | } |
| 60 | |
| 61 | synchronized void clear() { |
| 62 | thread = null; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private ThreadVar threadVar; |
| 67 | |
| 68 | /** |
| 69 | * Get the value produced by the worker thread, or null if it |
| 70 | * hasn't been constructed yet. |
| 71 | */ |
| 72 | protected synchronized Object getValue() { |
| 73 | return value; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Set the value produced by worker thread |
| 78 | */ |
| 79 | private synchronized void setValue(Object x) { |
| 80 | value = x; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Allows the priority of the worker thread to be set. |
| 85 | */ |
| 86 | public void setPriority(int priority) { |
| 87 | threadVar.get().setPriority(priority); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Compute the value to be returned by the <code>get</code> method. |
| 92 | */ |
| 93 | public abstract Object construct(); |
| 94 | |
| 95 | /** |
| 96 | * Called on the event dispatching thread (not on the worker thread) |
| 97 | * after the <code>construct</code> method has returned. |
| 98 | */ |
| 99 | public void finished() { |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * A new method that interrupts the worker thread. Call this method |
| 104 | * to force the worker to stop what it's doing. |
| 105 | */ |
| 106 | public void interrupt() { |
| 107 | Thread t = threadVar.get(); |
| 108 | if (t != null) { |
| 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 get() { |
| 122 | while (true) { |
| 123 | Thread t = threadVar.get(); |
| 124 | if (t == null) { |
| 125 | return getValue(); |
| 126 | } |
| 127 | try { |
| 128 | t.join(); |
| 129 | } catch (InterruptedException e) { |
| 130 | Thread.currentThread().interrupt(); // propagate |
| 131 | return null; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * Start a thread that will call the <code>construct</code> method |
| 139 | * and then exit. |
| 140 | */ |
| 141 | public SwingWorker() { |
| 142 | final Runnable doFinished = new Runnable() { |
| 143 | public void run() { |
| 144 | finished(); |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | Runnable doConstruct = new Runnable() { |
| 149 | public void run() { |
| 150 | try { |
| 151 | setValue(construct()); |
| 152 | } finally { |
| 153 | threadVar.clear(); |
| 154 | } |
| 155 | |
| 156 | SwingUtilities.invokeLater(doFinished); |
| 157 | } |
| 158 | }; |
| 159 | |
| 160 | Thread t = new Thread(doConstruct); |
| 161 | threadVar = new ThreadVar(t); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Start the worker thread. |
| 166 | */ |
| 167 | public void start() { |
| 168 | Thread t = threadVar.get(); |
| 169 | if (t != null) { |
| 170 | t.start(); |
| 171 | } |
| 172 | } |
| 173 | } |