Class Actions
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
SPI for supplying alternative implementation of connection between actions and presenters.static class
CheckboxMenuItem extends the java.awt.CheckboxMenuItem and adds a connection to boolean state actions.static class
Extension of Swing menu item with connection to system actions.static class
Deprecated.static interface
Deprecated.used by deprecatedActions.SubMenu
static class
Deprecated.extends deprecated ToolbarButtonstatic class
Deprecated.extends deprecated ToolbarToggleButton -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Key forAction.getValue(java.lang.String)
to indicate that the action should be presented as toggle, if possible.static final String
Action mayAction.putValue(java.lang.String, java.lang.Object)
this value to indicate that, if not enabled, it should not be visible at all. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Action
alwaysEnabled
(ActionListener delegate, String displayName, String iconBase, boolean noIconInMenu) Creates new action which is always enabled.static org.openide.util.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 inPreferences
.static void
connect
(AbstractButton button, Action action) Connects buttons to action.static void
connect
(AbstractButton button, org.openide.util.actions.BooleanStateAction action) Connects buttons to action.static void
connect
(AbstractButton button, org.openide.util.actions.SystemAction action) Deprecated.Useconnect(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, org.openide.util.actions.BooleanStateAction action, boolean popup) Deprecated.static void
Attaches menu item to an action.static void
Deprecated.Useconnect(JMenuItem, Action, boolean)
instead.static org.openide.util.ContextAwareAction
context
(Class<?> type, boolean single, boolean surviveFocusChange, org.openide.util.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
(org.openide.util.actions.SystemAction action) Method that finds the keydescription assigned to this action.static Action
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 eitherAbstractButton.setText(java.lang.String)
orMnemonics.setLocalizedText(AbstractButton, String)
as appropriate.
-
Field Details
-
ACTION_VALUE_VISIBLE
Action mayAction.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.- See Also:
-
ACTION_VALUE_TOGGLE
Key forAction.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 theAction.SELECTED_KEY
actual value during presenter construction, as evaluation may be expensive.- Since:
- 7.71
- See Also:
-
-
Constructor Details
-
Actions
Deprecated.should not be used
-
-
Method Details
-
findKey
Method that finds the keydescription assigned to this action.- Parameters:
action
- action to find key for- Returns:
- the text representing the key or null if there is no text assigned
-
keyStrokeToString
-
connect
@Deprecated public static void connect(JMenuItem item, org.openide.util.actions.SystemAction action, boolean popup) Deprecated.Useconnect(JMenuItem, Action, boolean)
instead.Attaches menu item to an action.- Parameters:
item
- menu itemaction
- actionpopup
- create popup or menu item
-
connect
Attaches menu item to an action. You can supply an alternative implementation for this method by implementing methodActions.ButtonActionConnector.connect(JMenuItem, Action, boolean)
and registering an instance ofActions.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.
- Parameters:
item
- menu itemaction
- actionpopup
- create popup or menu item- Since:
- 3.29
-
connect
@Deprecated public static void connect(JCheckBoxMenuItem item, org.openide.util.actions.BooleanStateAction action, boolean popup) Deprecated.Please useconnect(javax.swing.JCheckBoxMenuItem, javax.swing.Action, boolean)
. Have your action to implement properlyAction.getValue(java.lang.String)
forAction.SELECTED_KEY
Attaches checkbox menu item to boolean state action.- Parameters:
item
- menu itemaction
- actionpopup
- create popup or menu item
-
connect
Attaches checkbox menu item to boolean state action. The presenter connects to theAction.SELECTED_KEY
action value- Parameters:
item
- menu itemaction
- actionpopup
- create popup or menu item- Since:
- 7.71
-
connect
@Deprecated public static void connect(AbstractButton button, org.openide.util.actions.SystemAction action) Deprecated.Useconnect(AbstractButton, Action)
instead.Connects buttons to action.- Parameters:
button
- the buttonaction
- the action
-
connect
Connects buttons to action. If the action supplies value for "iconBase" key from getValue(String) with a path to icons, the methods set*Icon will be called on the button with loaded icons using the iconBase. E.g. if the value for "iconBase" is "com/mycompany/myIcon.gif" then the following images are tried- setIcon with "com/mycompany/myIcon.gif"
- setPressedIcon with "com/mycompany/myIcon_pressed.gif"
- setDisabledIcon with "com/mycompany/myIcon_disabled.gif"
- setRolloverIcon with "com/mycompany/myIcon_rollover.gif"
- setSelectedIcon with "com/mycompany/myIcon_selected.gif"
- setRolloverSelectedIcon with "com/mycompany/myIcon_rolloverSelected.gif"
- setDisabledSelectedIcon with "com/mycompany/myIcon_disabledSelected.gif"
SystemAction.iconResource()
for more details. You can supply an alternative implementation for this method by implementing methodActions.ButtonActionConnector.connect(AbstractButton, Action)
and registering an instance ofActions.ButtonActionConnector
in the default lookup.- Parameters:
button
- the buttonaction
- the action- Since:
- 3.29, 7.32 for set*SelectedIcon
-
connect
public static void connect(AbstractButton button, org.openide.util.actions.BooleanStateAction action) Connects buttons to action.- Parameters:
button
- the buttonaction
- the action
-
setMenuText
Deprecated.Use eitherAbstractButton.setText(java.lang.String)
orMnemonics.setLocalizedText(AbstractButton, String)
as appropriate.Sets the text for the menu item or other subclass of AbstractButton. Cut from the name '&' char.- Parameters:
item
- AbstractButtontext
- new labeluseMnemonic
- if true and '&' char found in new text, next char is used as Mnemonic.
-
cutAmpersand
Removes an ampersand from a text string; commonly used to strip out unneeded mnemonics. Replaces the first occurence of&?
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.
- Parameters:
text
- a localized label that may have mnemonic information in it- Returns:
- string without first
&
if there was any
-
alwaysEnabled
public static Action alwaysEnabled(ActionListener delegate, String displayName, String iconBase, boolean noIconInMenu) Creates new action which is always enabled. Rather than using this method directly, useActionRegistration
annotation:@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 justActionListener
, but alsoAction
, 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.- Parameters:
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 menu- Since:
- 7.3
-
checkbox
public static Action checkbox(String preferencesNode, String preferencesKey, String displayName, String iconBase, boolean noIconInMenu) Creates action which represents a boolean value inPreferences
. 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>
- Parameters:
preferencesNode
- It's one of:- Absolute path to preferences node under
NbPreferences.root()
. - "system:" followed by absolute path to preferences node under
Preferences.systemRoot()
. - "user:" followed by absolute path to preferences node under
Preferences.userRoot()
.
- Absolute path to preferences node under
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 menu- Since:
- 7.17
-
callback
public static org.openide.util.ContextAwareAction callback(String key, Action fallback, boolean surviveFocusChange, String displayName, String iconBase, boolean noIconInMenu) Creates new "callback" action. Such action has an assigned key which is used to find proper delegate inActionMap
of currently active component. You can useActionRegistration
annotation to register your action:@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>
- Parameters:
key
- the key to search for in anActionMap
fallback
- action to delegate to when no key found. Usenull
to make the action disabled if delegate assigned to key is missingsurviveFocusChange
- true to remember action provided by previously active component even some other component is currently activedisplayName
- localized name of the action (including ampersand)iconBase
- the location to the action iconnoIconInMenu
- true if this icon shall not have an item in menu- Returns:
- creates new action associated with given key
- Since:
- 7.10
-
context
public static org.openide.util.ContextAwareAction context(Class<?> type, boolean single, boolean surviveFocusChange, org.openide.util.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. Common interfaces to watch for includeOpenable
,Editable
,Closable
,Viewable
, and any interfaces defined and exposed by various other APIs. UseActionRegistration
annotation to register your action::@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 namedaction.pkg.YourAction
. It has to implementContextAwareAction
interface. ItsContextAwareAction.createContextAwareInstance(org.openide.util.Lookup)
method is called when the action is invoked. The passed inLookup
contains instances of thetype
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>
wherepkg.YourClass
is defined with public constructor takingtype
: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 oftype
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 ofYourClass
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>
- Parameters:
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 (seecallback(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 menu- Returns:
- new instance of context aware action watching for type
- Since:
- 7.10
-
forID
Locates a specific action programmatically. The action will typically have been registered usingActionRegistration
.Normally an
ActionReference
will suffice to insert the action into various UI elements (typically usingUtilities.actionsForPath(java.lang.String)
), but in special circumstances you may need to find a single known action. This method is just a shortcut for usingFileUtil.getConfigObject(java.lang.String, java.lang.Class<T>)
with the correct arguments, plus usingAcceleratorBinding.setAccelerator(javax.swing.Action, org.openide.filesystems.FileObject)
.- Parameters:
category
- as inActionID.category()
id
- as inActionID.id()
- Returns:
- the action registered under that ID, or null
- Throws:
IllegalArgumentException
- if a correspondingActionID
would have been rejected- Since:
- 7.42
-
JMenuPlus
.