com.sas.swing.visuals.util
Class ActionUIFactory

com.sas.swing.visuals.util.ActionUIFactory
All Implemented Interfaces:
LayoutConstants, javax.swing.SwingConstants

public class ActionUIFactory
implements LayoutConstants, javax.swing.SwingConstants

Creates user-interface components based on action definitions. The ui-components register listeners with the actions, so that as actions are, for example, enabled and disabled, the ui-components will update accordingly, thus freeing the application from having to enable/disable multiple components per action.

A separate factory method is provided for the instantiation of each ui-component, so that applications have the ability to use their own ui-component subclasses. See newMenuBar() et al.

Methods that map ui-components back to the actions that they were created from are provided, so that applications can readily resync views with models. See getAction(JMenuBar) et al.

Sample usage:

 import java.awt.*;
 import javax.swing.*;

 public class Test
    {

   public static void main (String[] args)
      {
      JFrame app = new JFrame();

      // load actions for menu bar
      ActionList menuActions = ActionResourceLoader.getActionList ("Menu");

      // load actions for tool bar
      ActionList toolActions = ActionResourceLoader.getActionList ("Toolbar");

      // set application context on our actions, so that we can find
      // ourselves from command handlers
      setAppContext (menuActions, app);
      setAppContext (toolActions, app);

      // create menu and tool bar
      ActionUIFactory auf = new ActionUIFactory ();
      JMenuBar menuBar = auf.getMenuBar (menuActions);
      JToolBar toolBar = auf.getToolBar (toolActions);

      // add menu and toolbar to app and display it
      app.setJMenuBar (menuBar);
      app.getContentPane().setLayout (new BorderLayout ());
      app.getContentPane().add (toolBar, BorderLayout.NORTH);
      app.setSize (new Dimension (400, 400));
      app.setVisible (true);
      }

   private static final String APP_CONTEXT = "Test";
   private static void setAppContext (Action action, Object app)
      {
      action.putValue (APP_CONTEXT, app);
      if (action instanceof ActionList)
         {
         java.util.List actions = ((ActionList)action).getActions();
         for (int i = 0, iCount = actions.size(); i < iCount; i++)
            {
            if (actions.get(i) == ActionList.SEPARATOR)
               continue;
            setAppContext ((Action)actions.get(i), app);
            }
         }
      }

   }

 public class ExitCommand extends com.sas.util.AbstractCommand
   {
   public void execute (Object arg)
      {
      Action action = (Action)arg;
      System.out.println (action.getValue("TestApp"));
      System.exit (0);
      }

   public Object clone ()
      {
      return null;
      }
   }
 

TODO: show dynamic example like MRU list


Field Summary
 
Fields inherited from interface com.sas.swing.visuals.util.LayoutConstants
DOTS_BUTTON_INSETS, EMPTY_INSETS, HGAP, HMARGIN, INDENTED_BORDER, TAB_BORDER, TEXTFIELD_INSETS, VGAP, VMARGIN
 
Constructor Summary
ActionUIFactory()
           
 
Method Summary
 com.sas.swing.util.Action getAction(javax.swing.AbstractButton toolItem)
          Returns the action that serves as a model to a given tool item.
 com.sas.swing.util.Action getAction(javax.swing.JMenuItem menuItem)
          Returns the action that serves as a model to a given menu item.
 com.sas.swing.util.ActionList getActionList(javax.swing.JMenu menu)
          Returns the list of actions that serve as a model to a given menu.
 com.sas.swing.util.ActionList getActionList(javax.swing.JMenuBar menuBar)
          Returns the list of actions that serve as a model to a given menu bar.
 com.sas.swing.util.ActionList getActionList(javax.swing.JPopupMenu popupMenu)
          Returns the list of actions that serve as a model to a given popup menu.
 com.sas.swing.util.ActionList getActionList(javax.swing.JToolBar toolBar)
          Returns the list of actions that serve as a model to a given tool bar.
 javax.swing.AbstractButton getButton(com.sas.swing.util.Action action)
          Returns a button on the given action.
 IconMap getIconMap()
          Returns the map for accessing icons that are associated with actions.
 javax.swing.JMenu getMenu(com.sas.swing.util.Action action)
          Returns a menu representation of the given actions.
 javax.swing.JMenu getMenu(com.sas.swing.util.ActionList actionList)
          Returns a menu representation of the given actions.
 javax.swing.JMenuBar getMenuBar(com.sas.swing.util.ActionList actionList)
          Returns a menu bar representation of the given actions.
 javax.swing.JMenuItem getMenuItem(com.sas.swing.util.Action action)
          Returns a menu item based on the given action.
 javax.swing.JPopupMenu getPopupMenu(com.sas.swing.util.ActionList actionList)
          Returns a popup menu representation of the given actions.
 com.sas.net.ResourceLocatorInterface getResourceLocator()
          Returns the handler for locating resources, such as images, that are associated with actions.
 javax.swing.AbstractButton getShortcutItem(com.sas.swing.util.Action action)
           
 javax.swing.AbstractButton getShortcutItem(com.sas.swing.util.Action action, boolean smallIcons)
           
 javax.swing.AbstractButton getTaskItem(com.sas.swing.util.Action action)
          Returns a task item based on the given action.
 javax.swing.JToolBar getToolBar(com.sas.swing.util.ActionList actionList)
          Returns a tool bar representation of the given actions.
 javax.swing.AbstractButton getToolItem(com.sas.swing.util.Action action)
          Returns a tool item based on the given action.
 void loadActionResources(com.sas.swing.util.Action action)
           
