Package org.uncommons.swing
Class SwingBackgroundTask<V>
- java.lang.Object
-
- 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.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
SwingBackgroundTask()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description 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 theperformTask()
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.
-
-
-
Method Detail
-
execute
public void execute()
Asynchronous call that begins execution of the task and returns immediately. TheperformTask()
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).
-
waitForCompletion
public void waitForCompletion() throws InterruptedException
Waits for the execution of this task to complete. If theexecute()
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 theonError(Throwable)
method will be invoked instead ofpostProcessing(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 theperformTask()
method.
-
onError
protected void onError(Throwable throwable)
This method is invoked, on the Event Dispatch Thread, if there is an exception or error executing theperformTask()
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.
-
-