com.pietschy.command.worker
Class SwingWorkerCommand<T,V>

java.lang.Object
  extended by com.pietschy.command.Command
      extended by com.pietschy.command.ActionCommand
          extended by com.pietschy.command.worker.SwingWorkerCommand<T,V>
All Implemented Interfaces:
ActionCommandExecutor, Bindable, Identifiable
Direct Known Subclasses:
ReflectionSwingWorkerCommand

public abstract class SwingWorkerCommand<T,V>
extends ActionCommand

This is an action command implementation that performs the operation on a background thread using a SwingWorker. This implementation provides a number of enhancements for controling the state of buttons and activating the glass pane. By default, the command will disable for the duration of the background task, see setDisableWhileWorking(boolean) and setActivateGlassPane(boolean) for more details.

Unlike SwingWorker, this command provides a basic implementation of done() that calls get() and then invokes either finished(T), failed(java.lang.Exception) or cancelled(java.lang.Exception) accordingly.

 SwingWorkerCommand command = new SwingWorkerCommand<Result, Chunck>()
 {
    public boolean beforeStarting()
    {
       // check any preconditions on the EDT
       return true;
    }

    public Result doInBackground()
    {
       // do some work..
       while (...)
       {
          // optionally update the progress
          // and publish a chunck
          setProgress(progress);
          publish(chunck);
       }

       // and return the result..
       return result;
    }

    public finished(Result result)
    {
       // update the UI.
    }

    public failed(ExecutionException e)
    {
       // handle the error.
    }

    public void process(Chunck chunck)
    {
       // update the UI with chuncks as they
       // are published
    }
 }
 

Glass Pane

When setActivateGlassPane(boolean) is set to true the CommandContainer hierarchy is searched for a GlassPaneStrategy and if found it's activated for the duration of the task.

The GlassPaneStrategy is passed this command instance so it is able to monitor and display the progress and/or chunk information to the user.

See Also:
doInBackground(), (), failed(Exception), cancelled(Exception), setDisableWhileWorking(boolean), setActivateGlassPane(boolean), isWorking(), CommandContainer.setGlassPaneStrategy(GlassPaneStrategy)

Nested Class Summary
static class SwingWorkerCommand.WorkingMode
          Deprecated. since 2.1, use setDisableWhileWorking(boolean) and setActivateGlassPane(boolean) instead.
 
Field Summary
static SwingWorkerCommand.WorkingMode ACTIVATE_GLASS_PANE
          Deprecated. since 2.1, use setActivateGlassPane(boolean) and setDisableWhileWorking(boolean) instead.
static SwingWorkerCommand.WorkingMode DISABLE
          Deprecated. since 2.1, use setActivateGlassPane(boolean) and setDisableWhileWorking(boolean) instead.
static SwingWorkerCommand.WorkingMode DISABLE_AND_ACTIVATE_GLASS_PANE
          Deprecated. since 2.1, use setActivateGlassPane(boolean) and setDisableWhileWorking(boolean) instead.
static SwingWorkerCommand.WorkingMode DO_NOTHING
          Deprecated. since 2.1, use setActivateGlassPane(boolean) and setDisableWhileWorking(boolean) instead.
static java.lang.String PROGRESS_PROPERTY
           
static java.lang.String STATE_PROPERTY
           
static java.lang.String WORKING_PROPERTY
           
 
Fields inherited from class com.pietschy.command.ActionCommand
PARAMETER_ACTION_EVENT, PARAMETER_INVOKER, PARAMETER_INVOKER_WINDOW, PARAMETER_MODIFIERS
 
Fields inherited from class com.pietschy.command.Command
internalLog, PROPERTY_ENABLED, PROPERTY_VISIBLE
 
Constructor Summary
SwingWorkerCommand()
          Creates a new anonymous instance.
SwingWorkerCommand(boolean disableWhileWorking, boolean activateGlassPane)
          Creaets a new anonymous instance that disables and activates the glass pane as specified.