protected  javax.swing.AbstractButton newButton()
          Creates a new button.
protected  javax.swing.AbstractButton newButton(com.sas.swing.util.Action action)
          Creates a new button based on a given action.
protected  javax.swing.AbstractButton newCheckBox()
          Creates a new check box.
protected  javax.swing.JMenuItem newCheckBoxMenuItem()
          Creates a new check box menu item.
protected  javax.swing.JMenu newMenu()
          Creates a new menu.
protected  javax.swing.JMenuBar newMenuBar()
          Creates a new menu bar.
protected  javax.swing.JMenuItem newMenuItem()
          Creates a new menu item.
protected  javax.swing.JMenuItem newMenuItem(com.sas.swing.util.Action action)
          Creates a new menu item based on a given action.
protected  javax.swing.JPopupMenu newPopupMenu()
          Creates a new popup menu.
protected  javax.swing.AbstractButton newRadioButton()
          Creates a new radio button.
protected  javax.swing.JMenuItem newRadioButtonMenuItem()
          Creates a new radio button menu item.
protected  javax.swing.AbstractButton newShortcutItem(com.sas.swing.util.Action action)
          Creates a new shortcut item based on a given action.
protected  javax.swing.AbstractButton newShortcutItem(com.sas.swing.util.Action action, boolean smallIcons)
          Creates a new shortcut item based on a given action.
protected  javax.swing.AbstractButton newTaskItem(com.sas.swing.util.Action action)
          Creates a new task item based on a given action.
protected  javax.swing.AbstractButton newToggleButton()
          Creates a new toggle button.
protected  javax.swing.JToolBar newToolBar()
          Creates a new tool bar.
protected  javax.swing.AbstractButton newToolItem(com.sas.swing.util.Action action)
          Creates a new tool item based on a given action.
 void populateMenuBar(javax.swing.JMenuBar menuBar, com.sas.swing.util.ActionList actionList)
          Populates a JMenuBar with the actions from the given ActionList.
 void populatePopupMenu(javax.swing.JPopupMenu menu, com.sas.swing.util.ActionList actionList)
          Populates a JPopupMenu with the action from the given ActionList.
 void populateToolBar(javax.swing.JToolBar toolBar, com.sas.swing.util.ActionList actionList)
          Populates a JToolBar with the action from the given ActionList.
 void setIconMap(IconMap iconMap)
          Sets the map for accessing icons that are associated with actions.
 void setResourceLocator(com.sas.net.ResourceLocatorInterface resourceLocator)
          Returns the handler for locating resources, such as images, that are associated with actions.
 

Constructor Detail

ActionUIFactory

public ActionUIFactory()
Method Detail

populateMenuBar

public void populateMenuBar(javax.swing.JMenuBar menuBar,
                            com.sas.swing.util.ActionList actionList)
Populates a JMenuBar with the actions from the given ActionList.

Parameters:
menuBar - the instance of the JMenuBar to have the action added to
actionList - the list of actions to add to the menu bar; each top-level item in actionList.getActions() must itself be an ActionList

getMenuBar

public javax.swing.JMenuBar getMenuBar(com.sas.swing.util.ActionList actionList)
Returns a menu bar representation of the given actions.

