wt.util
Class EventThread

java.lang.Object
  extended byjava.lang.Thread
      extended bywt.util.WTThread
          extended bywt.util.EventThread
All Implemented Interfaces:
Runnable

public class EventThread
extends WTThread

A thread for processing AWT events asynchronously. This is an extension of WTThread that is used to asynchronously process the semantic level AWT events, ActionEvent, ItemEvent, and TextEvent.

This class is used as a convenience to reduce the amount of extra code required to perform processing in a separate thread. This class allows event listeners to perform asynchronous processing of events by re-executing their event listener method in the context of a new thread. This avoids the need to create special Thread or Runnable classes just to provide customized run methods.

For example, the following code shows how it might be used from within a actionPerformed method to process a particular button's action in a separate thread.

   protected void actionPerformed (ActionEvent event)
   {
      Object source = e.getSource();
      if (source == goButton)
      {
         if (Thread.currentThread() instanceof EventThread)
         {
            // This is an asynchronous thread, do the processing
            ...
         }
         else
         {
            // Do the work in a new thread
            new EventThread(this, event).start(null, false);
         }
      }
      else ...
   }
The start method supports automatically disabling and enabling a target component as well as allowing the calling thread to wait until the new thread is ready to let it continue. For example, the following example disables the button and synchronizes with the new thread to let it access any volatile state before continuing. Since EventThread extends WTThread, listeners for property change events can be added as well.
   protected void processActionEvent (ActionEvent event)
   {
      Object source = e.getSource();
      if (source == goButton)
      {
         Thread thread = Thread.currentThread();
         if (thread instanceof EventThread)
         {
            // Capture volatile state of other components
            ...

            // Add our status component as a listener for thread status changes
            EventThread event_thread = (EventThread)thread;
            event_thread.addPropertyChangeListener(statusComponent);

            // Notify parent thread to continue
            event_thread.ready();

            // Do the processing
            ...
         }
         else
         {
            // Do the work in a new thread
            new EventThread(this, event).start(goButton, true);
         }
      }
      else ...
   }


Field Summary
private  Component disabledComponent
           
private  EventObject event
           
private  EventListener listener
           
private static String versionID
           
 
Fields inherited from class wt.util.WTThread
CREATED, DONE, PROGRESS_COUNT, PROGRESS_PERCENT, RUNNING, STATE, STATUS
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
  EventThread(ActionListener listener, ActionEvent event)
          Construct a thread to process a ActionEvent.
protected EventThread(EventListener listener, EventObject event)
          Protected constructor used by public constructors or subclasses.
  EventThread(ItemListener listener, ItemEvent event)
          Construct a thread to process a ItemEvent.
  EventThread(TextListener listener, TextEvent event)
          Construct a thread to process a TextEvent.
 
Method Summary
protected  void dispatchEvent()
          Dispatch event to listener.
 void run()
          Run method that dispatches event to listener.
 boolean start(Component disable_component, boolean wait)
          Start this thread.
 
Methods inherited from class wt.util.WTThread
addPropertyChangeListener, cancel, currentProgressCount, currentProgressPercent, currentStatus, firePropertyChange, firePropertyChange, getProgressCount, getProgressPercent, getState, getStatus, interrupt, isInterrupted, ready, removePropertyChangeListener, setInterruptHandler, setProgressCount, setProgressPercent, setStatus, start
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupted, isAlive, isDaemon, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

versionID

private static final String versionID
See Also:
Constant Field Values

listener

private EventListener listener

event

private EventObject event

disabledComponent

private Component disabledComponent
Constructor Detail

EventThread

protected EventThread(EventListener listener,
                      EventObject event)
Protected constructor used by public constructors or subclasses. Public constructors use type-specific event and listener arguments to guarantee that event and listener are compatible and that we'll know what listener method to invoke.

Parameters:
listener - the target EventListener object
event - the EventObject object

EventThread

public EventThread(ActionListener listener,
                   ActionEvent event)
Construct a thread to process a ActionEvent. The new thread becomes a member of the thread group associated with the WTContext object for the target ActionListener component. The run method of the new thread invokes the actionPerformed method on the target ActionListener.

Parameters:
listener - the target ActionListener object
event - the ActionEvent object

EventThread

public EventThread(ItemListener listener,
                   ItemEvent event)
Construct a thread to process a ItemEvent. The new thread becomes a member of the thread group associated with the WTContext object for the target ItemListener component. The run method of the new thread invokes the itemStateChanged method on the target ItemListener.

Parameters:
listener - the target ItemListener object
event - the ItemEvent object

EventThread

public EventThread(TextListener listener,
                   TextEvent event)
Construct a thread to process a TextEvent. The new thread becomes a member of the thread group associated with the WTContext object for the target TextListener component. The run method of the new thread invokes the textValueChanged method on the target TextListener.

Parameters:
listener - the target TextListener object
event - the TextEvent object
Method Detail

start

public boolean start(Component disable_component,
                     boolean wait)
Start this thread. This operation may optionally disable a target Component object as well as wait until the new thread calls its ready method. If a Component is disabled, it will automatically be re-enabled when the thread completes or if there is any exception thrown when trying to start the thread. This method returns a boolean value to indicate whether the thread was started successfully or the caller was interrupted while waiting.

Parameters:
disable_component - a Component to be disabled (may be null)
wait - calling thread should wait until this thread signals ready
Returns:
true if thread started successfully, false if waiting was interrupted

run

public final void run()
Run method that dispatches event to listener. On exit, if a Component was disabled, it is automatically re-enabled. Subclasses may extend for new event types by overriding the dispatchEvent method.

Specified by:
run in interface Runnable
Overrides:
run in class WTThread

dispatchEvent

protected void dispatchEvent()
Dispatch event to listener.