net.sf.swinglib
Class AsynchronousOperation<T>

java.lang.Object
  extended by net.sf.swinglib.AsynchronousOperation<T>
All Implemented Interfaces:
Runnable

public abstract class AsynchronousOperation<T>
extends Object
implements Runnable

A base class for implementing operations that need to run on their own thread and report back to the event thread. Unlike Sun's SwingWorker class, this object does not spawn its own thread. Instead, it must be passed to a program-created thread, or better, a threadpool.

To use, subclass and pass an instance to your operation thread.

You must implement at least the performOperation() method, which is executed on the operation thread. This method may return a single object, or throw any exception type. Depending on how it completes (return/throw), one of onSuccess(T), onFailure(java.lang.Throwable) will then be executed on the event thread.


Constructor Summary
AsynchronousOperation()
           
 
Method Summary
protected  void onComplete()
          This method is invoked on the event thread when the operation completes, regardless of whether it succeeded or failed.
protected  void onFailure(Throwable e)
          This method is invoked on the event thread if performOperation() threw an exception.
protected  void onSuccess(T result)
          This method is invoked on the event thread after a successful call to performOperation().
protected abstract  T performOperation()
          The concrete class implements this method, which is executed on the non-event thread.
 void run()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AsynchronousOperation

public AsynchronousOperation()
Method Detail

run

public final void run()
Specified by:
run in interface Runnable

performOperation

protected abstract T performOperation()
                               throws Exception
The concrete class implements this method, which is executed on the non-event thread. It is permitted to return a value that is then passed to the code running on the event thread. It is also permitted to throw any exception type.

Throws:
Exception

onComplete

protected void onComplete()
This method is invoked on the event thread when the operation completes, regardless of whether it succeeded or failed. It exists so that subclasses can manage user feedback (such as resetting a busy cursor).


onSuccess

protected void onSuccess(T result)
This method is invoked on the event thread after a successful call to performOperation(). Application code typically overrides to do something with that result.


onFailure

protected void onFailure(Throwable e)
This method is invoked on the event thread if performOperation() threw an exception. Application code typically overrides to do something with that exception.