JAPI 0.9.0
Yet another Java API
API Documentation

net.sf.japi.swing
Class ActionFactory

java.lang.Object
  extended by net.sf.japi.swing.ActionFactory

public final class ActionFactory
extends Object

Class for creating and initializing Actions that are localized, user configurable and may invoke their final methods using Reflection; also handles extremely much useful stuff for i18n/l10n. It is meant as a general service for creating Action objects that have some comfort for the programmer in several aspects:

You may choose to use one or more ActionFactories, depending on the size of your application. You may use to spread Action configuration information accross one or more ResourceBundles and one or more Preferences, as you wish. When looking for values, the Preferences are queried first, in addition order, after that the ResourceBundles, again in addition order, until a value was found. The behaviour when no value was found is undefined.

Usage

The recommended usage is to
  1. create and initialize an ActionFactory similar as the following example code, put it somewhere at the start of your program:
     ActionFactory myActionFactory = ActionFactory.getFactory("MyApplication");
     myActionFactory.addBundle("com.mycompany.mypackage.myresource"); // not always required
     myActionFactory.addPref(MyClass.class);
     
  2. then use the ActionFactory from anywhere within the application like this:
     ActionFactory myActionFactory = ActionFactory.getFactory("MyApplication");
     Action myAction = myActionFactory.createAction("load", this);
     

All actions created or initialized by an instance of this class are optionally put into that instance's ActionMap. If they are stored, you can use that map for later retrieval.

Usage Notes: Factory Name

Usage Notes: Action Key / Action Name

The key you supply as first argument of createAction(boolean, String, Object) determines several things:

Some methods are not related to actions, yet take base keys:

Final Notes

Author:
Christian Hujer
See Also:
AbstractAction, Action, Preferences, ResourceBundle
Todo:
think about toolbar interaction, think about toolbar configuration, eventually rename this ActionBuilder and provide an ActionBuilderFactory.

Field Summary
static String ACCELERATOR_KEY_2
          The key used for storing an alternative accelerator for this Action.
static String ACTION_ID
          The key used for storing a somewhat unique id for this Action.
 
Method Summary
 void addActionProvider(ActionProvider actionProvider)
          Registers an ActionProvider with this ActionFactory.
 void addBundle(ResourceBundle bundle)
          Add a ResourceBundle to the list of used bundles.
 void addBundle(String baseName)
          Add a ResourceBundle to the list of used bundles.
 void addParent(ActionFactory parent)
          Add a parent to the list of used parents.
 void addPref(Class<?> clazz)
          Add a Preferences to the list of used preferences.
 void addPref(Preferences pref)
          Add a Preferences to the list of used preferences.
 Action createAction(boolean store, String key)
          Create an Action.
 Action createAction(boolean store, String key, Object object)
          Create an Action.
 Action[] createActions(boolean store, Object target, String... keys)
          Creates actions.
 Action[] createActions(boolean store, String... keys)
          Creates actions.
 JLabel createLabel(Component component, String key, Object... args)
          Creates a label for a specified key and component.
 JLabel createLabel(String key, Object... args)
          Creates a label for a specified key.
 JMenu createMenu(boolean store, String menuKey)
          Method for creating a Menu.
 JMenu createMenu(boolean store, String menuKey, Object target)
          Method for creating a Menu.
 JMenu createMenu(boolean store, String menuKey, String... keys)
          Method for creating a Menu.
 JMenuBar createMenuBar(boolean store, String barKey)
          Method for creating a menubar.
 JMenuBar createMenuBar(boolean store, String barKey, Object target)
          Method for creating a menubar.
 Action createToggle(boolean store, String key, Object object)
          Create an Action.
 void createToggles(boolean store, Object target, String... keys)
          Creates actions.
 JToolBar createToolBar(Object object, String... keys)
          Method for creating a toolbar.
 JToolBar createToolBar(Object object, String barKey)
          Method for creating a toolbar.
 JToolBar createToolBar(String... keys)
          Method for creating a toolbar.
 JToolBar createToolBar(String barKey)
          Method for creating a toolbar.
static JMenuItem find(JMenu menu, Action action)
          Method to find the JMenuItem for a specific Action.
static JMenuItem find(JMenuBar menuBar, Action action)
          Method to find the JMenuItem for a specific Action.
 JMenuItem find(JMenuBar menuBar, String key)
          Method to find the JMenuItem for a specific Action key.
 String format(String key, Object... args)
          Formats a message with parameters.
 Action getAction(String key)
          Get an Action.
 ActionMap getActionMap()
          Get the ActionMap.