SwingWorkerCommand(java.lang.String id)
          Creates a new instance with the specified id.
SwingWorkerCommand(java.lang.String id, boolean disableWhileWorking, boolean activateGlassPane)
          Creates a new instance that disables and activates the glass pane as specified.
SwingWorkerCommand(java.lang.String id, SwingWorkerCommand.WorkingMode mode)
          Deprecated. since 2.1, use SwingWorkerCommand(boolean, boolean) instead.
SwingWorkerCommand(SwingWorkerCommand.WorkingMode mode)
          Deprecated. since 2.1, use SwingWorkerCommand(boolean, boolean) instead.
 
Method Summary
protected  boolean beforeStarting()
          Called on the EDT prior to the swing worker being started.
 boolean cancel(boolean mayInterruptIfRunning)
          Delegates to SwingWorker.cancel(boolean).
protected  void cancelled(java.lang.Exception e)
          Invoked on the EDT if the worker was cancelled.
protected abstract  T doInBackground()
          Invoked by to perform the commands work on a background thread.
protected  void done()
          Invoked on the EDT when doInBackground() has completed or thrown an exception.
protected  void failed(java.lang.Exception e)
          Invoked on the EDT if doInBackground() throws an exception.
protected  void finished(T result)
          Invoked on the EDT when doInBackground() completes successfully.
(package private)  void finishedWorking()
          Called by the worker when it has completed, either normally or due to an exception.
 T get()
          Delegates to SwingWorker.get().
 T get(long timeout, java.util.concurrent.TimeUnit timeUnit)
          Delegates to SwingWorker.get(long, TimeUnit).
protected  GlassPaneStrategy getGlassPaneStrategy()
          Searches the CommandContainer hierarchy to find the GlassPaneStrategy to use.
 int getProgress()
          Gets the progress of the background task.
 org.jdesktop.swingworker.SwingWorker.StateValue getState()
          Delegates to SwingWorker.getState().
 SwingWorkerCommand.WorkingMode getWorkingMode()
          Deprecated. since 2.1, see isActivateGlassPane() and isDisableWhileWorking().
protected  void handleExecute()
          Invokes beforeStarting() and if true is returned starts an swingworker instance that invokes doInBackground().
 boolean isActivateGlassPane()
          Checks if this command will automatically activate the glass pane while working.
 boolean isCancelled()
          Delegates to SwingWorker.isCancelled().
 boolean isDisableWhileWorking()
          Checks if this command will automatically disable while working.
 boolean isDone()
          Delegates to SwingWorker.isDone().
 boolean isEnabled()
          Checks if this command is enabled.
 boolean isWorking()
          Bound property the indicates that the command is current running a background task.
protected  void postExecute()
          This method is called after ActionCommand.handleExecute() has been called.
protected  void process(java.util.List<V> chunks)
          Invoked on the EDT to handle chucks published using #publish(V...).
protected  void publish(V... chunks)
          Delegates to SwingWorker#publish(V...).
 void setActivateGlassPane(boolean activateGlassPane)
          Configures the command to automatically activate the glass pane while the background process is in progress.
 void setDisableWhileWorking(boolean disableWhileWorking)
          Configures the command to automatically disable while the background process is in progress.
 void setProgress(int progress)
          Delegates to SwingWorker.setProgress(int).
 void setWorkingMode(SwingWorkerCommand.WorkingMode workingMode)
          Deprecated. since 2.1. Use setDisableWhileWorking(boolean) and setActivateGlassPane(boolean) instead.
(package private)  void startWorker()
          Starts a new worker, updates the working state and performs the actions specified by the current SwingWorkerCommand.WorkingMode.
 
