Watchmaker Framework for Evolutionary Computation API
(Version 0.7.1)

org.uncommons.swing
Class SwingBackgroundTask<V>

java.lang.Object
  extended by org.uncommons.swing.SwingBackgroundTask<V>
Type Parameters:
V - Type of result generated by the task.

public abstract class SwingBackgroundTask<V>
extends Object

A task that is executed on a background thread and then updates a Swing GUI. A task may only be executed once.

Author:
Daniel Dyer

Constructor Summary
protected SwingBackgroundTask()
           
 
Method Summary
 void execute()
          Asynchronous call that begins execution of the task and returns immediately.
protected  void onError(Throwable throwable)
          This method is invoked, on the Event Dispatch Thread, if there is an exception or error executing the performTask() method.
protected abstract  V performTask()
          Performs the processing of the task and returns a result.
protected  void postProcessing(V result)
          This method is invoked, on the Event Dispatch Thread, after the task has been executed.
 void waitForCompletion()
          Waits for the execution of this task to complete.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SwingBackgroundTask

protected SwingBackgroundTask()
Method Detail

execute

public void execute()
Asynchronous call that begins execution of the task and returns immediately. The performTask() method will be invoked on a background thread and, when it has completed, postProcessing(Object) will be invoked on the Event Dispatch Thread (or, if there is an exception, onError(Throwable) will be invoked instead - also on the EDT).

See Also:
performTask(), postProcessing(Object), onError(Throwable), waitForCompletion()

waitForCompletion

public void waitForCompletion()
                       throws InterruptedException
Waits for the execution of this task to complete. If the execute() method has not yet been invoked, this method will block indefinitely.

Throws:
InterruptedException - If the thread executing the task is interrupted.

performTask

protected abstract V performTask()
                          throws Exception
Performs the processing of the task and returns a result. Implement in sub-classes to provide the task logic. This method will run on a background thread and not on the Event Dispatch Thread and therefore should not manipulate any Swing components.

Returns:
The result of executing this task.
Throws:
Exception - The task may throw an exception, in which case the onError(Throwable) method will be invoked instead of postProcessing(Object).

postProcessing

protected void postProcessing(V result)
This method is invoked, on the Event Dispatch Thread, after the task has been executed. This empty default implementation should be over-ridden in sub-classes in order to provide GUI updates that should occur following successful task completion.

Parameters:
result - The result from the performTask() method.

onError

protected void onError(Throwable throwable)
This method is invoked, on the Event Dispatch Thread, if there is an exception or error executing the performTask() method. This default implementation displays a message dialog with details of the exception. It may be over-ridden in sub-classes.

Parameters:
throwable - The exception or error that was thrown while executing the task.

Watchmaker Framework for Evolutionary Computation API
(Version 0.7.1)