static ActionFactory getFactory(String key)
          Get an ActionFactory.
 Icon getIcon(String key)
          Get an icon.
 int getMessageType(String dialogKey)
          Get the message type for a dialog.
 String getString(String key)
          Get a String.
 String getStringFromBundles(String key)
          Get a String from the resource bundles, ignoring the preferences.
 String getStringFromPrefs(String key)
          Get a String from the preferences, ignoring the resource bundles.
 Action initAction(boolean store, Action action, String key)
          Initialize an Action.
 int showConfirmDialog(Component parentComponent, int optionType, int messageType, String key, Object... args)
          Show a localized confirmation dialog.
 void showMessageDialog(Component parentComponent, int messageType, String key, Object... args)
          Deprecated. use showMessageDialog(Component, String, Object...) instead and put the messagetype in the action.properties file.
 void showMessageDialog(Component parentComponent, String key, Object... args)
          Show a localized message dialog.
 int showOnetimeConfirmDialog(Component parentComponent, int optionType, int messageType, String key, Object... args)
          Show a localized confirmation dialog which the user can suppress in future (remembering his choice).
 void showOnetimeMessageDialog(Component parentComponent, int messageType, String key, Object... args)
          Show a localized message dialog which the user can suppress in future.
 boolean showQuestionDialog(Component parentComponent, String key, Object... args)
          Show a localized question dialog.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ACTION_ID

@NotNull
public static final String ACTION_ID
The key used for storing a somewhat unique id for this Action.

See Also:
Constant Field Values

ACCELERATOR_KEY_2

@NotNull
public static final String ACCELERATOR_KEY_2
The key used for storing an alternative accelerator for this Action. Currently unused.

See Also:
Constant Field Values
Method Detail

getFactory

@NotNull
public static ActionFactory getFactory(@Nullable
                                               String key)
Get an ActionFactory. If there is no ActionFactory with name key, a new ActionFactory is created and stored. Future invocations of this method will constantly return that ActionFactory unless the key is garbage collected. If you must prevent the key from being garbage collected (and with it the ActionFactory), you may internalize the key (String.intern()). A good name for a key is the application or package name. The key may be a package name, in which case it is tried to load a ResourceBundle named "action" from that package and add it (addBundle(ResourceBundle)); nothing special happens if that fails.

Parameters:
key - name of ActionFactory (which even may be null if you are too lazy to invent a key)
Returns:
ActionFactory for given key. The factory is created in case it didn't already exist.

addBundle

public void addBundle(@NotNull
                      String baseName)
Add a ResourceBundle to the list of used bundles.

Parameters:
baseName - the base name of the resource bundle, a fully qualified class name
See Also:
ResourceBundle.getBundle(String)

find

@Nullable
public static JMenuItem find(@NotNull
                                      JMenuBar menuBar,
                                      @NotNull
                                      Action action)
Method to find the JMenuItem for a specific Action.

Parameters:
menuBar - JMenuBar to search
action - Action to find JMenuItem for
Returns:
JMenuItem for action or null if none found
Throws:
NullPointerException - if action or menuBar is null

find

@Nullable
public static JMenuItem find(@NotNull
                                      JMenu menu,
                                      @NotNull
                                      Action action)
Method to find the JMenuItem for a specific Action.

Parameters:
menu - JMenu to search
action - Action to find JMenuItem for
Returns:
JMenuItem for action or null if none found
Throws:
NullPointerException - if menu or action is null

getActionMap

@NotNull
public ActionMap getActionMap()
Get the ActionMap.

Returns:
ActionMap

addBundle

public void addBundle(@NotNull
                      ResourceBundle bundle)
               throws NullPointerException
Add a ResourceBundle to the list of used bundles.

Parameters:
bundle - ResourceBundle to add
Throws:
NullPointerException - if bundle == null

addParent

public void addParent(@NotNull
                      ActionFactory parent)
               throws NullPointerException
Add a parent to the list of used parents.

Parameters:
parent - Parent to use if lookups failed in this ActionFactory WARNING: Adding a descendents as parents of ancestors or vice versa will result in endless recursion and thus stack overflow!
Throws:
NullPointerException - if parent == null

addPref

public void addPref(@NotNull
                    Preferences pref)
             throws NullPointerException
Add a Preferences to the list of used preferences.

Parameters:
pref - Preferences to add
Throws:
NullPointerException - if pref == null

addPref

public void addPref(@NotNull
                    Class<?> clazz)
Add a Preferences to the list of used preferences.

Parameters:
clazz - the class whose package a user preference node is desired
Throws:
NullPointerException - if clazz == null
See Also:
Preferences.userNodeForPackage(Class)

