public class Actions extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
Actions.ButtonActionConnector
SPI for supplying alternative implementation of connection between actions and presenters.
|
static class |
Actions.CheckboxMenuItem
CheckboxMenuItem extends the java.awt.CheckboxMenuItem and adds
a connection to boolean state actions.
|
static class |
Actions.MenuItem
Extension of Swing menu item with connection to
system actions.
|
static class |
Actions.SubMenu
Deprecated.
Extends deprecated
JMenuPlus . Instead create a regular JMenu and add items to it (or use DynamicMenuContent ). |
static interface |
Actions.SubMenuModel
Deprecated.
used by deprecated
Actions.SubMenu |
static class |
Actions.ToolbarButton
Deprecated.
extends deprecated ToolbarButton
|
static class |
Actions.ToolbarToggleButton
Deprecated.
extends deprecated ToolbarToggleButton
|
Modifier and Type | Field and Description |
---|---|
static String |
ACTION_VALUE_TOGGLE
Key for
Action.getValue(java.lang.String) to indicate that the action should be presented
as toggle, if possible. |
static String |
ACTION_VALUE_VISIBLE
Action may
Action.putValue(java.lang.String, java.lang.Object) this value to indicate that, if not enabled,
it should not be visible at all. |
Constructor and Description |
---|
Actions()
Deprecated.
should not be used
|
Modifier and Type | Method and Description |
---|---|
static Action |
alwaysEnabled(ActionListener delegate,
String displayName,
String iconBase,
boolean noIconInMenu)
Creates new action which is always enabled.
|
static ContextAwareAction |
callback(String key,
Action fallback,
boolean surviveFocusChange,
String displayName,
String iconBase,
boolean noIconInMenu)
Creates new "callback" action.
|
static Action |
checkbox(String preferencesNode,
String preferencesKey,
String displayName,
String iconBase,
boolean noIconInMenu)
Creates action which represents a boolean value in
Preferences . |
static void |
connect(AbstractButton button,
Action action)
Connects buttons to action.
|
static void |
connect(AbstractButton button,
BooleanStateAction action)
Connects buttons to action.
|
static void |
connect(AbstractButton button,
SystemAction action)
Deprecated.
Use
Actions.connect(AbstractButton, Action) instead. |
static void |
connect(JCheckBoxMenuItem item,
Action action,
boolean popup)
Attaches checkbox menu item to boolean state action.
|
static void |
connect(JCheckBoxMenuItem item,
BooleanStateAction action,
boolean popup)
Deprecated.
Please use
Actions.connect(javax.swing.JCheckBoxMenuItem, javax.swing.Action, boolean) .
Have your action to implement properly Action.getValue(java.lang.String) for Action.SELECTED_KEY |
static void |
connect(JMenuItem item,
Action action,
boolean popup)
Attaches menu item to an action.
|
static void |
connect(JMenuItem item,
SystemAction action,
boolean popup)
Deprecated.
Use
Actions.connect(JMenuItem, Action, boolean) instead. |
static ContextAwareAction |
context(Class<?> type,
boolean single,
boolean surviveFocusChange,
ContextAwareAction delegate,
String key,
String displayName,
String iconBase,
boolean noIconInMenu)
Creates new "context" action, an action that observes the current
selection for a given type and enables if instances of given type are
present.
|
static String |
cutAmpersand(String text)
Removes an ampersand from a text string; commonly used to strip out unneeded mnemonics.
|
static String |
findKey(SystemAction action)
Method that finds the keydescription assigned to this action.
|
static Action |
forID(String category,
String id)
Locates a specific action programmatically.
|
static String |
keyStrokeToString(KeyStroke stroke)
Creates nice textual representation of KeyStroke.
|
static void |
setMenuText(AbstractButton item,
String text,
boolean useMnemonic)
Deprecated.
Use either
AbstractButton.setText(java.lang.String) or Mnemonics.setLocalizedText(AbstractButton, String) as appropriate. |
public static final String ACTION_VALUE_VISIBLE
Action.putValue(java.lang.String, java.lang.Object)
this value to indicate that, if not enabled,
it should not be visible at all. Presenters may honour the request by removing
action's presenter from the UI.public static final String ACTION_VALUE_TOGGLE
Action.getValue(java.lang.String)
to indicate that the action should be presented
as toggle, if possible. Presenters may create checkbox item, toggle button etc.
This is to avoid accessing the Action.SELECTED_KEY
actual value during
presenter construction, as evaluation may be expensive.@Deprecated public Actions()
public static String findKey(SystemAction action)
action
- action to find key forpublic static String keyStrokeToString(KeyStroke stroke)
stroke
- the KeyStroke to get description of@Deprecated public static void connect(JMenuItem item, SystemAction action, boolean popup)
Actions.connect(JMenuItem, Action, boolean)
instead.item
- menu itemaction
- actionpopup
- create popup or menu itempublic static void connect(JMenuItem item, Action action, boolean popup)
Actions.ButtonActionConnector.connect(JMenuItem, Action, boolean)
and
registering an instance of Actions.ButtonActionConnector
in the
default lookup.
Since version 7.1 the action can also provide properties
"menuText" and "popupText" if one wants to use other text on the JMenuItem
than the name
of the action taken from Action.NAME. The popupText is checked only if the
popup parameter is true and takes the biggest precedence. The menuText is
tested everytime and takes precedence over standard Action.NAME
By default icons are not visible in popup menus. This can be configured via branding.
item
- menu itemaction
- actionpopup
- create popup or menu item@Deprecated public static void connect(JCheckBoxMenuItem item, BooleanStateAction action, boolean popup)
Actions.connect(javax.swing.JCheckBoxMenuItem, javax.swing.Action, boolean)
.
Have your action to implement properly Action.getValue(java.lang.String)
for Action.SELECTED_KEY
item
- menu itemaction
- actionpopup
- create popup or menu itempublic static void connect(JCheckBoxMenuItem item, Action action, boolean popup)
Action.SELECTED_KEY
action valueitem
- menu itemaction
- actionpopup
- create popup or menu item@Deprecated public static void connect(AbstractButton button, SystemAction action)
Actions.connect(AbstractButton, Action)
instead.button
- the buttonaction
- the actionpublic static void connect(AbstractButton button, Action action)
SystemAction.iconResource()
for more details.
You can supply an alternative implementation
for this method by implementing method
Actions.ButtonActionConnector.connect(AbstractButton, Action)
and
registering an instance of Actions.ButtonActionConnector
in the
default lookup.button
- the buttonaction
- the actionpublic static void connect(AbstractButton button, BooleanStateAction action)
button
- the buttonaction
- the action@Deprecated public static void setMenuText(AbstractButton item, String text, boolean useMnemonic)
AbstractButton.setText(java.lang.String)
or Mnemonics.setLocalizedText(AbstractButton, String)
as appropriate.item
- AbstractButtontext
- new labeluseMnemonic
- if true and '&' char found in new text, next char is used
as Mnemonic.public static String cutAmpersand(String text)
&?
by ?
or (&??
by the empty string
where ?
is a wildcard for any character.
&?
is a shortcut in English locale.
(&?)
is a shortcut in Japanese locale.
Used to remove shortcuts from workspace names (or similar) when shortcuts are not supported.
The current implementation behaves in the same way regardless of locale. In case of a conflict it would be necessary to change the behavior based on the current locale.
text
- a localized label that may have mnemonic information in it&
if there was anypublic static Action alwaysEnabled(ActionListener delegate, String displayName, String iconBase, boolean noIconInMenu)
ActionRegistration
annotation:
This method can also be used from XML Layer directly by following XML definition:@ActionRegistration
(displayName="#key")@ActionID
(id="your.pkg.action.id", category="Tools") public final class Always implementsActionListener
{ public Always() { } public void actionPerformed(ActionEvent
e) { // your code } }
<file name="your-pkg-action-id.instance"> <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.alwaysEnabled"/> <attr name="delegate" methodvalue="your.pkg.YourAction.factoryMethod"/> <attr name="displayName" bundlevalue="your.pkg.Bundle#key"/> <attr name="iconBase" stringvalue="your/pkg/YourImage.png"/> <!-- if desired: <attr name="noIconInMenu" boolvalue="true"/> --> <!-- if desired: <attr name="asynchronous" boolvalue="true"/> --> </file>In case the "delegate" is not just
ActionListener
, but also
Action
, the returned action acts as a lazy proxy - it defers initialization
of the action itself, but as soon as it is created, it delegates all queries
to it. This way one can create an action that looks statically enabled, and as soon
as user really uses it, it becomes active - it can change its name, it can
change its enabled state, etc.delegate
- the task to perform when action is invokeddisplayName
- the name of the actioniconBase
- the location to the actions iconnoIconInMenu
- true if this icon shall not have an item in menupublic static Action checkbox(String preferencesNode, String preferencesKey, String displayName, String iconBase, boolean noIconInMenu)
Preferences
.
When added to a menu the action is presented as a JCheckBox.
This method can also be used from
XML Layer
directly by following XML definition:
<file name="your-pkg-action-id.instance"> <attr name="preferencesNode" methodvalue="method-returning-Preferences-instance" or methodvalue="method-returning-Lookup-that-contains-Preferences-instance" or stringvalue="see below for the preferencesNode parameter description" /> <attr name="preferencesKey" stringvalue="preferences-key-name"/> <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.checkbox"/> <attr name="displayName" bundlevalue="your.pkg.Bundle#key"/> <attr name="iconBase" stringvalue="your/pkg/YourImage.png"/> <!-- if desired: <attr name="noIconInMenu" boolvalue="true"/> --> <!-- if desired: <attr name="asynchronous" boolvalue="true"/> --> </file>
preferencesNode
- It's one of:
NbPreferences.root()
.Preferences.systemRoot()
.Preferences.userRoot()
.preferencesKey
- name of the preferences key.displayName
- the name of the actioniconBase
- the location to the actions iconnoIconInMenu
- true if this icon shall not have an item in menupublic static ContextAwareAction callback(String key, Action fallback, boolean surviveFocusChange, String displayName, String iconBase, boolean noIconInMenu)
ActionMap
of currently
active component. You can use ActionRegistration
annotation to
register your action:
If you want to create callback action without any fallback implementation, you can annotate any string constant:@ActionRegistration
(displayName="#Key", key="KeyInActionMap")@ActionID
(category="Tools", id = "action.pkg.ClassName") public final class Fallback implementsActionListener
{ public void actionPerformed(ActionEvent
e) { // your code } }
@ActionRegistration
(displayName = "#Key")@ActionID
(category = "Edit", id = "my.field.action") public static final String ACTION_MAP_KEY = "KeyInActionMap";
This action can be lazily declared in a layer file using following XML snippet:
<file name="action-pkg-ClassName.instance"> <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.callback"/> <attr name="key" stringvalue="KeyInActionMap"/> <attr name="surviveFocusChange" boolvalue="false"/> <!-- defaults to false --> <attr name="fallback" newvalue="action.pkg.DefaultAction"/> <!-- may be missing --> <attr name="displayName" bundlevalue="your.pkg.Bundle#key"/> <attr name="iconBase" stringvalue="your/pkg/YourImage.png"/> <!-- if desired: <attr name="noIconInMenu" boolvalue="true"/> --> <!-- if desired: <attr name="asynchronous" boolvalue="true"/> --> </file>
key
- the key to search for in an ActionMap
surviveFocusChange
- true to remember action provided by previously
active component even some other component is currently activefallback
- action to delegate to when no key found. Use null
to make the action disabled if delegate assigned to key is missingdisplayName
- localized name of the action (including ampersand)iconBase
- the location to the action iconnoIconInMenu
- true if this icon shall not have an item in menupublic static ContextAwareAction context(Class<?> type, boolean single, boolean surviveFocusChange, ContextAwareAction delegate, String key, String displayName, String iconBase, boolean noIconInMenu)
Openable
,
Editable
, Closable
, Viewable
, and any interfaces
defined and exposed by various other APIs. Use ActionRegistration
annotation to register your action::
In case you are interested in creating multi selection action, just change parameters of your constructor:@ActionRegistration
(displayName="#Key")@ActionID
(category="Tools", id = "action.pkg.YourClass") public final class YourClass implementsActionListener
{ Openable context; public YourClass(Openable context) { this.context = context; } public void actionPerformed(ActionEvent
ev) { // do something with context } }
@ActionRegistration
(displayName="#Key")@ActionID
(category="Tools", id = "action.pkg.YourClass") public final class YourClass implementsActionListener
{ List<Openable> context; public YourClass(List<Openable> context) { this.context = context; } public void actionPerformed(ActionEvent
ev) { // do something with context } }
Actions of this kind can be declared in layer file using following XML snippet:
<file name="action-pkg-ClassName.instance"> <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.context"/> <attr name="type" stringvalue="org.netbeans.api.actions.Openable"/> <attr name="selectionType" stringvalue="ANY"/> <-- or EXACTLY_ONE --> <attr name="delegate" newvalue="action.pkg.YourAction"/> <!-- Similar registration like in case of "callback" action. May be missing completely: --> <attr name="key" stringvalue="KeyInActionMap"/> <attr name="surviveFocusChange" boolvalue="false"/> <attr name="displayName" bundlevalue="your.pkg.Bundle#key"/> <attr name="iconBase" stringvalue="your/pkg/YourImage.png"/> <!-- if desired: <attr name="noIconInMenu" boolvalue="true"/> --> <!-- if desired: <attr name="asynchronous" boolvalue="true"/> --> </file>In the previous case there has to be a class with public default constructor named
action.pkg.YourAction
. It has to implement
ContextAwareAction
interface. Its ContextAwareAction.createContextAwareInstance(org.openide.util.Lookup)
method is called when the action is invoked. The passed in Lookup
contains instances of the type
interface, actionPerformed
is then called on the returned clone.
Alternatively one can use support for simple dependency injection by using following attributes:
<file name="action-pkg-ClassName.instance"> <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.context"/> <attr name="type" stringvalue="org.netbeans.api.actions.Openable"/> <attr name="delegate" methodvalue="org.openide.awt.Actions.inject"/> <attr name="selectionType" stringvalue="EXACTLY_ONE"/> <attr name="injectable" stringvalue="pkg.YourClass"/> <attr name="displayName" bundlevalue="your.pkg.Bundle#key"/> <attr name="iconBase" stringvalue="your/pkg/YourImage.png"/> <!-- if desired: <attr name="noIconInMenu" boolvalue="true"/> --> </file>where
pkg.YourClass
is defined with public constructor taking
type
:
public final class YourClass implements ActionListener { Openable context; public YourClass(Openable context) { this.context = context; } public void actionPerformed(ActionEvent ev) { // do something with context } }The instance of this class is created when the action is invoked and its constructor is fed with the instance of
type
inside
the active context. actionPerformed
method is called then.
To create action that handled multiselection one can use following XML snippet:
<file name="action-pkg-ClassName.instance"> <attr name="type" stringvalue="org.netbeans.api.actions.Openable"/> <attr name="delegate" methodvalue="org.openide.awt.Actions.inject"/> <attr name="selectionType" stringvalue="ANY"/> <attr name="injectable" stringvalue="pkg.YourClass"/> <attr name="displayName" bundlevalue="your.pkg.Bundle#key"/> <attr name="iconBase" stringvalue="your/pkg/YourImage.png"/> <!-- if desired: <attr name="noIconInMenu" boolvalue="true"/> --> <!-- since 7.33: <attr name="context" newvalue="org.my.own.LookupImpl"/> --> </file>Now the constructor of
YourClass
needs to have following
form:
public final class YourClass implements ActionListener { List<Openable> context; public YourClass(List<Openable> context) { this.context = context; } }
Further attributes are defined to control action's enabled and checked state.
Attributes which control enable state are prefixed by "enableOn
". Attributes
controlling checked state have prefix "checkedOn
":
<file name="action-pkg-ClassName.instance">
<!-- Enable on certain type in Lookup -->
<attr name="enableOnType" stringvalue="qualified.type.name"/>
<!-- Monitor specific property in that type -->
<attr name="enableOnProperty" stringvalue="propertyName"/>
<!-- The property value, which corresponds to enabled action.
Values "#null" and "#non-null" are treated specially.
-->
<attr name="enableOnValue" stringvalue="propertyName"/>
<!-- Name of custom listener interface -->
<attr name="enableOnChangeListener" stringvalue="qualifier.listener.interface"/>
<!-- Name of listener method that triggers state re-evaluation -->
<attr name="enableOnMethod" stringvalue="methodName"/>
<!-- Delegate to the action instance for final decision -->
<attr name="enableOnActionProperty" stringvalue="actionPropertyName"/>
<!-- ... -->
</file>
type
- the object to seek for in the active contextsingle
- shall there be just one or multiple instances of the objectsurviveFocusChange
- shall the action remain enabled and act on
previous selection even if no selection is currently in context?delegate
- action to call when this action is invokedkey
- alternatively an action can be looked up in action map
(see Actions.callback(java.lang.String, javax.swing.Action, boolean, java.lang.String, java.lang.String, boolean)
)displayName
- localized name of the action (including ampersand)iconBase
- the location to the action iconnoIconInMenu
- true if this icon shall not have an item in menupublic static Action forID(String category, String id) throws IllegalArgumentException
ActionRegistration
.
Normally an ActionReference
will suffice to insert the action
into various UI elements (typically using Utilities.actionsForPath(java.lang.String)
),
but in special circumstances you may need to find a single known action.
This method is just a shortcut for using FileUtil.getConfigObject(java.lang.String, java.lang.Class<T>)
with the correct arguments, plus using AcceleratorBinding.setAccelerator(javax.swing.Action, org.openide.filesystems.FileObject)
.
category
- as in ActionID.category()
id
- as in ActionID.id()
IllegalArgumentException
- if a corresponding ActionID
would have been rejected