Class NMAction
- java.lang.Object
-
- javax.swing.AbstractAction
-
- com.nomagic.actions.NMAction
-
- All Implemented Interfaces:
java.awt.event.ActionListener
,java.io.Serializable
,java.lang.Cloneable
,java.lang.Comparable
,java.util.EventListener
,javax.swing.Action
- Direct Known Subclasses:
ActionsCategory
,ColorChooseAction
,MDAction
,NMStateAction
@OpenApiAll public abstract class NMAction extends javax.swing.AbstractAction implements java.lang.Comparable
The class describes some abstract action. Any specific action must implement actionPerformed() method. The action has such properties: name, id,small icon,large icon,some shortcuts,mnemonic, description, group- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ACTION_SHORTCUTS
Key for storing command keys.static java.lang.String
BELONGS_TO_SEPARATE_GROUP_IN_UI
A name of property to put this action into a dedicate special group.static java.lang.String
DO_NO_SHOW_ACTION_NAME_IN_UI
A name of property to hide action name text in create UI.static java.lang.String
DO_NOT_REGISTER_ACTION_TO_COMPONENTS
A name of property to skip registering this action to components.static java.lang.String
GENERATED_ID_PREFIX
static java.lang.String
GROUP
Key for storing group.static java.lang.String
ID
Key for storing id.static java.lang.String
LARGE_ICON
Key for storing large icon.static int
MENU_SHORTCUT_MASK
Menu shortcut mask used to create shortcuts.static java.lang.String
TINY_ICON
Key for storing tiny icon (smaller than small icon).
-
Constructor Summary
Constructors Constructor Description NMAction()
Constructs the action with nulls.NMAction(java.lang.String id, java.lang.String name, int mnemonic)
Constructs the action with given id, name, mnemonic key.NMAction(java.lang.String id, java.lang.String name, int mnemonic, java.lang.String group)
Constructs the action with given id, name, mnemonic key and group.NMAction(java.lang.String id, java.lang.String name, javax.swing.KeyStroke stroke)
Constructs the action with given id, name, keystroke.NMAction(java.lang.String id, java.lang.String name, javax.swing.KeyStroke stroke, java.lang.String group)
Constructs the action with given id, name, keystroke and group.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
accept(ActionsVisitor visitor)
Accepts ActionsVisitor using Visitor pattern.abstract void
actionPerformed(java.awt.event.ActionEvent e)
Executes given action.void
addAction(NMAction action)
Adds action.void
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds aPropertyChangeListener
to the listener list.void
addShortcut(javax.swing.KeyStroke key)
Adds the shortcut to the action.void
addShotcut(javax.swing.KeyStroke key)
Deprecated.type error.void
addWeakPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds aPropertyChangeListener
to the weak listener list.NMAction
clone()
Clones the object.int
compareTo(java.lang.Object o)
Compares the name of the action.javax.swing.JMenuItem
createMenuItem(com.nomagic.actions.MenuComponentFactory factory)
Action can return preferred representation of itself as menu item.boolean
equals(java.lang.Object o)
Checks if given object is equal to the current action.protected void
firePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
Supports reporting bound property changes.void
forEach(java.util.function.Consumer<NMAction> consumer)
Performs the given consumer for each owned action.void
forEachIncludingSelf(java.util.function.Consumer<NMAction> consumer)
Performs the given consumer for each owned action including self.java.util.List<NMAction>
getActions()
Returns the list of the actions.javax.swing.KeyStroke
getCommandKey()
Returns the shortcut of the action.java.lang.String
getDescription()
Returns description of this action.java.lang.String
getGroup()
Returns the name of the related actions group.javax.swing.Icon
getIcon()
Deprecated.use {link #getSmallOrLargeIcon}java.lang.String
getID()
Returns the id of the action.javax.swing.Icon
getLargeIcon()
Returns the large icon of the action.javax.swing.Icon
getLargeOrSmallIcon()
static java.lang.String
getMenuShortcutMaskAsString()
int
getMnemonicKey()
Returns the mnemonic of the action.java.lang.String
getName()
Returns the name of the action.java.beans.PropertyChangeListener[]
getPropertyChangeListeners()
Returns an array of all thePropertyChangeListener
s added to this action with addPropertyChangeListener().java.util.List<javax.swing.KeyStroke>
getShortcuts()
Returns all shortcuts of the the action.javax.swing.Icon
getSmallIcon()
Returns the small icon of the action.javax.swing.Icon
getSmallOrLargeIcon()
javax.swing.Icon
getTinyIcon()
Returns the tiny icon of the action.int
hashCode()
Creates hashcode from id.static boolean
hasMenuShortcutMask(java.awt.AWTEvent event)
Check if event has menu shortcut mask modifierboolean
isIDGenerated()
Returns true if id was generated.void
removeAction(NMAction action)
Remove action.void
removeIf(java.util.function.Predicate<NMAction> filter)
Removes all of the owned actions that satisfy the given predicatevoid
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes aPropertyChangeListener
from the listener list.void
removeShortcut(javax.swing.KeyStroke key)
Removes the shortcut from the action.void
removeWeakPropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes aPropertyChangeListener
from the weak listener list.void
setActions(java.util.List<? extends NMAction> actions)
Sets the list of the actions(do nothing here).void
setDescription(java.lang.String description)
Sets new action description.protected void
setGroup(java.lang.String group)
Sets new group for this action.void
setIcon(javax.swing.Icon icon)
Sets small and large icon.void
setLargeIcon(javax.swing.Icon icon)
Sets the large icon of the action.void
setMnemonicKey(int key)
Sets the mnemonic key of the action.void
setName(java.lang.String name)
Sets new name of action, method implemented by putting value with key Action.NAMEvoid
setShortcuts(java.util.List<javax.swing.KeyStroke> shortcuts)
Sets the shortcuts list.void
setSmallIcon(javax.swing.Icon icon)
Sets the small icon of the action.void
setTinyIcon(javax.swing.Icon icon)
Sets the tiny icon of the action.NMAction
shallowClone()
Returns itselfjava.lang.String
toString()
toString returns the action's name.void
updateState()
Method should update action state (enabled or disabled, checked or unchecked).-
Methods inherited from class javax.swing.AbstractAction
getKeys, getValue, isEnabled, putValue, setEnabled
-
-
-
-
Field Detail
-
DO_NO_SHOW_ACTION_NAME_IN_UI
public static final java.lang.String DO_NO_SHOW_ACTION_NAME_IN_UI
A name of property to hide action name text in create UI. For example buttons will not show name of this action as text- See Also:
- Constant Field Values
-
BELONGS_TO_SEPARATE_GROUP_IN_UI
public static final java.lang.String BELONGS_TO_SEPARATE_GROUP_IN_UI
A name of property to put this action into a dedicate special group. For example Options action can be in the right side of horizontal toolbar.- See Also:
- Constant Field Values
-
DO_NOT_REGISTER_ACTION_TO_COMPONENTS
public static final java.lang.String DO_NOT_REGISTER_ACTION_TO_COMPONENTS
A name of property to skip registering this action to components.- See Also:
- Constant Field Values
-
ACTION_SHORTCUTS
public static final java.lang.String ACTION_SHORTCUTS
Key for storing command keys.- See Also:
- Constant Field Values
-
ID
public static final java.lang.String ID
Key for storing id.- See Also:
- Constant Field Values
-
LARGE_ICON
public static final java.lang.String LARGE_ICON
Key for storing large icon.- See Also:
- Constant Field Values
-
TINY_ICON
public static final java.lang.String TINY_ICON
Key for storing tiny icon (smaller than small icon).- See Also:
- Constant Field Values
-
GROUP
public static final java.lang.String GROUP
Key for storing group.- See Also:
- Constant Field Values
-
MENU_SHORTCUT_MASK
public static final int MENU_SHORTCUT_MASK
Menu shortcut mask used to create shortcuts.
-
GENERATED_ID_PREFIX
public static final java.lang.String GENERATED_ID_PREFIX
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
NMAction
public NMAction(@CheckForNull java.lang.String id, @CheckForNull java.lang.String name, @CheckForNull javax.swing.KeyStroke stroke, @CheckForNull java.lang.String group)
Constructs the action with given id, name, keystroke and group.- Parameters:
id
- the id of the action.name
- the name of the action.stroke
- the KeyStroke of the action.group
- the name of the related commands group.
-
NMAction
public NMAction(@CheckForNull java.lang.String id, @CheckForNull java.lang.String name, @CheckForNull javax.swing.KeyStroke stroke)
Constructs the action with given id, name, keystroke.- Parameters:
id
- the id of the action.name
- the name of the action.stroke
- the KeyStroke of the action.
-
NMAction
public NMAction()
Constructs the action with nulls.
-
NMAction
public NMAction(@CheckForNull java.lang.String id, @CheckForNull java.lang.String name, int mnemonic, @CheckForNull java.lang.String group)
Constructs the action with given id, name, mnemonic key and group.- Parameters:
id
- the id of the action.name
- the name of the action.mnemonic
- the mnemonic key of the actiongroup
- the name of the related commands group.
-
NMAction
public NMAction(@CheckForNull java.lang.String id, @CheckForNull java.lang.String name, int mnemonic)
Constructs the action with given id, name, mnemonic key.- Parameters:
id
- the id of the action.name
- the name of the action.mnemonic
- the mnemonic key of the action
-
-
Method Detail
-
setName
public void setName(java.lang.String name)
Sets new name of action, method implemented by putting value with key Action.NAME- Parameters:
name
- name
-
getID
public java.lang.String getID()
Returns the id of the action.- Returns:
- the id of the action.
-
getCommandKey
@CheckForNull public javax.swing.KeyStroke getCommandKey()
Returns the shortcut of the action.- Returns:
- the first shortcut from all available shortcuts, null if no shortcuts are defined.
-
addShotcut
@Deprecated public void addShotcut(@CheckForNull javax.swing.KeyStroke key)
Deprecated.type error. UseaddShortcut(KeyStroke)
Adds the shortcut to the action.- Parameters:
key
- the given shortcut.
-
addShortcut
public void addShortcut(@CheckForNull javax.swing.KeyStroke key)
Adds the shortcut to the action.- Parameters:
key
- the given shortcut.
-
removeShortcut
public void removeShortcut(@CheckForNull javax.swing.KeyStroke key)
Removes the shortcut from the action.- Parameters:
key
- the given shortcut.
-
setShortcuts
public void setShortcuts(@CheckForNull java.util.List<javax.swing.KeyStroke> shortcuts)
Sets the shortcuts list.- Parameters:
shortcuts
- the list of all shortcuts.
-
getShortcuts
public java.util.List<javax.swing.KeyStroke> getShortcuts()
Returns all shortcuts of the the action.- Returns:
- the list of all shortcuts. List is unmodifiable.
-
getName
public java.lang.String getName()
Returns the name of the action.- Returns:
- the name of the action.
-
setMnemonicKey
public void setMnemonicKey(int key)
Sets the mnemonic key of the action.- Parameters:
key
- the mnemonic key of the action.
-
getMnemonicKey
public int getMnemonicKey()
Returns the mnemonic of the action.- Returns:
- the mnemonic of the action or -1 if command does not have mnemonic.
-
setIcon
public void setIcon(@CheckForNull javax.swing.Icon icon)
Sets small and large icon.- Parameters:
icon
- Icon of the action.
-
setSmallIcon
public void setSmallIcon(@CheckForNull javax.swing.Icon icon)
Sets the small icon of the action.- Parameters:
icon
- the small icon of the action.
-
getSmallIcon
@CheckForNull public javax.swing.Icon getSmallIcon()
Returns the small icon of the action.- Returns:
- the small icon of the action.
-
setTinyIcon
public void setTinyIcon(@CheckForNull javax.swing.Icon icon)
Sets the tiny icon of the action.- Parameters:
icon
- the tiny icon of the action
-
getTinyIcon
@CheckForNull public javax.swing.Icon getTinyIcon()
Returns the tiny icon of the action.- Returns:
- the tiny icon of the action.
-
getIcon
@Deprecated @CheckForNull public javax.swing.Icon getIcon()
Deprecated.use {link #getSmallOrLargeIcon}
-
setLargeIcon
public void setLargeIcon(@CheckForNull javax.swing.Icon icon)
Sets the large icon of the action.- Parameters:
icon
- the large icon of the action.
-
getLargeIcon
@CheckForNull public javax.swing.Icon getLargeIcon()
Returns the large icon of the action.- Returns:
- the large icon of the action.
-
getLargeOrSmallIcon
@CheckForNull public javax.swing.Icon getLargeOrSmallIcon()
- Returns:
- large icon if present. If not, small one
-
getSmallOrLargeIcon
@CheckForNull public javax.swing.Icon getSmallOrLargeIcon()
- Returns:
- small icon if present. If not, large one
-
equals
public boolean equals(java.lang.Object o)
Checks if given object is equal to the current action.- Overrides:
equals
in classjava.lang.Object
- Returns:
- true if given object is NMAction and id of the given action is the same as id of current action.
-
hashCode
public int hashCode()
Creates hashcode from id.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- hash code of action.
-
toString
public java.lang.String toString()
toString returns the action's name.- Overrides:
toString
in classjava.lang.Object
- Returns:
- the name.
-
actionPerformed
public abstract void actionPerformed(@CheckForNull java.awt.event.ActionEvent e)
Executes given action.- Specified by:
actionPerformed
in interfacejava.awt.event.ActionListener
- Parameters:
e
- event caused execution.
-
compareTo
public int compareTo(java.lang.Object o)
Compares the name of the action. Uses String.compareTo method.- Specified by:
compareTo
in interfacejava.lang.Comparable
- Parameters:
o
- the given object to compare to.- Returns:
- name comparison result
-
clone
public NMAction clone()
Clones the object. Does the deep clone.- Overrides:
clone
in classjavax.swing.AbstractAction
- Returns:
- cloned object
-
shallowClone
public NMAction shallowClone()
Returns itself- Returns:
- itself here
-
accept
public void accept(ActionsVisitor visitor)
Accepts ActionsVisitor using Visitor pattern.- Parameters:
visitor
- visitor to accept.
-
setDescription
public void setDescription(@CheckForNull java.lang.String description)
Sets new action description.- Parameters:
description
- action description.
-
getDescription
public java.lang.String getDescription()
Returns description of this action.- Returns:
- action description.
-
setGroup
protected void setGroup(@CheckForNull java.lang.String group)
Sets new group for this action. Empty string will be converted to null.- Parameters:
group
- group name.
-
getGroup
public java.lang.String getGroup()
Returns the name of the related actions group.- Returns:
- the name of the related actions group.
-
getActions
public java.util.List<NMAction> getActions()
Returns the list of the actions.- Returns:
- empty list here. Some derived classes may override this method.
-
forEach
public void forEach(java.util.function.Consumer<NMAction> consumer)
Performs the given consumer for each owned action. Owned actions should not be added/removed inside consumer.- Parameters:
consumer
- the action to be performed for each NMAction
-
forEachIncludingSelf
public void forEachIncludingSelf(java.util.function.Consumer<NMAction> consumer)
Performs the given consumer for each owned action including self. Owned actions should not be added/removed inside consumer.- Parameters:
consumer
- the action to be performed for each NMAction
-
setActions
public void setActions(java.util.List<? extends NMAction> actions)
Sets the list of the actions(do nothing here).- Parameters:
actions
- the list of the actions.
-
addAction
public void addAction(NMAction action)
Adds action. Not supported here.- Parameters:
action
- the action to add.
-
removeAction
public void removeAction(NMAction action)
Remove action. Not supported here.- Parameters:
action
- the action to remove.
-
removeIf
public void removeIf(java.util.function.Predicate<NMAction> filter)
Removes all of the owned actions that satisfy the given predicate- Parameters:
filter
- given predicate
-
updateState
public void updateState()
Method should update action state (enabled or disabled, checked or unchecked). Method is called after other actions was executed. Always enable here.
-
addPropertyChangeListener
public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds aPropertyChangeListener
to the listener list. The listener is registered for all properties.A
PropertyChangeEvent
will get fired in response to setting a bound property, e.g.setFont
,setBackground
, orsetForeground
. Note that if the current component is inheriting its foreground, background, or font from its container, then no event will be fired in response to a change in the inherited property. This method creates advanced SwingPropertyChangeSupport which is safe to use when removing/adding listeners during firing property change event.- Specified by:
addPropertyChangeListener
in interfacejavax.swing.Action
- Overrides:
addPropertyChangeListener
in classjavax.swing.AbstractAction
- Parameters:
listener
- ThePropertyChangeListener
to be added- See Also:
Action.addPropertyChangeListener(java.beans.PropertyChangeListener)
-
addWeakPropertyChangeListener
public void addWeakPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds aPropertyChangeListener
to the weak listener list. The listener is registered for all properties.A
PropertyChangeEvent
will get fired in response to setting a bound property, e.g.setFont
,setBackground
, orsetForeground
. Note that if the current component is inheriting its foreground, background, or font from its container, then no event will be fired in response to a change in the inherited property. This method creates advanced SwingPropertyChangeSupport which is safe to use when removing/adding listeners during firing property change event.- Parameters:
listener
- ThePropertyChangeListener
to be added- See Also:
Action.addPropertyChangeListener(java.beans.PropertyChangeListener)
-
firePropertyChange
protected void firePropertyChange(java.lang.String propertyName, @CheckForNull java.lang.Object oldValue, @CheckForNull java.lang.Object newValue)
Supports reporting bound property changes. This method can be called when a bound property has changed and it will send the appropriatePropertyChangeEvent
to any registeredPropertyChangeListeners
.- Overrides:
firePropertyChange
in classjavax.swing.AbstractAction
-
removePropertyChangeListener
public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes aPropertyChangeListener
from the listener list. This removes aPropertyChangeListener
that was registered for all properties.- Specified by:
removePropertyChangeListener
in interfacejavax.swing.Action
- Overrides:
removePropertyChangeListener
in classjavax.swing.AbstractAction
- Parameters:
listener
- thePropertyChangeListener
to be removed- See Also:
Action.removePropertyChangeListener(java.beans.PropertyChangeListener)
-
removeWeakPropertyChangeListener
public void removeWeakPropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes aPropertyChangeListener
from the weak listener list. This removes aPropertyChangeListener
that was registered for all properties.- Parameters:
listener
- thePropertyChangeListener
to be removed- See Also:
Action.removePropertyChangeListener(java.beans.PropertyChangeListener)
-
createMenuItem
@CheckForNull public javax.swing.JMenuItem createMenuItem(com.nomagic.actions.MenuComponentFactory factory)
Action can return preferred representation of itself as menu item. If action returns null, some external menu item creation mechanism will be used.- Parameters:
factory
- factory.- Returns:
- the menu item or null.
-
isIDGenerated
public boolean isIDGenerated()
Returns true if id was generated.- Returns:
- true if id was generated.
-
getPropertyChangeListeners
public java.beans.PropertyChangeListener[] getPropertyChangeListeners()
Returns an array of all thePropertyChangeListener
s added to this action with addPropertyChangeListener().- Overrides:
getPropertyChangeListeners
in classjavax.swing.AbstractAction
- Returns:
- all of the
PropertyChangeListener
s added or an empty array if no listeners have been added
-
hasMenuShortcutMask
public static boolean hasMenuShortcutMask(java.awt.AWTEvent event)
Check if event has menu shortcut mask modifier- Parameters:
event
- event- Returns:
- true if event has CONTROL (or COMMAND on Mac) modifiers
-
getMenuShortcutMaskAsString
public static java.lang.String getMenuShortcutMaskAsString()
- Returns:
- representation of menu shortcut modifier. Representation can be used in various UI messages.
-
-