createActions

public Action[] createActions(boolean store,
                              @NotNull
                              String... keys)
                       throws NullPointerException
Creates actions. This is a loop variant of createAction(boolean,String). The actions created can be retrieved using getAction(String) or via the ActionMap returned by getActionMap().

Parameters:
store - whether to store the initialized Action in the ActionMap of this ActionFactory
keys - Keys of actions to create
Returns:
Array with created actions
Throws:
NullPointerException - in case keys is or contains null

createAction

public Action createAction(boolean store,
                           @NotNull
                           String key)
                    throws NullPointerException
Create an Action. The created Action is automatically stored together with all other Actions created by this Factory instance in an ActionMap, which you can retreive using getActionMap().

Parameters:
store - whether to store the initialized Action in the ActionMap of this ActionFactory
key - Key for Action, which is used as basename for access to Preferences and ResourceBundles and as ActionMap key (may not be null)
Returns:
created Action, which is a dummy in the sense that its ActionListener.actionPerformed(ActionEvent) method does not do anything
Throws:
NullPointerException - in case key was null
See Also:
createAction(boolean,String,Object), createToggle(boolean,String,Object), initAction(boolean,Action,String)

initAction

public Action initAction(boolean store,
                         @NotNull
                         Action action,
                         @NotNull
                         String key)
                  throws NullPointerException
Initialize an Action. This is a convenience method for Action users which want to use the services provided by this ActionFactory class but need more sophisticated Action objects they created on their own. So you can simply create an Action and pass it to this Initialization method to fill its data. The initialized Action is automatically stored together with all other Actions created by this Factory instance in an ActionMap, which you can retreive using getActionMap().

Parameters:
store - whether to store the initialized Action in the ActionMap of this ActionFactory
action - Action to fill
key - Key for Action, which is used as basename for access to Preferences and ResourceBundles and as ActionMap key (may not be null)
Returns:
the supplied Action object (action) is returned for convenience
Throws:
NullPointerException - in case key was null

getString

@Nullable
public String getString(@NotNull
                                 String key)
                 throws NullPointerException
Get a String. First looks one pref after another, in their addition order. Then looks one bundle after another, in their addition order.

Parameters:
key - Key to get String for
Returns:
the first value found or null if no value could be found
Throws:
NullPointerException - if key is null

getStringFromPrefs

@Nullable
public String getStringFromPrefs(@NotNull
                                          String key)
                          throws NullPointerException
Get a String from the preferences, ignoring the resource bundles.

Parameters:
key - Key to get String for
Returns:
the first value found or null if no value could be found
Throws:
NullPointerException - if key is null

getStringFromBundles

@Nullable
public String getStringFromBundles(@NotNull
                                            String key)
                            throws NullPointerException
Get a String from the resource bundles, ignoring the preferences.

Parameters:
key - Key to get String for
Returns:
the first value found or null if no value could be found
Throws:
NullPointerException - if key is null

createActions

public Action[] createActions(boolean store,
                              Object target,
                              String... keys)
                       throws NullPointerException
Creates actions. This is a loop variant of createAction(boolean,String,Object). The actions created can be retrieved using getAction(String) or via the ActionMap returned by getActionMap().

Parameters:
store - whether to store the initialized Action in the ActionMap of this ActionFactory
target - Target object
keys - Keys of actions to create
Returns:
Array with created actions
Throws:
NullPointerException - in case keys is or contains null

createAction

public Action createAction(boolean store,
                           String key,
                           Object object)
                    throws NullPointerException
Create an Action. The created Action is automatically stored together with all other Actions created by this Factory instance in an ActionMap, which you can retreive using getActionMap(). The supplied object needs to have a zero argument method named key. You may pass null as object, which means that the object the method is invoked on is not defined yet. You may safely use the Action, it will not throw any Exceptions upon ActionListener.actionPerformed(ActionEvent) but simply silently do nothing. The desired object can be set later using action.putValue(ReflectionAction.REFLECTION_TARGET, object).

Users of this method can assume that the returned object behaves quite like ReflectionAction. Wether or not this method returns an instance of ReflectionAction should not be relied on.

Parameters:
store - whether to store the initialized Action in the ActionMap of this ActionFactory
key - Key for Action, which is used as basename for access to Preferences and ResourceBundles, as ActionMap key and as Reflection Method name within the supplied object (may not be null)
object - Instance to invoke method on if the Action was activated (ActionListener.actionPerformed(ActionEvent))
Returns:
created Action
Throws:
NullPointerException - in case key was null
See Also:
createAction(boolean,String), createToggle(boolean,String,Object), initAction(boolean,Action,String)

createMenu

public JMenu createMenu(boolean store,
                        String menuKey,
                        String... keys)
                 throws Error
Method for creating a Menu.

Parameters:
store - whether to store the initialized Action in the ActionMap of this ActionFactory
menuKey - action key for Menu
keys - Action keys for menu items
Returns:
menu created from the menu definition found
Throws:
Error - in case action definitions for keys weren't found

getAction

public Action getAction(String key)
Get an Action. For an action to be retrieved with this method, it must have been initialized with initAction(boolean,Action,String), either directly by invoking initAction(boolean,Action,String) or indirectly by invoking one of this class' creation methods like createAction(boolean,String), createAction(boolean,String,Object) or createToggle(boolean,String,Object).

Parameters:
key - Key of action to get
Returns:
Action for key This method does the same as getActionMap().get(key).

createMenuBar

public JMenuBar createMenuBar(boolean store,
                              String barKey)
Method for creating a menubar.

Parameters:
store - whether to store the initialized Actions in the ActionMap of this ActionFactory
barKey - Action key of menu to create
Returns:
JMenuBar created for barKey

createMenu

public JMenu createMenu(boolean store,
                        String menuKey)
                 throws Error
Method for creating a Menu. This method assumes that the underlying properties contain an entry like key + ".menu" which lists the menu element's keys. Submenus are build recursively.

Parameters:
store - whether to store the initialized Action in the ActionMap of this ActionFactory
menuKey - action key for menu
Returns:
menu created from the menu definition found
Throws:
Error - in case a menu definition for menuKey wasn't found

createMenuBar

public JMenuBar createMenuBar(boolean store,
                              String barKey,
                              Object target)
Method for creating a menubar.

Parameters:
store - whether to store the initialized Actions in the ActionMap of this ActionFactory
barKey - Action key of menu to create
target - Target object
Returns:
JMenuBar created for barKey

createMenu

public JMenu createMenu(boolean store,
                        String menuKey,
                        Object target)
                 throws Error
Method for creating a Menu. This method assumes that the underlying properties contain an entry like key + ".menu" which lists the menu element's keys. Submenus are build recursively.

Parameters:
store - whether to store the initialized Action in the ActionMap of this ActionFactory
menuKey - action key for menu
target - Target object
Returns:
menu created from the menu definition found
Throws:
Error - in case a menu definition for menuKey wasn't found

createToggles

public void createToggles(boolean store,
                          Object target,
                          String... keys)
                   throws NullPointerException
Creates actions. This is a loop variant of createToggle(boolean,String,Object). The actions created can be retrieved using getAction(String) or via the ActionMap returned by getActionMap().

Parameters:
store - whether to store the initialized Action in the ActionMap of this ActionFactory
target - Target object
keys - Keys of actions to create
Throws:
NullPointerException - in case keys was or contained null

createToggle

public Action createToggle(boolean store,
                           String key,
                           Object object)
                    throws NullPointerException
Create an Action. The created Action is automatically stored together with all other Actions created by this Factory instance in an ActionMap, which you can retreive using getActionMap(). The supplied object needs to have a boolean return void argument getter method and a void return boolean argument setter method matching the key. You may pass null as object, which means that the object the getters and setters are invoked on is not defined yet. You may safely use the Action, it will not throw any Exceptions upon ActionListener.actionPerformed(ActionEvent) but simply silently do nothing. The desired object can be set later using action.putValue(ToggleAction.REFLECTION_TARGET, object).

Users of this method can assume that the returned object behaves quite like ToggleAction. Wether or not this method returns an instance of ToggleAction shuold not be relied on.

Parameters:
store - whether to store the initialized Action in the ActionMap of this ActionFactory
key - Key for Action, which is used as basename for access to Preferences and ResourceBundles, as ActionMap key and as Property name within the supplied object (may not be null)
object - Instance to invoke method on if the Action was activated (ActionListener.actionPerformed(ActionEvent))
Returns:
ToggleAction
Throws:
NullPointerException - in case key was null
See Also:
createAction(boolean,String), createAction(boolean,String,Object), initAction(boolean,Action,String)

createToolBar

public JToolBar createToolBar(String... keys)
Method for creating a toolbar.

Parameters:
keys - Action keys for toolbar entries
Returns:
JToolBar created for keys

createToolBar

public JToolBar createToolBar(String barKey)
Method for creating a toolbar.

Parameters:
barKey - Action keys of toolbar to create
Returns:
JToolBar created for barKey

createToolBar

public JToolBar createToolBar(Object object,
                              String... keys)
Method for creating a toolbar.

