|
JAPI 0.9.0 Yet another Java API API Documentation |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.japi.swing.ActionFactory
public final class ActionFactory
Class for creating and initializing Action
s 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:
ActionFactory myActionFactory = ActionFactory.getFactory("MyApplication"); myActionFactory.addBundle("com.mycompany.mypackage.myresource"); // not always required myActionFactory.addPref(MyClass.class);
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.
ActionFactory.getFactory("net.sf.japi.swing");
for the first time, it is tried to load a
ResourceBundle
named net.sf.japi.swing.actions
for that ActionFactory
.
This automatism has been implemented to save you from the need of initializing an ActionFactory before use.
createAction(boolean, String, Object)
determines several things:
What | Preferences / Bundle key | Action key if stored in an action |
---|---|---|
An (somewhat unique) ID | (basename itself) | ACTION_ID |
The icon | basename + ".icon" | Action.SMALL_ICON |
The tooltip help | basename + ".shortdescription" | Action.SHORT_DESCRIPTION |
The long help | basename + ".longdescription" | Action.LONG_DESCRIPTION |
The text label | basename + ".text" | Action.NAME |
The keyboard accelerator | basename + ".accel" | Action.ACCELERATOR_KEY |
The alternate keyboard accelerator | basename + ".accel2" | ACCELERATOR_KEY_2 |
The mnemonic | basename + ".mnemonic" | Action.MNEMONIC_KEY |
The method name | ReflectionAction.REFLECTION_METHOD_NAME | |
The method | ReflectionAction.REFLECTION_METHOD | |
The boolean property name | ToggleAction.REFLECTION_PROPERTY_NAME | |
The target instance | ReflectionAction.REFLECTION_TARGET | |
Exception handler dialogs | basename + ".exception." + exception class name + ... The message can be formatted with 1 parameter that will be the localized message of the thrown exception. | n/a |
Some methods are not related to actions, yet take base keys:
showMessageDialog(Component, String, Object...)
:
What | Preferences / Bundle key |
---|---|
Dialog title | basename + ".title" |
Dialog message | basename + ".message" |
Dialog messagetype | basename + ".messagetype" The message type should be one of the message types defined in JOptionPane , e.g. JOptionPane.PLAIN_MESSAGE . |
getFactory(String)
, just go ahead
and do so.
Looking up created ActionFactories is extremely fast, and of course they are initialized exactly once, not more.
AbstractAction
,
Action
,
Preferences
,
ResourceBundle
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 |
---|
@NotNull public static final String ACTION_ID
@NotNull public static final String ACCELERATOR_KEY_2
Method Detail |
---|
@NotNull public static ActionFactory getFactory(@Nullable String 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.
key
- name of ActionFactory (which even may be null
if you are too lazy to invent a key)
public void addBundle(@NotNull String baseName)
baseName
- the base name of the resource bundle, a fully qualified class nameResourceBundle.getBundle(String)
@Nullable public static JMenuItem find(@NotNull JMenuBar menuBar, @NotNull Action action)
menuBar
- JMenuBar to searchaction
- Action to find JMenuItem for
null
if none found
NullPointerException
- if action or menuBar is null
@Nullable public static JMenuItem find(@NotNull JMenu menu, @NotNull Action action)
menu
- JMenu to searchaction
- Action to find JMenuItem for
null
if none found
NullPointerException
- if menu or action is null
@NotNull public ActionMap getActionMap()
public void addBundle(@NotNull ResourceBundle bundle) throws NullPointerException
bundle
- ResourceBundle to add
NullPointerException
- if bundle == null
public void addParent(@NotNull ActionFactory parent) throws NullPointerException
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!
NullPointerException
- if parent == null
public void addPref(@NotNull Preferences pref) throws NullPointerException
pref
- Preferences to add
NullPointerException
- if pref == null
public void addPref(@NotNull Class<?> clazz)
clazz
- the class whose package a user preference node is desired
NullPointerException
- if clazz == null
Preferences.userNodeForPackage(Class)
public Action[] createActions(boolean store, @NotNull String... keys) throws NullPointerException
createAction(boolean,String)
.
The actions created can be retrieved using getAction(String)
or via the ActionMap returned by getActionMap()
.
store
- whether to store the initialized Action in the ActionMap of this ActionFactorykeys
- Keys of actions to create
NullPointerException
- in case keys is or contains null
public Action createAction(boolean store, @NotNull String key) throws NullPointerException
getActionMap()
.
store
- whether to store the initialized Action in the ActionMap of this ActionFactorykey
- Key for Action, which is used as basename for access to Preferences and ResourceBundles and as ActionMap key (may not be
null
)
ActionListener.actionPerformed(ActionEvent)
method does not do anything
NullPointerException
- in case key was null
createAction(boolean,String,Object)
,
createToggle(boolean,String,Object)
,
initAction(boolean,Action,String)
public Action initAction(boolean store, @NotNull Action action, @NotNull String key) throws NullPointerException
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()
.
store
- whether to store the initialized Action in the ActionMap of this ActionFactoryaction
- Action to fillkey
- Key for Action, which is used as basename for access to Preferences and ResourceBundles and as ActionMap key (may not be null
)
NullPointerException
- in case key was null
@Nullable public String getString(@NotNull String key) throws NullPointerException
key
- Key to get String for
null
if no value could be found
NullPointerException
- if key is null
@Nullable public String getStringFromPrefs(@NotNull String key) throws NullPointerException
key
- Key to get String for
null
if no value could be found
NullPointerException
- if key is null
@Nullable public String getStringFromBundles(@NotNull String key) throws NullPointerException
key
- Key to get String for
null
if no value could be found
NullPointerException
- if key is null
public Action[] createActions(boolean store, Object target, String... keys) throws NullPointerException
createAction(boolean,String,Object)
.
The actions created can be retrieved using getAction(String)
or via the ActionMap returned by getActionMap()
.
store
- whether to store the initialized Action in the ActionMap of this ActionFactorytarget
- Target objectkeys
- Keys of actions to create
NullPointerException
- in case keys is or contains null
public Action createAction(boolean store, String key, Object object) throws NullPointerException
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.
store
- whether to store the initialized Action in the ActionMap of this ActionFactorykey
- 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)
)
NullPointerException
- in case key was null
createAction(boolean,String)
,
createToggle(boolean,String,Object)
,
initAction(boolean,Action,String)
public JMenu createMenu(boolean store, String menuKey, String... keys) throws Error
store
- whether to store the initialized Action in the ActionMap of this ActionFactorymenuKey
- action key for Menukeys
- Action keys for menu items
Error
- in case action definitions for keys weren't foundpublic Action getAction(String key)
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)
.
key
- Key of action to get
getActionMap().get(key)
.public JMenuBar createMenuBar(boolean store, String barKey)
store
- whether to store the initialized Actions in the ActionMap of this ActionFactorybarKey
- Action key of menu to create
public JMenu createMenu(boolean store, String menuKey) throws Error
key + ".menu"
which lists the menu element's keys.
Submenus are build recursively.
store
- whether to store the initialized Action in the ActionMap of this ActionFactorymenuKey
- action key for menu
Error
- in case a menu definition for menuKey wasn't foundpublic JMenuBar createMenuBar(boolean store, String barKey, Object target)
store
- whether to store the initialized Actions in the ActionMap of this ActionFactorybarKey
- Action key of menu to createtarget
- Target object
public JMenu createMenu(boolean store, String menuKey, Object target) throws Error
key + ".menu"
which lists the menu element's keys.
Submenus are build recursively.
store
- whether to store the initialized Action in the ActionMap of this ActionFactorymenuKey
- action key for menutarget
- Target object
Error
- in case a menu definition for menuKey wasn't foundpublic void createToggles(boolean store, Object target, String... keys) throws NullPointerException
createToggle(boolean,String,Object)
.
The actions created can be retrieved using getAction(String)
or via the ActionMap returned by getActionMap()
.
store
- whether to store the initialized Action in the ActionMap of this ActionFactorytarget
- Target objectkeys
- Keys of actions to create
NullPointerException
- in case keys was or contained null
public Action createToggle(boolean store, String key, Object object) throws NullPointerException
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.
store
- whether to store the initialized Action in the ActionMap of this ActionFactorykey
- 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)
)
NullPointerException
- in case key was null
createAction(boolean,String)
,
createAction(boolean,String,Object)
,
initAction(boolean,Action,String)
public JToolBar createToolBar(String... keys)
keys
- Action keys for toolbar entries
public JToolBar createToolBar(String barKey)
barKey
- Action keys of toolbar to create
public JToolBar createToolBar(Object object, String... keys)
object
- Instance to invoke method on if the Action was activated (ActionListener.actionPerformed(ActionEvent)
)keys
- Action keys for toolbar entries
public JToolBar createToolBar(Object object, String barKey)
object
- Instance to invoke method on if the Action was activated (ActionListener.actionPerformed(ActionEvent)
)barKey
- Action keys of toolbar to create
public JMenuItem find(JMenuBar menuBar, String key)
menuBar
- JMenuBar to searchkey
- Key to find JMenuItem for
null
if none foundpublic Icon getIcon(String key)
key
- i18n key for icon
@Deprecated public void showMessageDialog(Component parentComponent, int messageType, String key, Object... args)
showMessageDialog(Component, String, Object...)
instead and put the messagetype in the action.properties file.
parentComponent
- determines the Frame in which the dialog is displayed; if null
, or if the parentComponent
has
no Frame
, a default Frame
is usedmessageType
- the type of message to be displayedkey
- localization key to use for the title, the message and eventually the iconargs
- formatting arguments for the message textpublic void showMessageDialog(Component parentComponent, String key, Object... args)
parentComponent
- determines the Frame in which the dialog is displayed; if null
, or if the parentComponent
has
no Frame
, a default Frame
is usedkey
- localization key to use for the title, the message and eventually the iconargs
- formatting arguments for the message textpublic int getMessageType(String dialogKey)
dialogKey
- dialog key
public String format(String key, Object... args)
MessageFormat
.
key
- message keyargs
- parameters
MessageFormat.format(String,Object...)
public int showOnetimeConfirmDialog(Component parentComponent, int optionType, int messageType, String key, Object... args) throws IllegalStateException
parentComponent
- determines the Frame in which the dialog is displayed; if null
, or if the parentComponent
has
no Frame
, a default Frame
is usedoptionType
- the option typemessageType
- the type of message to be displayedkey
- localization key to use for the title, the message and eventually the iconargs
- formatting arguments for the message text
IllegalStateException
- if no preferences are associatedpublic void showOnetimeMessageDialog(Component parentComponent, int messageType, String key, Object... args) throws IllegalStateException
parentComponent
- determines the Frame in which the dialog is displayed; if null
, or if the parentComponent
has
no Frame
, a default Frame
is usedmessageType
- the type of message to be displayedkey
- localization key to use for the title, the message and eventually the iconargs
- formatting arguments for the message text
IllegalStateException
- if no preferences are associatedpublic boolean showQuestionDialog(Component parentComponent, String key, Object... args)
parentComponent
- determines the Frame in which the dialog is displayed; if null
, or if the parentComponent
has
no Frame
, a default Frame
is usedkey
- localization key to use for the title, the message and eventually the iconargs
- formatting arguments for the message text
true
if user confirmed, otherwise false
public int showConfirmDialog(Component parentComponent, int optionType, int messageType, String key, Object... args)
parentComponent
- determines the Frame in which the dialog is displayed; if null
, or if the parentComponent
has
no Frame
, a default Frame
is usedoptionType
- the option typemessageType
- the type of message to be displayedkey
- localization key to use for the title, the message and eventually the iconargs
- formatting arguments for the message text
public JLabel createLabel(String key, Object... args)
key
- Key to create label forargs
- formatting arguments for the label text
public JLabel createLabel(Component component, String key, Object... args)
component
- Component to associate label to (maybe null
)key
- Key to create label forargs
- formatting arguments for the label text
public void addActionProvider(ActionProvider actionProvider)
actionProvider
- ActionProvider to register
|
JAPI Yet another Java API API Documentation |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |