Swing Workers

SwingWorkerCommands provide a convenient wrapper for the SwingWorker class. The API is essentially the same as SwingWorker, but includes a few command specific additions. The SwingWorkerCommand automatically disables during the operation and can optionally activate the glass pane. The command also provides a beforeStarting() method that can be overriden to perform checks on the EDT prior to starting the background task. The command will only continue if this method returns true. Once finished the command will invoke either finished(..), failed(..) or interrupted(..) on the EDT.

The following is a basic example:

   SwingWorkerCommand command = new SwingWorkerCommand("my-command")
   {
      @Override
      protected boolean beforeStarting()
      {
         // this method runs on the EDT, you can perform
         // checks and return false to cancel the operation.
         return true;
      }

      protected Object doInBackground()
      {
         // do long running operation and optionally
         // return the result
         return ...;
      }

      @Override
      protected void finished(Object result)
      {
         //update the UI
         ...
      }

      @Override
      protected void failed(ExecutionException e)
      {
         // handle the exception
         ...
      }

      @Override
      protected void cancelled(Exception e)
      {
         // The exception is either a CancellationException 
         // or InterruptedException
         ...
      }
   }

Disabling

SwingWorkerCommands will automatically disable while the background process is in opertaion. You can change this default behaviour using the setDisableWhileWorking(boolean) method as follows:

   command = new SwingWorkerCommand("my-command");
   command.setDisabledWhileWorking(false);

Glass Pane Integration

SwingWorkerCommands can be configured to activate the glasspane during operation. To do this you need to do the following:

  1. Invoke setActivateGlassPane(true) on the command.
  2. Configure the GlassPaneStrategy property of the appropriate CommandContainer.
  3. Bind the command to the container.

The following is an example:

   CommandContainer container = ...;
   GlassPaneStrategy glassPaneStrategy = ...;

   // set the strategy on the container.
   container.setGlassPaneStrategy(glassPaneStrategy);

   // create the worker and set the busy mode.
   SwingWorkerCommand worker = new SwingWorkerCommand("my-worker") {...};
   worker.setActivateGlassPane(true);

   // bind the worker so it can find the glass pane strategy to use.
   worker.bind(container);

Glass Pane Strategies

Worker commands activate and deactivate the glass pane using a GlassPaneStrategy. This allows you to use any glass pane approach you like, as long as you provide a strategy for controlling it.

The strategy defines two methods, one for activating and one for deactivating the glass pane. The methods take the invoker as the only parameter. SwingWorkerCommands pass themselves as the invoker allowing the strategy to monitor progress and optionally display chunking information. Since the invoker is of type Object you can also use the strategy for other invokers such as normal SwingWorker instances.

GUI Commands provides a simple strategy that beeps if the user clicks while it's activated. The following is an example:

   DefaultGlassPaneStrategy strategy = new DefaultGlassPaneStrategy();
   frame.setGlassPane(strategy.getGlassPane());