Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.crosswire.common.swing
Class ActionFactory

    java.lang.Object
      org.crosswire.common.swing.ActionFactory

All Implemented Interfaces:
  ActionListener, EventListener, Actionable

    public class ActionFactory

extends Object
implements ActionListener, Actionable

The ActionFactory is responsible for creating CWActions and making them available to the program. Each Action is constructed from resources of the form: ActionName.field=value where ActionName is the ACTION_COMMAND_KEY value and field is one of the CWAction constants, e.g. LargeIcon.
Field is one of:

- Name - This is required. The value is used for the text of the Action.
- Mnemonic - An upper case letter or other character in the value of the Name field. If found, using a case insensitive search, the mnemonic will cause the corresponding character to be underlined. In a platform dependent way it provides a keyboard mechanism to fire the action. For example, on Windows, alt + mnemonic will cause a visible, active element with that mnemonic to fire. For this reason, it is important to ensure that two visible, active elements do not have the same mnemonic.
  Note: Mnemonics are suppressed on MacOSX.
- ToolTip - A tip to show when the mouse is over an element. If not present, Name is used. This is likely to change. It is redundant to show a tooltip that is identical to the shown text.
- SmallIcon - A 16x16 pixel image to be shown for the item. The value for this is a path which can be found as a resource.
  Note: the small icon will be used when actions are tied to menu items and buttons.
- LargeIcon - A 24x24 pixel image to be shown for the item when large items are shown. Currently, these are only used for the ToolBar, when a large toolbar is requested. The value is a resource path to the image.
- AcceleratorKey - A key on the keyboard, which may be specified with 0x25 kind of notation.
  Accelerators are global key combinations that work within an application to fire the action. When the action is shown as a menu item the accelerator will be listed with the name. Note: The accelerator key and it's modifiers are converted into a `KeyStroke` with `KeyStroke.getKeyStroke(key, modifierMask);`
- AcceleratorKey.Modifier - A comma separated list of ctrl, alt, and shift, indicating what modifiers are necessary for the accelerator.
  Note: ctrl will use a platform's command key. On MacOSX this is the Apple/Command key. Other platforms use Ctrl.
- Enabled - Defaults to true when not present. It is disabled when the value does not match "true" regardless of case. This is used to initialize widgets tied to actions to disabled. Once the action is created, it's state can be changed and the tied widgets will behave appropriately.
- Shared - Defaults to true when not present. It is unshared when the value does not match "true" regardless of case. When false, each copy of the action is independent of other copies.

In order to facilitate easier translation, Enabled, SmallIcon and LargeIcon can be specified in a parallel resource, whose name is suffixed with "_control" as in Desktop_control. This is meant to extrapolate the constant behavior of an action into a file that probably does not need to be internationalized. If it does, for example, to suppress the display of icons, then one would create a resource further suffixed with the language and perhaps country, as in Desktop_control_fa.

To add another twist, several actions may have the same name and mnemonic, differing perhaps by tooltip. To facilitate the sharing of these definitions, an Aliases resource is defined to contain common values. If the value of a ActionName.Name is prefixed with "Alias.", as in Go.Name=Alias.Go, then Go will be used as the ActionName to look up values in the Aliases resource.

Aliases defines defaults that can be overridden by the referring resource file. The only value that cannot be overridden is Name.

When an action is fired, this class, as a listener, reflects the action on the class providing the resource. For example, DesktopActions creates an ActionFactory from the Desktop ResourceBundle. When the Exit action is fired, ActionFactory calls DesktopActions.doExit(ActionEvent event) or DesktopActions.doExit(), if the first did not exist.

Author:
  DM Smith [dmsmith555 at yahoo dot com], Joe Walker [joe at eireneh dot com]
See Also:
  `for license details. The copyright to this program is held by it's authors.`

Field Summary
`private Map` | `actions` The map of known CWActions
`private static String` | `ALIAS`
`private static ResourceBundle` | `aliases` The aliases known by this system.
`private static String` | `ALIASES`
`private Object` | `bean` The object to which we forward events
`private static Logger` | `log` The log stream
`private static String` | `METHOD_PREFIX` The prefix to methods that we call
`private static String` | `MISSING_RESOURCE` The tooltip for actions that we generate to paper around missing resources Normally we would assert, but in live we might want to limp on.
`private static String` | `SEPARATOR` What we lookup
`private static String` | `TEST` The test string to find actions

Constructor Summary
`ActionFactory(Class type, Object bean)` Constructor that distinguishes between the object to call and the type to look up resources against.

