Using Annotations

Using annotated methods in GUI Commands is a two step process.

  1. Annotate your method with either @ActionCommand_ or @SwingWorkerCommand_.
  2. Invoke GuiCommands.bindAnnotatedCommands(..) to create and bind the commands from the annotated class.

The following shows an example of annotating methods with both the ActionCommand and SwingWorkerCommand annotations.

   public class AnnotatedClass
   {
      @ActionCommand_("open")
      public void doOpen()
      {
         // do the same operation.      
      }

      @SwingWorkerCommand_("save")
      public void doSave()
      {
         // do the save operation on a background thread.      
      }
   }

The value of the annotation is used to define the command id, thus in the above example the "open" command will invoke the doOpen() method.

In order to make the commands available they need to be created and bound to a container. This is achieved by calling GuiCommands.bindAnnotatedCommands(...) as the following example demonstrates.

   AnnotatedClass annotatedClass = ...;
   CommandContainer container = ...;

   // create commands from the annotated methods and bind them
   // to the conatiner.
   GuiCommands.bindAnnotatedCommands(container, annotatedClass);

In the above example the container will then be populated with both the "open" and "save" commands generated from the annotated class.

Method Parameters

Annotated methods can also specify a single parameter a type of either Map, ActionCommand or Window. Each of these is described below:

Parameter Details
Map The command parameter map is passed to the method.
ActionCommand The action command created from the annotated method is passed back to the method.
Window The commands invoker window is passed to the method.


The following example shows an annotated method that will be passed the commands invoking window.

   @ActionCommand_("hello-world")
   public void helloWorld(Window invokerWindow)
   {
      JOptionPane.showMessageDialog(invokerWindow, "Hello World!!!");
   }

SwingWorkerCommand Annotation

The @SwingWorker annotation allows you to create Swing Worker Commands directly from annotated methods. It defines a number of additional parameters that are specific to SwingWorkerCommands that are shown below.

Parameter Details
id The command id.
disable If true the command is disabled for the duration of the operation. The default value is true.
glassPane If true the glass pane is activated for the duration of the operation. The default value is false.


Methods annotated with @SwingWorker also support method parameters of the type SwingWorkerCommand. This allows the method to invoke the progress and chunking methods on the command. The following example shows a method that activates the glasspane and updates the progress of the invoking command.

   // we've had to use the fully qualified class name of the command as it
   // clashes with the name of the @SwingWorkerCommand_ annotation.
   @SwingWorkerCommand_(id="save", glassPane="true")
   public void doSave(com.pietschy.command.worker.SwingWorkerCommand command)
   {
      while (...)
      {
         command.setProgress(...);
      }
   }