Dispatch task completions on the swing event thread.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@99 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2001-03-15 19:34:06 +00:00
parent 53d7315932
commit 23cce67346
@@ -1,10 +1,12 @@
// //
// $Id: TaskMaster.java,v 1.2 2000/12/07 06:14:36 mdb Exp $ // $Id: TaskMaster.java,v 1.3 2001/03/15 19:34:06 mdb Exp $
package com.samskivert.swing.util; package com.samskivert.swing.util;
import java.util.Hashtable; import java.util.Hashtable;
import com.samskivert.util.Log; import javax.swing.SwingUtilities;
import com.samskivert.Log;
/** /**
* The task master provides the ability for swing applications to invoke * The task master provides the ability for swing applications to invoke
@@ -21,9 +23,6 @@ import com.samskivert.util.Log;
*/ */
public class TaskMaster public class TaskMaster
{ {
/** The log object to be used by the task master and services. */
public static Log log = new Log("com.samskivert.swing.util.task");
/** /**
* Instructs the task master to run the supplied task. The task is * Instructs the task master to run the supplied task. The task is
* given the supplied name and can be referenced by that name in * given the supplied name and can be referenced by that name in
@@ -56,44 +55,75 @@ public class TaskMaster
_name = name; _name = name;
_task = task; _task = task;
_observer = observer; _observer = observer;
_mode = INVOKE;
} }
public void run () /**
* Invokes the task and then reports completion or failure later
* on the swing event dispatcher thread. We need to ensure that
* _mode and _result are visible to the various threads that
* invoke this runnable so run() is synchronized. Oh how I love
* Chapter 17.
*/
public synchronized void run ()
{ {
try { switch (_mode) {
Object result = _task.invoke(); default:
if (_observer != null) { case INVOKE:
try { // invoke the task and catch any errors
_observer.taskCompleted(_name, result); try {
} catch (Throwable t) { _result = _task.invoke();
log.warning("Observer choked in " + _mode = COMPLETED;
"taskCompleted(): " + t); } catch (Throwable t) {
log.logStackTrace(Log.WARNING, t); _result = t;
} _mode = FAILED;
} } finally {
TaskMaster.removeTask(_name);
}
} catch (Throwable t) { // queue ourselves up to run again on the swing event
if (_observer != null) { // dispatch thread if we have an observer
try { if (_observer != null) {
_observer.taskFailed(_name, t); SwingUtilities.invokeLater(this);
} catch (Throwable ot) { }
log.warning("Observer choked in taskFailed(): " + ot); break;
log.logStackTrace(Log.WARNING, t);
}
}
}
TaskMaster.removeTask(_name); case COMPLETED:
try {
_observer.taskCompleted(_name, _result);
} catch (Throwable t) {
Log.warning("Observer choked in " +
"taskCompleted(): " + t);
Log.logStackTrace(t);
}
break;
case FAILED:
try {
_observer.taskFailed(_name, (Throwable)_result);
} catch (Throwable ot) {
Log.warning("Observer choked in taskFailed(): " + ot);
Log.logStackTrace(ot);
}
break;
}
} }
public void abort () public void abort ()
{ {
log.warning("abort() not currently supported."); Log.warning("abort() not currently supported.");
} }
protected String _name; protected String _name;
protected Task _task; protected Task _task;
protected TaskObserver _observer; protected TaskObserver _observer;
protected int _mode;
protected Object _result;
protected static final int INVOKE = 0;
protected static final int COMPLETED = 1;
protected static final int FAILED = 2;
} }
protected static Hashtable _tasks = new Hashtable(); protected static Hashtable _tasks = new Hashtable();