Method Summary
`void` | `actionPerformed(ActionEvent ev)`
`void` | `actionPerformed(String action)`
`private void` | `buildActionMap(Class basis)` Build the map of actions from resources
`JButton` | `createActionIcon(String key)` Build a button from an action that consist solely of the icon.
`JButton` | `createActionIcon(String key, ActionListener listener)` Build a button from an action that consist solely of the icon.
`JButton` | `createJButton(String key)` Build a button from an action.
`JButton` | `createJButton(String key, ActionListener listener)` Build a button from an action.
`JLabel` | `createJLabel(String key)` Construct a JLabel from the Action.
`private KeyStroke` | `getAccelerator(ResourceBundle nicknames, ResourceBundle resources, String actionName)` Convert the string to a valid Accelerator (ie a KeyStroke)
`Action` | `getAction(String key)` Get the Action for the given actionName.
`Action` | `getAction(String key, ActionListener listener)` Get the Action for the given actionName.
`private String` | `getActionString(ResourceBundle resources, String actionName, String field)` Lookup an action/field combination, warning about missing resources rather than excepting.
`private Icon` | `getIcon(ResourceBundle resources, String actionName, String iconName)` Get an icon for the string
`private int` | `getInteger(String str)` Convert the string to an integer.
`private Integer` | `getMnemonic(ResourceBundle nicknames, ResourceBundle resources, String actionName)` Convert the string to a mnemonic
`private int` | `getModifier(String[] modifiers)`
`private String` | `getOptionalActionString(ResourceBundle nicknames, ResourceBundle resources, String actionName, String field)` Lookup an action/field combination, returning null for missing resources.
`private String` | `getOptionalActionString(ResourceBundle resources, String actionName, String field)` Lookup an action/field combination, returning null for missing resources.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### MISSING_RESOURCE

    private static final String MISSING_RESOURCE

  The tooltip for actions that we generate to paper around missing resources Normally we would assert, but in live we might want to limp on.

  See Also:
    Constant Field Values

### METHOD_PREFIX

    private static final String METHOD_PREFIX

  The prefix to methods that we call

  See Also:
    Constant Field Values

### SEPARATOR

    private static final String SEPARATOR

  What we lookup

  See Also:
    Constant Field Values

### TEST

    private static final String TEST

  The test string to find actions

  See Also:
    Constant Field Values

### bean

    private Object bean

  The object to which we forward events

### ALIASES

    private static final String ALIASES

See Also:
  Constant Field Values

### ALIAS

    private static final String ALIAS

See Also:
  Constant Field Values

### log

    private static final Logger log

  The log stream

### aliases

    private static ResourceBundle aliases

  The aliases known by this system.

### actions

    private Map actions

  The map of known CWActions

Constructor Detail

### ActionFactory

    public ActionFactory(Class type,
                         Object bean)

  Constructor that distinguishes between the object to call and the type to look up resources against. This is useful for when you are writing a class with subclasses but wish to keep the resources registered in the name of the superclass.

Method Detail

### actionPerformed

    public void actionPerformed(String action)

  Specified by:
    `actionPerformed` in interface `Actionable`

### actionPerformed

    public void actionPerformed(ActionEvent ev)

  Specified by:
    `actionPerformed` in interface `ActionListener`

### getAction

    public Action getAction(String key)

  Get the Action for the given actionName.

  Parameters:
    `key` - the internal name of the CWAction
  Returns:
    CWAction null if it does not exist

### getAction

    public Action getAction(String key,
                            ActionListener listener)

  Get the Action for the given actionName.

  Parameters:
    `key` - the internal name of the CWAction
  Returns:
    CWAction null if it does not exist

### createJLabel

    public JLabel createJLabel(String key)

  Construct a JLabel from the Action. Only Action.NAME and Action.MNEMONIC_KEY are used.

  Parameters:
    `key` - the internal name of the CWAction
  Returns:
    A label, asserting if missing resources or with default values otherwise

### createActionIcon

    public JButton createActionIcon(String key)

  Build a button from an action that consist solely of the icon.

  Parameters:
    `key` - the action to use
  Returns:
    the button

### createActionIcon

    public JButton createActionIcon(String key,
                                    ActionListener listener)

  Build a button from an action that consist solely of the icon.

  Parameters:
    `key` - the action to use
  Returns:
    the button

### createJButton

    public JButton createJButton(String key)

  Build a button from an action.

  Parameters:
    `key` - the action to use
  Returns:
    the button

### createJButton

    public JButton createJButton(String key,
                                 ActionListener listener)

  Build a button from an action.

  Parameters:
    `key` - the action to use
  Returns:
    the button

### buildActionMap

    private void buildActionMap(Class basis)

  Build the map of actions from resources

### getActionString

    private String getActionString(ResourceBundle resources,
                                   String actionName,
                                   String field)

  Lookup an action/field combination, warning about missing resources rather than excepting.

### getOptionalActionString

    private String getOptionalActionString(ResourceBundle resources,
                                           String actionName,
                                           String field)

  Lookup an action/field combination, returning null for missing resources.

### getOptionalActionString

    private String getOptionalActionString(ResourceBundle nicknames,
                                           ResourceBundle resources,
                                           String actionName,
                                           String field)

  Lookup an action/field combination, returning null for missing resources. If aliases are present use them, with values in resources over-riding.

### getIcon

    private Icon getIcon(ResourceBundle resources,
                         String actionName,
                         String iconName)

  Get an icon for the string

### getMnemonic

    private Integer getMnemonic(ResourceBundle nicknames,
                                ResourceBundle resources,
                                String actionName)

  Convert the string to a mnemonic

### getAccelerator

    private KeyStroke getAccelerator(ResourceBundle nicknames,
                                     ResourceBundle resources,
                                     String actionName)

  Convert the string to a valid Accelerator (ie a KeyStroke)

### getInteger

    private int getInteger(String str)
                    throws NumberFormatException

  Convert the string to an integer. The string is either a single character or it is hex number.

  Returns:
    the integer value of the accelerator
  Throws:
    `NumberFormatException`

### getModifier

    private int getModifier(String[] modifiers)

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Copyright ¨ 2003-2007
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD