Skip to content

Action Container Builders

eugener edited this page Jul 3, 2012 · 3 revisions

Actions are used to separate functionality and state from specific UI component and can be associated with buttons, menus etc.

Sophisticated Swing applications use synchronized system of tool bars, top menus and context menus to simplify user interaction. All of these components can be thought of as action containers. Building them takes a lot time and code. Swing-bits provides a very simple way of accomplishing such task: first a hierarchy of actions is created and then any action container can be built using it (usually in one line of code). Here is an example:

import static org.oxbow.swingbits.action.Actions.actions;
import static org.oxbow.swingbits.action.Actions.collapsedGroup;
import static org.oxbow.swingbits.action.Actions.expandedGroup;
import static org.oxbow.swingbits.action.Actions.separator;

Icon icon = new ImageIcon( getClass().getResource("pin_blue.png"));

// create action hierarchy
Collection<Action> actions = actions(

    collapsedGroup("Group 1", icon).actions(
         new XCheckAction("Action 1.1", icon),
         collapsedGroup( "Group 1.1", icon).actions(
             new EmptyAction("Action 1.1.1", icon),
             new EmptyAction("Action 1.1.2"),
             new EmptyAction("Action 1.1.3")
         ),
         expandedGroup( "Group 1.2", icon).actions(
         new XRadioAction("Action 1.2.1"),
         new XRadioAction("Action 1.2.2", icon),
         new XRadioAction("Action 1.2.3")
         ),
         separator(),
         new XCheckAction("Action 1.2", icon)),
             collapsedGroup("Group 2", icon),
             collapsedGroup("Group 3"),
             new XCheckAction("Action 4", icon)
);

// create action containers
JMenuBar mbar = ActionContainerBuilderFactory.getMenuBarBuilder().build(actions);
JPopupMenu menu = ActionContainerBuilderFactory.getPopupMenuBuilder().build(actions);
JToolBar toolbar = ActionContainerBuilderFactory.getToolBarBuilder().build(actions);

// custom actions
public static class EmptyAction extends AbstractAction {
                
    public EmptyAction( String name, Icon icon ) { super( name, icon ); }
    public EmptyAction( String name ) { super( name ); }

    @Override
    public void actionPerformed(ActionEvent e) {}

}

@CheckAction
public static final class XCheckAction extends EmptyAction {

   public XCheckAction(String name, Icon icon) { super(name, icon);}
   public XCheckAction( String name ) { super( name ); }
                
}

@RadioAction
public static final class XRadioAction extends EmptyAction {

   public XRadioAction(String name, Icon icon) { super(name, icon); }
   public XRadioAction( String name ) { super( name ); }
               
}

Action hierarchy is built using a combination of custom actions and factory methods. Actions can be combined into action groups, which can be defined as expanded or collapsed. Custom actions can be annotated as @CheckAction or @RadioAction.

Here are the rules for action containers generation:

  • Menus: MenuBar and PopupMenu

    • if action group is collapsed sub-menu is generated
    • if action group is expanded - actions from the group are embedded into the current menu. Separators are used to visually separate the group.
    • check action produces CheckMenuItem
    • radio action produces RadioMenuItem. Groups of radio actions are automatically joined using ButtonGroups
    • For MenuBar Top level actions are expected to be of type action group
  • ToolBar

    • if action group is collapsed - drop-down menu button is generated
    • if action group is expanded - actions from the group are embedded into toolbar. Separators are used to visually separate the group.
    • check action produces ToggleButton
    • radio action produces ToggleButton. Groups of radio actions are automatically joined using ButtonGroups