Delegating Toggles

DelegatingToggleCommands and DelegatingToggleGroups are a new addition to GUI Commands version 2.1. They provide the equivalent functionality of DelegatingCommands for toggles.

Delegating Toggles
Delegating Toggles in the Demo

Delegating Toggle Commands

Delegating toggles are created in the same way as normal delegating commands. You first export your toggles via the DelegateProvider as follows:

   public ComponentWithToggles
   extends JPanel
   implements DelegateProvider
   {
      private DelegateProviderSupport delegates = new DelegateProviderSupport();

      public ComponentWithDelegates()
      {
         // create and add our delegates.
         delegates.add(new ToggleCommand("auto-save"));
      }

      /**
       * Implement DelegateProvider
       */
      public ActionCommandExecutor getActionCommandExecutor(String id)
      {
         return delegates.getActionCommandExecutor(id);
      }
   }

Then you create and bind the DelegatingToggleCommand.

   CommandContainer container = ...;
   container.setDelegateMediator(...);

   // create and bind the delegating command
   new DelegatingToggleCommand("auto-save").bind(container);

You can also use the bindAll convenience method.

   // create and bind 
   DelegatingToggleCommand.bindAll(container, "toggle-one", "toggle-two");

Delegating Toggle Groups

DelegatingToggleGroups allow you to delegate to all the members of a ToggleGroup in one hit. It also ensures the delegates render correctly when using Quaqua on the Mac.

The following demonstrates using a DelegatingToggleGroup:

   public ComponentWithToggleGroup
   extends JPanel
   implements DelegateProvider
   {
      // we'll use a container this time as we need to
      // bind our toggles and toggle group.
      private CommandContainer delegates = new CommandContainer();

      public ComponentWithDelegates()
      {
         // create the toggles and the toggle group.
         new AlignLeftCommand("align-left").bind(delegates);
         new AlignCenterCommand("align-center").bind(delegates);
         new AlignRightCommand("align-right").bind(delegates);

         new ToggleGroup("text-alignment").bind(delegates);
      }

      /**
       * Implement DelegateProvider
       */
      public ActionCommandExecutor getActionCommandExecutor(String id)
      {
         return delegates.getActionCommandExecutor(id);
      }
   }

Now we can simply create and bind the delegating group.

   CommandContainer container = ...;
   container.setDelegateMediator(...);

   delegatingToggles = new DelegatingToggleGroup("text-alignment");
   delegatingToggles.bind(container);

Once bound the group will use the DelegateMeditator to find and track all the members of the "text-alignment" toggle group vended from the DelegateMediator.