New JMenuBar instances are created using the factory method newMenuBar() so that an application may override and create a JMenuBar subclass.

Parameters:
actionList - the list of actions to add to the menu bar; each top-level item in actionList.getActions() must itself be an ActionList
Returns:
a JMenuBar; will never be null
See Also:
newMenuBar()

getMenu

public javax.swing.JMenu getMenu(com.sas.swing.util.ActionList actionList)
Returns a menu representation of the given actions.

New JMenu instances are created using the factory method newMenu() so that an application may override and create a JMenu subclass.

Parameters:
actionList - the list of actions to add to the menu
Returns:
a JMenu; will never be null
See Also:
newMenu()

getMenu

public javax.swing.JMenu getMenu(com.sas.swing.util.Action action)
Returns a menu representation of the given actions.

New JMenu instances are created using the factory method newMenu() so that an application may override and create a JMenu subclass.

Parameters:
actionList - the list of actions to add to the menu
Returns:
a JMenu; will never be null
See Also:
newMenu()

populatePopupMenu

public void populatePopupMenu(javax.swing.JPopupMenu menu,
                              com.sas.swing.util.ActionList actionList)
Populates a JPopupMenu with the action from the given ActionList.

Parameters:
menu - the instance of the JPopupMenu to have the action added to
actionList - the list of actions to add to the menu bar; each top-level item in actionList.getActions() must itself be an ActionList

getPopupMenu

public javax.swing.JPopupMenu getPopupMenu(com.sas.swing.util.ActionList actionList)
Returns a popup menu representation of the given actions.

New JPopupMenu instances are created using the factory method newPopupMenu() so that an application may override and create a JPopupMenu subclass.

Parameters:
actionList - the list of actions to add to the menu
Returns:
a JPopupMenu; will never be null
See Also:
newPopupMenu()

getMenuItem

public javax.swing.JMenuItem getMenuItem(com.sas.swing.util.Action action)
Returns a menu item based on the given action.

The determination, creation, and customization of the menu item is delegated to the factory method newMenuItem(Action).

Parameters:
action - the action to create a menu item for
Returns:
a JMenuItem; will never be null
See Also:
newMenuItem(Action)

populateToolBar

public void populateToolBar(javax.swing.JToolBar toolBar,
                            com.sas.swing.util.ActionList actionList)
Populates a JToolBar with the action from the given ActionList.

Parameters:
toolBar - the instance of the JToolBar to have the action added to
actionList - the list of actions to add to the menu bar; each top-level item in actionList.getActions() must itself be an ActionList

getToolBar

public javax.swing.JToolBar getToolBar(com.sas.swing.util.ActionList actionList)
Returns a tool bar representation of the given actions.

New JToolBar instances are created using the factory method newToolBar() so that an application may override and create a JToolBar subclass.

Parameters:
actionList - the list of actions to add to the tool bar
Returns:
a JToolBar; will never be null
See Also:
newToolBar()

getButton

public javax.swing.AbstractButton getButton(com.sas.swing.util.Action action)
Returns a button on the given action.

The determination, creation, and customization of the button is delegated to the factory method newButton(Action).

Parameters:
action - the action to create a button for
Returns:
a AbstractButton; will never be null
See Also:
newButton(Action)

getToolItem

public javax.swing.AbstractButton getToolItem(com.sas.swing.util.Action action)
Returns a tool item based on the given action.

The determination, creation, and customization of the tool item is delegated to the factory method newToolItem(Action).

Parameters:
action - the action to create a tool item for
Returns:
a AbstractButton; will never be null
See Also:
newToolItem(Action)

getTaskItem

public javax.swing.AbstractButton getTaskItem(com.sas.swing.util.Action action)
Returns a task item based on the given action.

The determination, creation, and customization of the task item is delegated to the factory method newTaskItem(Action).

Parameters:
action - the action to create a tool item for
Returns:
a AbstractButton; will never be null
See Also:
newTaskItem(Action)

getShortcutItem

public javax.swing.AbstractButton getShortcutItem(com.sas.swing.util.Action action)

getShortcutItem

public javax.swing.AbstractButton getShortcutItem(com.sas.swing.util.Action action,
                                                  boolean smallIcons)

getActionList

public com.sas.swing.util.ActionList getActionList(javax.swing.JMenuBar menuBar)
Returns the list of actions that serve as a model to a given menu bar.