Methods inherited from class com.pietschy.command.ActionCommand
addActionCommandListener, addInterceptor, bindToContainer, createButton, createButton, createButton, createButton, createMenuItem, createMenuItem, createMenuItem, createMenuItem, doButtonAttachments, doButtonDetachments, execute, execute, getActionAdapter, getActionAdapter, getActionEvent, getInvoker, getInvokerWindow, getModifiers, getParameter, getParameter, getParameters, installShortCut, installShortCut, preExecute, putParameter, putParameters, removeActionCommandListener, removeInterceptor, requestDefaultIn, unbindFromContainer, uninstallShortCut, uninstallShortCut
 
Methods inherited from class com.pietschy.command.Command
addContainerPropertyListener, addFace, addHoverListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, attach, bind, bind, bind, buttonIterator, configureAppearance, configureAppearances, configureButtonAppearances, configureButtonStates, configureButtonStates, createButton, createButton, createButton, createButton, createComponent, createMenuItem, createMenuItem, createMenuItem, createMenuItem, detach, faceExists, findBestFace, fireHoverEnded, fireHoverStarted, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlternativeFaceContexts, getButtonFactory, getButtonIn, getCommandContainer, getContainerProperty, getDefaultFace, getDefaultFace, getFace, getFace, getId, getMenuFactory, getProperty, getProperty, getPropertyNames, getToolbarFactory, isAnonymous, isAttachedTo, isBound, isVisible, loadConfiguration, notifyCommandContainerHierarchyChanged, putProperties, putProperty, rebind, removeContainerPropertyListener, removeHoverListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, requestFocusIn, setButtonFactory, setEnabled, setMenuFactory, setToolbarFactory, setVisible, toString, unbind, validateId
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.pietschy.command.ActionCommandExecutor
addPropertyChangeListener, addPropertyChangeListener, removePropertyChangeListener, removePropertyChangeListener
 
Methods inherited from interface com.pietschy.command.Identifiable
getId
 

Field Detail

PROGRESS_PROPERTY

public static final java.lang.String PROGRESS_PROPERTY
See Also:
Constant Field Values

STATE_PROPERTY

public static final java.lang.String STATE_PROPERTY
See Also:
Constant Field Values

WORKING_PROPERTY

public static final java.lang.String WORKING_PROPERTY
See Also:
Constant Field Values

DO_NOTHING

public static final SwingWorkerCommand.WorkingMode DO_NOTHING
Deprecated. since 2.1, use setActivateGlassPane(boolean) and setDisableWhileWorking(boolean) instead.
Do nothing while working.


DISABLE

public static final SwingWorkerCommand.WorkingMode DISABLE
Deprecated. since 2.1, use setActivateGlassPane(boolean) and setDisableWhileWorking(boolean) instead.
Disable the command while working. The default mode.


ACTIVATE_GLASS_PANE

public static final SwingWorkerCommand.WorkingMode ACTIVATE_GLASS_PANE
Deprecated. since 2.1, use setActivateGlassPane(boolean) and setDisableWhileWorking(boolean) instead.
Activate the glass pane while working.


DISABLE_AND_ACTIVATE_GLASS_PANE

public static final SwingWorkerCommand.WorkingMode DISABLE_AND_ACTIVATE_GLASS_PANE
Deprecated. since 2.1, use setActivateGlassPane(boolean) and setDisableWhileWorking(boolean) instead.
Disable the command and activate the glass pane while working.

Constructor Detail

SwingWorkerCommand

public SwingWorkerCommand()
Creates a new anonymous instance.


SwingWorkerCommand

public SwingWorkerCommand(java.lang.String id)
Creates a new instance with the specified id.

Parameters:
id - the command id.

SwingWorkerCommand

public SwingWorkerCommand(SwingWorkerCommand.WorkingMode mode)
Deprecated. since 2.1, use SwingWorkerCommand(boolean, boolean) instead.

Creaets a new anonymous instance with the specified SwingWorkerCommand.WorkingMode. The available modes are DISABLE, ACTIVATE_GLASS_PANE and DISABLE_AND_ACTIVATE_GLASS_PANE.

Parameters:
mode - the busy mode.

SwingWorkerCommand

public SwingWorkerCommand(boolean disableWhileWorking,
                          boolean activateGlassPane)
Creaets a new anonymous instance that disables and activates the glass pane as specified.

Parameters:
disableWhileWorking - true to automatically disable the command while working.
activateGlassPane - true to automatically activate the glass pane while working.

SwingWorkerCommand

public SwingWorkerCommand(java.lang.String id,
                          SwingWorkerCommand.WorkingMode mode)
Deprecated. since 2.1, use SwingWorkerCommand(boolean, boolean) instead.

Creates a new instance with the specified id and busy mode. The available modes are DISABLE, ACTIVATE_GLASS_PANE and DISABLE_AND_ACTIVATE_GLASS_PANE.

Parameters:
id - the command id.
mode - the busy mode.

SwingWorkerCommand

public SwingWorkerCommand(java.lang.String id,
                          boolean disableWhileWorking,
                          boolean activateGlassPane)
Creates a new instance that disables and activates the glass pane as specified.

Parameters:
id - the command id.
disableWhileWorking - true to automatically disable the command while working.
activateGlassPane - true to automatically activate the glass pane while working.
Method Detail

setWorkingMode

public void setWorkingMode(SwingWorkerCommand.WorkingMode workingMode)
Deprecated. since 2.1. Use setDisableWhileWorking(boolean) and setActivateGlassPane(boolean) instead.

Sets the working mode of this command. The available modes are DISABLE, ACTIVATE_GLASS_PANE and DISABLE_AND_ACTIVATE_GLASS_PANE and the default mode is DISABLE.

Parameters:
workingMode - the busy mode of this command.

isDisableWhileWorking

public boolean isDisableWhileWorking()
Checks if this command will automatically disable while working. The default value is true.

Returns:
true if the command will automatically disable while in progress, false if it will remain enabled.

setDisableWhileWorking

public void setDisableWhileWorking(boolean disableWhileWorking)
Configures the command to automatically disable while the background process is in progress. The default value is true.

Parameters:
disableWhileWorking - true to automatically disable the command while working, false to leave it enabled.

isActivateGlassPane

public boolean isActivateGlassPane()
Checks if this command will automatically activate the glass pane while working. The default value is false.

Returns:
true if the command will automatically activate the glass pane while in progress, false if it will remain enabled.

setActivateGlassPane

public void setActivateGlassPane(boolean activateGlassPane)
Configures the command to automatically activate the glass pane while the background process is in progress. The default value is false.

The glass pane will only be activated if there is a GlassPaneStrategy configured within the commands container hierarchy. See CommandContainer.setGlassPaneStrategy(GlassPaneStrategy).

Parameters:
activateGlassPane - true to automatically activate the glass pane while the command while working, false otherwise.
See Also:
CommandContainer.setGlassPaneStrategy(GlassPaneStrategy)

getWorkingMode

public SwingWorkerCommand.WorkingMode getWorkingMode()
Deprecated. since 2.1, see isActivateGlassPane() and isDisableWhileWorking().

Gets the current working mode of this command.

Returns:
the current working mode of this command.
See Also:
setWorkingMode(WorkingMode)

getGlassPaneStrategy

protected GlassPaneStrategy getGlassPaneStrategy()
Searches the CommandContainer hierarchy to find the GlassPaneStrategy to use.

Returns:
the GlassPaneStrategy or null if none found.

handleExecute

protected final void handleExecute()
Invokes beforeStarting() and if true is returned starts an swingworker instance that invokes doInBackground().

Specified by:
handleExecute in class ActionCommand

beforeStarting

protected boolean beforeStarting()
Called on the EDT prior to the swing worker being started. The worker will only proceed if this method returns true.

Returns:
true to proceed with the worker or false to terminate immediately. The default implemetation always returns true.

postExecute

protected final void postExecute()
Description copied from class: ActionCommand
This method is called after ActionCommand.handleExecute() has been called. It invokes all the ActionCommandInterceptors and notifies all the ActionCommandListeners.