Parameters:
object - Instance to invoke method on if the Action was activated (ActionListener.actionPerformed(ActionEvent))
keys - Action keys for toolbar entries
Returns:
JToolBar created for keys

createToolBar

public JToolBar createToolBar(Object object,
                              String barKey)
Method for creating a toolbar.

Parameters:
object - Instance to invoke method on if the Action was activated (ActionListener.actionPerformed(ActionEvent))
barKey - Action keys of toolbar to create
Returns:
JToolBar created for barKey

find

public JMenuItem find(JMenuBar menuBar,
                      String key)
Method to find the JMenuItem for a specific Action key.

Parameters:
menuBar - JMenuBar to search
key - Key to find JMenuItem for
Returns:
JMenuItem for key or null if none found

getIcon

public Icon getIcon(String key)
Get an icon.

Parameters:
key - i18n key for icon
Returns:
icon

showMessageDialog

@Deprecated
public void showMessageDialog(Component parentComponent,
                                         int messageType,
                                         String key,
                                         Object... args)
Deprecated. use showMessageDialog(Component, String, Object...) instead and put the messagetype in the action.properties file.

Show a localized message dialog.

Parameters:
parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used
messageType - the type of message to be displayed
key - localization key to use for the title, the message and eventually the icon
args - formatting arguments for the message text

showMessageDialog

public void showMessageDialog(Component parentComponent,
                              String key,
                              Object... args)
Show a localized message dialog.

Parameters:
parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used
key - localization key to use for the title, the message and eventually the icon
args - formatting arguments for the message text

getMessageType

public int getMessageType(String dialogKey)
Get the message type for a dialog.

Parameters:
dialogKey - dialog key
Returns:
message type

format

public String format(String key,
                     Object... args)
Formats a message with parameters. It's a proxy method for using MessageFormat.

Parameters:
key - message key
args - parameters
Returns:
formatted String
See Also:
MessageFormat.format(String,Object...)

showOnetimeConfirmDialog

public int showOnetimeConfirmDialog(Component parentComponent,
                                    int optionType,
                                    int messageType,
                                    String key,
                                    Object... args)
                             throws IllegalStateException
Show a localized confirmation dialog which the user can suppress in future (remembering his choice).

Parameters:
parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used
optionType - the option type
messageType - the type of message to be displayed
key - localization key to use for the title, the message and eventually the icon
args - formatting arguments for the message text
Returns:
an integer indicating the option selected by the user now or eventually in a previous choice
Throws:
IllegalStateException - if no preferences are associated

showOnetimeMessageDialog

public void showOnetimeMessageDialog(Component parentComponent,
                                     int messageType,
                                     String key,
                                     Object... args)
                              throws IllegalStateException
Show a localized message dialog which the user can suppress in future.

Parameters:
parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used
messageType - the type of message to be displayed
key - localization key to use for the title, the message and eventually the icon
args - formatting arguments for the message text
Throws:
IllegalStateException - if no preferences are associated

showQuestionDialog

public boolean showQuestionDialog(Component parentComponent,
                                  String key,
                                  Object... args)
Show a localized question dialog.

Parameters:
parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used
key - localization key to use for the title, the message and eventually the icon
args - formatting arguments for the message text
Returns:
true if user confirmed, otherwise false

showConfirmDialog

public int showConfirmDialog(Component parentComponent,
                             int optionType,
                             int messageType,
                             String key,
                             Object... args)
Show a localized confirmation dialog.

Parameters:
parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used
optionType - the option type
messageType - the type of message to be displayed
key - localization key to use for the title, the message and eventually the icon
args - formatting arguments for the message text
Returns:
an integer indicating the option selected by the user

createLabel

public JLabel createLabel(String key,
                          Object... args)
Creates a label for a specified key.

Parameters:
key - Key to create label for
args - formatting arguments for the label text
Returns:
JLabel for key Key
Notes:
the label text will be the key if no String for the key was found

createLabel

public JLabel createLabel(Component component,
                          String key,
                          Object... args)
Creates a label for a specified key and component.

Parameters:
component - Component to associate label to (maybe null)
key - Key to create label for
args - formatting arguments for the label text
Returns:
JLabel for key Key
Notes:
the label text will be the key if no String for the key was found
Todo:
support icons, support mnemonics, alignments and textpositions

addActionProvider

public void addActionProvider(ActionProvider actionProvider)
Registers an ActionProvider with this ActionFactory.

Parameters:
actionProvider - ActionProvider to register

JAPI
Yet another Java API
API Documentation

© 2005-2006 Christian Hujer. All rights reserved. See copyright