Parameters:
menuBar - a JMenuBar from getMenuBar()
Returns:
an ActionList or null if the menu bar is not from getMenuBar()

getActionList

public com.sas.swing.util.ActionList getActionList(javax.swing.JPopupMenu popupMenu)
Returns the list of actions that serve as a model to a given popup menu.

Parameters:
popupMenu - a JPopupMenu from getPopupMenu()
Returns:
an ActionList or null if the menu is not from getPopupMenu()

getActionList

public com.sas.swing.util.ActionList getActionList(javax.swing.JMenu menu)
Returns the list of actions that serve as a model to a given menu.

Convenience method for (ActionList)menu.getAction().

Parameters:
menu - a JMenu from getMenu()
Returns:
an ActionList or null if the menu is not from getMenu()

getAction

public com.sas.swing.util.Action getAction(javax.swing.JMenuItem menuItem)
Returns the action that serves as a model to a given menu item.

Convenience method for (Action)menuItem.getAction().

Parameters:
menuItem - a JMenuItem from getMenuItem()
Returns:
an Action or null if the menu item is not from getMenuItem()

getActionList

public com.sas.swing.util.ActionList getActionList(javax.swing.JToolBar toolBar)
Returns the list of actions that serve as a model to a given tool bar.

Parameters:
toolBar - a JToolBar from getToolBar()
Returns:
an ActionList or null if the tool bar is not from getToolBar()

getAction

public com.sas.swing.util.Action getAction(javax.swing.AbstractButton toolItem)
Returns the action that serves as a model to a given tool item.

Convenience method for (Action)toolItem.getAction().

Parameters:
toolItem - a AbstractButton from getToolItem()
Returns:
an Action or null if the tool item is not from getToolItem()

getResourceLocator

public com.sas.net.ResourceLocatorInterface getResourceLocator()
Returns the handler for locating resources, such as images, that are associated with actions.

Returns:
an instance of ResourceLocatorInterface or null
See Also:
setResourceLocator(com.sas.net.ResourceLocatorInterface)

getIconMap

public IconMap getIconMap()
Returns the map for accessing icons that are associated with actions.

Returns:
an instance of IconMap or null
See Also:
setIconMap(com.sas.swing.visuals.util.IconMap)

setIconMap

public void setIconMap(IconMap iconMap)
Sets the map for accessing icons that are associated with actions.

Parameters:
iconMap - an instance of IconMap or null
See Also:
getIconMap()

setResourceLocator

public void setResourceLocator(com.sas.net.ResourceLocatorInterface resourceLocator)
Returns the handler for locating resources, such as images, that are associated with actions.

Action.getValue(Action.SMALL_ICON_NAME), for example, returns the name of an icon. The resource locator will be used to locate and load the image identified by the name, so that a javax.swing.Icon can be created for the corresponding menu or tool item.

Parameters:
resourceLocator - an instance of ResourceLocatorInterface or null
See Also:
getResourceLocator()

newMenuBar

protected javax.swing.JMenuBar newMenuBar()
Creates a new menu bar.

Returns:
an instance of JMenuBar

newPopupMenu

protected javax.swing.JPopupMenu newPopupMenu()
Creates a new popup menu.

Returns:
an instance of JPopupMenu

newMenu

protected javax.swing.JMenu newMenu()
Creates a new menu.

Returns:
an instance of JMenu

newMenuItem

protected javax.swing.JMenuItem newMenuItem(com.sas.swing.util.Action action)
Creates a new menu item based on a given action.

If the action's type property, Action.TYPE, is set to Action.TYPE_BOOLEAN, then a check box menu item is created instead of a regular menu item. If in addition to the type being boolean, the action's group name property, Action.GROUP_NAME, is set, then a radio button menu item is created instead of a check box menu item.

New regular menu items, check box menu items, and radio button menu items are created using the factory methods newMenuItem(), newCheckBoxMenuItem and newRadioButtonMenuItem, respectively, so that an application may override and create subclasses of those.

Parameters:
action - the action to create a menu item for
Returns:
an instance of JMenuItem
See Also:
newMenuItem(com.sas.swing.util.Action), newCheckBoxMenuItem(), newRadioButtonMenuItem()

newMenuItem

protected javax.swing.JMenuItem newMenuItem()
Creates a new menu item.

Returns:
an instance of JMenuItem

newCheckBoxMenuItem

