3 TrakEM2 plugin for ImageJ(C).
4 Copyright (C) 2005,2006 Albert Cardona and Rodney Douglas.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation (http://www.gnu.org/licenses/gpl.txt)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 You may contact Albert Cardona at acardona at ini.phys.ethz.ch
20 Institute of Neuroinformatics, University of Zurich / ETH, Switzerland.
23 package ini
.trakem2
.utils
;
25 import ini
.trakem2
.ControlWindow
;
27 import java
.util
.HashMap
;
30 public abstract class Worker
implements Runnable
{
31 private String thread_name
;
32 private String task_name
;
33 private Thread thread
;
34 private boolean working
= false;
35 protected boolean quit
= false;
36 private boolean started
= false;
37 private boolean background
= false;
38 private boolean interrupt_on_quit
= false;
39 /** Extending classes may store a resulting piece of data. */
40 protected Object result
= null;
42 public Worker(String task_name
) {
43 this(task_name
, !ControlWindow
.isGUIEnabled() || null == IJ
.getInstance());
45 public Worker(String task_name
, boolean headless_mode
) {
46 super(); // the Run$_ tag is for ImageJ to properly grant it Macro.getOptions()
47 this.thread_name
= (headless_mode ?
"Run$_": "") + "Worker";
48 this.task_name
= task_name
;
50 public Worker(String task_name
, boolean headless_mode
, boolean interrupt_on_quit
) {
51 this(task_name
, headless_mode
);
52 this.interrupt_on_quit
= interrupt_on_quit
;
54 // private to the package
55 void setThread(Thread t
) {
58 public void setTaskName(String name
) { this.task_name
= name
; }
59 protected void startedWorking() {
63 public boolean hasStarted() { return started
; }
64 protected void finishedWorking() { this.working
= false; this.quit
= true; }
65 public boolean isWorking() { return working
; }
66 public String
getTaskName() { return task_name
; }
67 public String
getThreadName() { return thread_name
; }
68 public void setPriority(int priority
) {
69 if (null != this.thread
) thread
.setPriority(priority
);
71 /** If interrupt_on_quit, then it will call thread.getThreadGroup().interrupt() to set a quit flag to each child thread. */
74 if (interrupt_on_quit
) {
75 if (null != thread
) thread
.getThreadGroup().interrupt();
78 public void join() throws InterruptedException
{
79 if (null != thread
) thread
.join();
81 public boolean hasQuitted() { return this.quit
; }
82 protected void setAsBackground(boolean b
) { this.background
= b
; }
83 /** Whether the work is done on the background, without need to bring ImageJ toolbar to front for instance. */
84 public boolean onBackground() { return this.background
; }
86 private boolean cleaned_up
= false;
87 // private to the package
90 if (cleaned_up
) return;
95 /** When quitted or interrupted, executes this method once. */
96 public void cleanup() {}
98 /** Returns data generated by the worker, or null if none was set. */
99 public Object
getResult() { return this.result
; }
101 // ugly, ugly ... why, java, do you make me do this, when all I need is a closure?
102 private HashMap properties
= null;
103 public synchronized void setProperty(Object key
, Object value
) {
104 if (null == key
) return;
105 if (null == properties
) properties
= new HashMap();
106 properties
.put(key
, value
);
108 public synchronized Object
getProperty(Object key
) {
109 if (null == key
|| null == properties
) return null;
110 return properties
.get(key
);
113 /** A class that calls run() wrapped properly for task monitoring;
114 * Create it like this:
116 * Bureaucrat b = Bureaucrat.createAndStart(new Worker.Task("Title") { public void exec() {
122 static public abstract class Task
extends Worker
{
123 public Task(String title
) {
127 abstract public void exec();
133 } catch (Throwable t
) {