Class NMAction

java.lang.Object
javax.swing.AbstractAction
com.nomagic.actions.NMAction
All Implemented Interfaces:
ActionListener, Serializable, Cloneable, Comparable, EventListener, Action
Direct Known Subclasses:
ActionsCategory, BaseNMStateAction, ColorChooseAction, MDAction

@OpenApiAll public abstract class NMAction extends AbstractAction implements 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:
  • Field Details

    • DO_NO_SHOW_ACTION_NAME_IN_UI

      public static final 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:
    • BELONGS_TO_SEPARATE_GROUP_IN_UI

      public static final 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:
    • DO_NOT_REGISTER_ACTION_TO_COMPONENTS

      public static final String DO_NOT_REGISTER_ACTION_TO_COMPONENTS
      A name of property to skip registering this action to components.
      See Also:
    • ACTION_SHORTCUTS

      public static final String ACTION_SHORTCUTS
      Key for storing command keys.
      See Also:
    • ID

      public static final String ID
      Key for storing id.
      See Also:
    • LARGE_ICON

      public static final String LARGE_ICON
      Key for storing large icon.
      See Also:
    • TINY_ICON

      public static final String TINY_ICON
      Key for storing tiny icon (smaller than small icon).
      See Also:
    • GROUP

      public static final String GROUP
      Key for storing group.
      See Also:
    • GENERATED_ID_PREFIX

      public static final String GENERATED_ID_PREFIX
      See Also:
  • Constructor Details

    • NMAction

      public NMAction(@CheckForNull String id, @CheckForNull String name, @CheckForNull KeyStroke stroke, @CheckForNull 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 String id, @CheckForNull String name, @CheckForNull 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 String id, @CheckForNull String name, int mnemonic, @CheckForNull 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 action
      group - the name of the related commands group.
    • NMAction

      public NMAction(@CheckForNull String id, @CheckForNull 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 Details

    • setName

      public void setName(@CheckForNull String name)
      Sets new name of action, method implemented by putting value with key Action.NAME
      Parameters:
      name - name
    • getID

      public String getID()
      Returns the id of the action.
      Returns:
      the id of the action.
    • getCommandKey

      @CheckForNull public 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 KeyStroke key)
      Deprecated.
      type error. Use addShortcut(KeyStroke)
      Adds the shortcut to the action.
      Parameters:
      key - the given shortcut.
    • addShortcut

      public void addShortcut(@CheckForNull KeyStroke key)
      Adds the shortcut to the action.
      Parameters:
      key - the given shortcut.
    • removeShortcut

      public void removeShortcut(@CheckForNull KeyStroke key)
      Removes the shortcut from the action.
      Parameters:
      key - the given shortcut.
    • setShortcuts

      public void setShortcuts(@CheckForNull List<KeyStroke> shortcuts)
      Sets the shortcuts list.
      Parameters:
      shortcuts - the list of all shortcuts.
    • getShortcuts

      public List<KeyStroke> getShortcuts()
      Returns all shortcuts of the the action.
      Returns:
      the list of all shortcuts. List is unmodifiable.
    • getName

      public 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 Icon icon)
      Sets small and large icon.
      Parameters:
      icon - Icon of the action.
    • setSmallIcon

      public void setSmallIcon(@CheckForNull Icon icon)
      Sets the small icon of the action.
      Parameters:
      icon - the small icon of the action.
    • getSmallIcon

      @CheckForNull public Icon getSmallIcon()
      Returns the small icon of the action.
      Returns:
      the small icon of the action.
    • setTinyIcon

      public void setTinyIcon(@CheckForNull Icon icon)
      Sets the tiny icon of the action.
      Parameters:
      icon - the tiny icon of the action
    • getTinyIcon

      @CheckForNull public Icon getTinyIcon()
      Returns the tiny icon of the action.
      Returns:
      the tiny icon of the action.
    • getIcon

      @Deprecated @CheckForNull public Icon getIcon()
      Deprecated.
      use {link #getSmallOrLargeIcon}
    • setLargeIcon

      public void setLargeIcon(@CheckForNull Icon icon)
      Sets the large icon of the action.
      Parameters:
      icon - the large icon of the action.
    • getLargeIcon

      @CheckForNull public Icon getLargeIcon()
      Returns the large icon of the action.
      Returns:
      the large icon of the action.
    • getLargeOrSmallIcon

      @CheckForNull public Icon getLargeOrSmallIcon()
      Returns:
      large icon if present. If not, small one
    • getSmallOrLargeIcon

      @CheckForNull public Icon getSmallOrLargeIcon()
      Returns:
      small icon if present. If not, large one
    • equals

      public boolean equals(Object o)
      Checks if given object is equal to the current action.
      Overrides:
      equals in class 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 class Object
      Returns:
      hash code of action.
    • toString

      public String toString()
      toString returns the action's name.
      Overrides:
      toString in class Object
      Returns:
      the name.
    • actionPerformed

      public abstract void actionPerformed(@CheckForNull ActionEvent e)
      Executes given action.
      Specified by:
      actionPerformed in interface ActionListener
      Parameters:
      e - event caused execution.
    • compareTo

      public int compareTo(Object o)
      Compares the name of the action. Uses String.compareTo method.
      Specified by:
      compareTo in interface 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 class 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 String description)
      Sets new action description.
      Parameters:
      description - action description.
    • getDescription

      public String getDescription()
      Returns description of this action.
      Returns:
      action description.
    • setGroup

      protected void setGroup(@CheckForNull String group)
      Sets new group for this action. Empty string will be converted to null.
      Parameters:
      group - group name.
    • getGroup

      public String getGroup()
      Returns the name of the related actions group.
      Returns:
      the name of the related actions group.
    • getActions

      public List<NMAction> getActions()
      Returns the list of the actions.
      Returns:
      empty list here. Some derived classes may override this method.
    • forEach

      public void forEach(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(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(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(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(PropertyChangeListener listener)
      Adds a PropertyChangeListener 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, or setForeground. 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 interface Action
      Overrides:
      addPropertyChangeListener in class AbstractAction
      Parameters:
      listener - The PropertyChangeListener to be added
      See Also:
    • addWeakPropertyChangeListener

      public void addWeakPropertyChangeListener(PropertyChangeListener listener)
      Adds a PropertyChangeListener 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, or setForeground. 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 - The PropertyChangeListener to be added
      See Also:
    • firePropertyChange

      protected void firePropertyChange(String propertyName, @CheckForNull Object oldValue, @CheckForNull Object newValue)
      Supports reporting bound property changes. This method can be called when a bound property has changed and it will send the appropriate PropertyChangeEvent to any registered PropertyChangeListeners.
      Overrides:
      firePropertyChange in class AbstractAction
    • removePropertyChangeListener

      public void removePropertyChangeListener(PropertyChangeListener listener)
      Removes a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties.
      Specified by:
      removePropertyChangeListener in interface Action
      Overrides:
      removePropertyChangeListener in class AbstractAction
      Parameters:
      listener - the PropertyChangeListener to be removed
      See Also:
    • removeWeakPropertyChangeListener

      public void removeWeakPropertyChangeListener(PropertyChangeListener listener)
      Removes a PropertyChangeListener from the weak listener list. This removes a PropertyChangeListener that was registered for all properties.
      Parameters:
      listener - the PropertyChangeListener to be removed
      See Also:
    • createMenuItem

      @CheckForNull public 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 PropertyChangeListener[] getPropertyChangeListeners()
      Returns an array of all the PropertyChangeListeners added to this action with addPropertyChangeListener().
      Overrides:
      getPropertyChangeListeners in class AbstractAction
      Returns:
      all of the PropertyChangeListeners added or an empty array if no listeners have been added
    • hasMenuShortcutMask

      public static boolean hasMenuShortcutMask(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 String getMenuShortcutMaskAsString()
      Returns:
      representation of menu shortcut modifier. Representation can be used in various UI messages.