protected javax.swing.JMenuItem newCheckBoxMenuItem()
Creates a new check box menu item.

Returns:
an instance of javax.swing.JCheckBoxMenuItem

newRadioButtonMenuItem

protected javax.swing.JMenuItem newRadioButtonMenuItem()
Creates a new radio button menu item.

Returns:
an instance of javax.swing.JRadioButtonMenuItem

newToolBar

protected javax.swing.JToolBar newToolBar()
Creates a new tool bar.

Returns:
an instance of JToolBar

newTaskItem

protected javax.swing.AbstractButton newTaskItem(com.sas.swing.util.Action action)
Creates a new task item based on a given action.

If the action's type property, Action.TYPE, is set to Action.TYPE_BOOLEAN, then a check box is created instead of a regular button. If in addition to the type being boolean, the action's group name property, Action.GROUP_NAME, is set, then a radio button is created instead of a check box.

The item's text will be set from Action.LONG_NAME, if available, and from Action.NAME otherwise.

New buttons, check boxes, and radio buttons are created using the factory methods newButton(), newCheckBox and newRadioButton, respectively, so that an application may override and create subclasses of those.

Parameters:
action - the action to create a task item for
Returns:
an instance of AbstractButton
See Also:
newButton(com.sas.swing.util.Action), newCheckBox(), newRadioButton()

newToolItem

protected javax.swing.AbstractButton newToolItem(com.sas.swing.util.Action action)
Creates a new tool item based on a given action.

If the action's type property, Action.TYPE, is set to Action.TYPE_BOOLEAN, then a check box is created instead of a regular button. If in addition to the type being boolean, the action's group name property, Action.GROUP_NAME, is set, then a radio button is created instead of a check box.

New buttons, check boxes, and radio buttons are created using the factory methods newButton(), newCheckBox and newRadioButton, respectively, so that an application may override and create subclasses of those.

Parameters:
action - the action to create a tool item for
Returns:
an instance of AbstractButton
See Also:
newButton(com.sas.swing.util.Action), newCheckBox(), newRadioButton()

newButton

protected javax.swing.AbstractButton newButton(com.sas.swing.util.Action action)
Creates a new button based on a given action.

If the action's type property, Action.TYPE, is set to Action.TYPE_BOOLEAN, then a toggle button is created instead of a regular button.

New buttons and toggle buttons are created using the factory methods newButton() and newToggleBox. respectively, so that an application may override and create subclasses of those.

Parameters:
action - the action to create a tool item for
Returns:
an instance of AbstractButton
See Also:
newButton(com.sas.swing.util.Action), newToggleButton()

newButton

protected javax.swing.AbstractButton newButton()
Creates a new button.

Returns:
an instance of javax.swing.JButton

newCheckBox

protected javax.swing.AbstractButton newCheckBox()
Creates a new check box.

Returns:
an instance of javax.swing.JCheckBox

newRadioButton

protected javax.swing.AbstractButton newRadioButton()
Creates a new radio button.

Returns:
an instance of javax.swing.JRadioButton

newToggleButton

protected javax.swing.AbstractButton newToggleButton()
Creates a new toggle button.

Returns:
an instance of javax.swing.JToggleButton

newShortcutItem

protected javax.swing.AbstractButton newShortcutItem(com.sas.swing.util.Action action)
Creates a new shortcut item based on a given action.

The item's text will be set from Action.LARGE_ICON, if available, and from Action.SMALL_ICON otherwise.

New buttons are created using the factory method newButton(), so that an application may override and create a button subclass.

Parameters:
action - the action to create a shortcut item for
Returns:
an instance of AbstractButton
See Also:
newButton(com.sas.swing.util.Action)

newShortcutItem

protected javax.swing.AbstractButton newShortcutItem(com.sas.swing.util.Action action,
                                                     boolean smallIcons)
Creates a new shortcut item based on a given action.

The item's text will be set from Action.LARGE_ICON, if available, and from Action.SMALL_ICON otherwise.

New buttons are created using the factory method newButton(), so that an application may override and create a button subclass.

Parameters:
action - the action to create a shortcut item for
Returns:
an instance of AbstractButton
See Also:
newButton(com.sas.swing.util.Action)

loadActionResources

public void loadActionResources(com.sas.swing.util.Action action)
Parameters:
action -



Copyright © 2009 SAS Institute Inc. All Rights Reserved.