Overrides:
postExecute in class ActionCommand

startWorker

final void startWorker()
Starts a new worker, updates the working state and performs the actions specified by the current SwingWorkerCommand.WorkingMode.


finishedWorking

final void finishedWorking()
Called by the worker when it has completed, either normally or due to an exception. This method clears the working state and undoes any actions specified by the current SwingWorkerCommand.WorkingMode.


isWorking

public boolean isWorking()
Bound property the indicates that the command is current running a background task.

Returns:
true if the command is currently running a background task, false otherwise.

isEnabled

public boolean isEnabled()
Checks if this command is enabled. This method takes into account the the current busy mode.

Specified by:
isEnabled in interface ActionCommandExecutor
Overrides:
isEnabled in class Command
Returns:
false if this command is disbled or the command is working and the busy operation is either DISABLE or DISABLE_AND_ACTIVATE_GLASS_PANE, otherwise true.

getProgress

public int getProgress()
Gets the progress of the background task.

Returns:
the progress of the background task.

setProgress

public void setProgress(int progress)
Delegates to SwingWorker.setProgress(int).

Parameters:
progress - the progress.

cancel

public boolean cancel(boolean mayInterruptIfRunning)
Delegates to SwingWorker.cancel(boolean).


isCancelled

public boolean isCancelled()
Delegates to SwingWorker.isCancelled().


isDone

public boolean isDone()
Delegates to SwingWorker.isDone().


get

public T get()
      throws java.lang.InterruptedException,
             java.util.concurrent.ExecutionException
Delegates to SwingWorker.get().

Throws:
java.lang.InterruptedException
java.util.concurrent.ExecutionException

get

public T get(long timeout,
             java.util.concurrent.TimeUnit timeUnit)
      throws java.lang.InterruptedException,
             java.util.concurrent.ExecutionException,
             java.util.concurrent.TimeoutException
Delegates to SwingWorker.get(long, TimeUnit).

Throws:
java.lang.InterruptedException
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException

doInBackground

protected abstract T doInBackground()
                             throws java.lang.Exception
Invoked by to perform the commands work on a background thread.

Returns:
the result of the operation.
Throws:
java.lang.Exception - if an error occurs. This exception will be propegated by get().

done

protected void done()
Invoked on the EDT when doInBackground() has completed or thrown an exception. This method in turn invokes either the #finished(T), failed(Exception) or cancelled(Exception) methods depending on the outcome of doInBackground().

See Also:
#finished(T), failed(Exception), cancelled(Exception)

finished

protected void finished(T result)
Invoked on the EDT when doInBackground() completes successfully. Subclasses can override to perform post task gui updates.

Parameters:
result - the object returned by doInBackground().
See Also:
failed(Exception), cancelled(Exception)

failed

protected void failed(java.lang.Exception e)
Invoked on the EDT if doInBackground() throws an exception. Subclasses can override to handle any errors.

See Also:
#finished(T), cancelled(Exception)

cancelled

protected void cancelled(java.lang.Exception e)
Invoked on the EDT if the worker was cancelled. This can either be because the invoked cancel(boolean) or because the doInBackground() thread was interrupated. Subclasses can override to handle.

Parameters:
e - the exception that was thrown by get().
See Also:
#finished(T), failed(Exception)

publish

protected final void publish(V... chunks)
Delegates to SwingWorker#publish(V...). The published chunks will be processed by #process(List).

Parameters:
chunks - the data to be published.

process

protected void process(java.util.List<V> chunks)
Invoked on the EDT to handle chucks published using #publish(V...).

Parameters:
chunks - the chunk published by the assicated #publish(V... method.

getState

public org.jdesktop.swingworker.SwingWorker.StateValue getState()
Delegates to SwingWorker.getState(). This property is bound.

Returns:
the value of SwingWorker.getState().


Copyright © 2006 - 2007 Andrew Pietsch