net.pacbell.jfai.toh.util
Class AbstractSwingWorker

java.lang.Object
  extended by net.pacbell.jfai.toh.util.AbstractSwingWorker
Direct Known Subclasses:
AbstractSwingWorkerWithWaitCursor

public abstract class AbstractSwingWorker
extends Object

Subclasses perform GUI-related work in a dedicated thread. For instructions on using this class, see Swing tutorial

Author:
Doug Lea, Jürgen Failenschmid
See Also:
EDU.oswego.cs.dl.util.concurrent , JDK 5.0

Constructor Summary
AbstractSwingWorker()
          Creates a thread that will call construct()followed by finished()and then exits.
 
Method Summary
abstract  Object construct()
          Computes the value to be returned by the get()method.
 void finished()
          Called on the event dispatching thread (not on the worker thread) after the construct()method has returned.
 Object get()
          Returns the value created by the construct()method.
protected  net.pacbell.jfai.toh.util.AbstractSwingWorker.ThreadVar getThreadVar()
          Gets the synchronization instance.
protected  Object getValue()
          Gets the value produced by the worker thread, or null if it hasn't been constructed yet.
 void interrupt()
          Interrupts the worker thread.
static void invokeAndWait(Runnable runner)
          Guarantees that the argument is executed by the event dispatch thread while the caller is blocked.
static void invokeLater(Runnable runner)
          Guarantees that the argument is executed by the event dispatch thread.
protected  void setThreadVar(net.pacbell.jfai.toh.util.AbstractSwingWorker.ThreadVar threadVar)
          Sets the synchronization instance.
protected  void setValue(Object x)
          Sets the value produced by the worker thread.
static void showMessageDialog(Component parentComponent, Object message, String title, int messageType)
          Displays a dialog and waits until the dialog is closed.
 void start()
          Starts the worker thread.
 void started()
          Called on the event dispatching thread (not on the worker thread) before the construct()method is called.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractSwingWorker

public AbstractSwingWorker()
Creates a thread that will call construct()followed by finished()and then exits.

Method Detail

invokeAndWait

public static void invokeAndWait(Runnable runner)
                          throws InterruptedException,
                                 InvocationTargetException
Guarantees that the argument is executed by the event dispatch thread while the caller is blocked. This will execute the runner immediately if the current thread is the event dispatch thread. Otherwise the runner is executed after all pending events in the dispatch queue.

Note that if the Runnable.run() method throws an uncaught exception it is caught and re-thrown as an InvocationTargetExceptionin the callers thread.

Parameters:
runner - a Runnable
Throws:
InterruptedException - if the worker thread is interrupted
InvocationTargetException - if an uncaught exception occurs in the worker thread

invokeLater

public static void invokeLater(Runnable runner)
Guarantees that the argument is executed by the event dispatch thread. This will execute the runner immediately if the current thread is the event dispatch thread. Otherwise the runner is executed after all pending events in the dispatch queue.

Parameters:
runner - a Runnable

showMessageDialog

public static void showMessageDialog(Component parentComponent,
                                     Object message,
                                     String title,
                                     int messageType)
Displays a dialog and waits until the dialog is closed. Guarantees that the dialog is displayed from the event dispatch thread.

Parameters:
parentComponent - the parent component of the dialog
message - a message
title - the dialog title
messageType - the dialog type
See Also:
JOptionPane.showMessageDialog(java.awt.Component, java.lang.Object, java.lang.String, int)

construct

public abstract Object construct()
Computes the value to be returned by the get()method.

Returns:
the computed value

finished

public void finished()
Called on the event dispatching thread (not on the worker thread) after the construct()method has returned. By default, does nothing.


get

public Object get()
Returns the value created by the construct()method. Returns null if either the constructing thread or the current thread was interrupted before a value was produced.

Returns:
the value object

interrupt

public void interrupt()
Interrupts the worker thread. Call this method to force the worker to stop what it's doing.


start

public void start()
Starts the worker thread.

See Also:
invokeAndWait(Runnable), started()

started

public void started()
Called on the event dispatching thread (not on the worker thread) before the construct()method is called. By default, does nothing.


getThreadVar

protected net.pacbell.jfai.toh.util.AbstractSwingWorker.ThreadVar getThreadVar()
Gets the synchronization instance.

Returns:
the threadVar for synchronized access to the worker thread

getValue

protected Object getValue()
Gets the value produced by the worker thread, or null if it hasn't been constructed yet.

Returns:
the value object (or null)

setThreadVar

protected void setThreadVar(net.pacbell.jfai.toh.util.AbstractSwingWorker.ThreadVar threadVar)
Sets the synchronization instance.

Parameters:
threadVar - the threadVar to set for synchronized access to the worker thread

setValue

protected void setValue(Object x)
Sets the value produced by the worker thread.

Parameters:
x - the value object