Creating Commands

There are three steps to creating an ActionCommand:

  1. Create the configuration.
  2. Load the configuration.
  3. Extend ActionCommand and implement handleExecute().

1. Command Configuration

All configuration in GUI Commands is loaded from ResourceBundles. The property keys consists of the command id, an @ symbol then the property name. Property names are hierarchical.

For example, let's add the following to MyCommands.properties:

   hello-world@face.text=_Hello World!!!@control H
   hello-world@face.icon=classpath:images/hello-world.png
   hello-world@face.description=Says hello ($accelerator).
   # create a user defined property
   hello-world@properties.greeting=Hello World!!!

The above examples configures the text, icon and description of the "hello-world" command. The text property specifies that 'H' is the mnemonic and "Control H" is the accelerator. The description contains the command accelerator in parenthesis.

We also configure a custom user property called greeting that we will use during the command's execution. You can create any number of custom properties in this way.

2. Load the configuration

The next step is to load the configuration. GUI Commands allows you to provide just the resource bundle name using the same syntax as ResourceBundle.getBundle(String). You can load as many bundles as you like.

Now we load the configuration file MyCommands.properties defined above:

   // load our configuration file
   GuiCommands.load("MyCommands");

3. Create the Command

Finally all you need to do is extend ActionCommand and implement handleExecute(). You can then create as many buttons and menus as you like.

   /**
    * A command with the id "hello-world" that displays 
    * a greeting over the current frame.
    */
   public class HelloWorldCommand extends ActionCommand
   {
      public HelloWorldCommand()
      {
         super("hello-world");
      }

      /**
       * Say hello
       */
      public void handleExecute()
      {
         Window window = getInvokerWindow();

         // get the user property we defined in the configuration
         String greeting = getProperty("greeting");

         // show the greeting
         JOptionPane.showMessageDialog(window, greeting);
      }
   }

   // now create the command.
   HelloWorldCommand helloWorldCommand = new HelloWorldCommand();

   // and ask it for a button.
   AbstractButton button = helloWorldCommand.createButton();