Class 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 GENERATED_ID_PREFIX  
      static java.lang.String GROUP
      Key for storing group.
      static java.lang.String ID
      Key for storing id.
      private static com.nomagic.utils.Counter ID_GENERATOR
      The id generator for actions without id.
      static java.lang.String LARGE_ICON
      Key for storing large icon.
      static int MENU_SHORTCUT_MASK
      Menu shortcut mask used to create shortcuts.
      private com.nomagic.utils.ExtendedPropertyChangeSupport support
      Property change support.
      static java.lang.String TINY_ICON
      Key for storing tiny icon (smaller than small icon).
      private com.nomagic.utils.WeakExtendedPropertyChangeSupport weakSupport
      Property change support for weak property change listeners This support allows adding/removing property change listeners of this action.
      • Fields inherited from class javax.swing.AbstractAction

        changeSupport, enabled
      • Fields inherited from interface javax.swing.Action

        ACCELERATOR_KEY, ACTION_COMMAND_KEY, DEFAULT, DISPLAYED_MNEMONIC_INDEX_KEY, LARGE_ICON_KEY, LONG_DESCRIPTION, MNEMONIC_KEY, NAME, SELECTED_KEY, SHORT_DESCRIPTION, 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 a PropertyChangeListener 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 a PropertyChangeListener to the weak listener list.
      private static java.lang.String checkID​(java.lang.String id)
      Method generates id if it is null or empty.
      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 the PropertyChangeListeners 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 modifier
      boolean 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 predicate
      void removePropertyChangeListener​(java.beans.PropertyChangeListener listener)
      Removes a PropertyChangeListener from the listener list.
      void removeShortcut​(javax.swing.KeyStroke key)
      Removes the shortcut from the action.
      void removeWeakPropertyChangeListener​(java.beans.PropertyChangeListener listener)
      Removes a PropertyChangeListener 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.NAME
      void 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 itself
      java.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
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface javax.swing.Action

        accept
    • 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
      • ID_GENERATOR

        private static final com.nomagic.utils.Counter ID_GENERATOR
        The id generator for actions without id.
      • ACTION_SHORTCUTS

        public static final java.lang.String ACTION_SHORTCUTS
        Key for storing command keys.
        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
      • support

        @CheckForNull
        private com.nomagic.utils.ExtendedPropertyChangeSupport support
        Property change support. This support allows adding/removing property change listeners of this action.
      • weakSupport

        @CheckForNull
        private com.nomagic.utils.WeakExtendedPropertyChangeSupport weakSupport
        Property change support for weak property change listeners This support allows adding/removing property change listeners of this action.
    • 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 action
        group - 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

      • checkID

        private static java.lang.String checkID​(@CheckForNull
                                                java.lang.String id)
        Method generates id if it is null or empty.
        Parameters:
        id - id to check.
        Returns:
        String generated or original id.
      • 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. Use addShortcut(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 class java.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 class java.lang.Object
        Returns:
        hash code of action.
      • toString

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

        public abstract void actionPerformed​(@CheckForNull
                                             java.awt.event.ActionEvent e)
        Executes given action.
        Specified by:
        actionPerformed in interface java.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 interface java.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 class javax.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 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 javax.swing.Action
        Overrides:
        addPropertyChangeListener in class javax.swing.AbstractAction
        Parameters:
        listener - The PropertyChangeListener to be added
        See Also:
        Action.addPropertyChangeListener(java.beans.PropertyChangeListener)
      • addWeakPropertyChangeListener

        public void addWeakPropertyChangeListener​(java.beans.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:
        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 appropriate PropertyChangeEvent to any registered PropertyChangeListeners.
        Overrides:
        firePropertyChange in class javax.swing.AbstractAction
      • removePropertyChangeListener

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

        public void removeWeakPropertyChangeListener​(java.beans.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:
        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 the PropertyChangeListeners added to this action with addPropertyChangeListener().
        Overrides:
        getPropertyChangeListeners in class javax.swing.AbstractAction
        Returns:
        all of the PropertyChangeListeners 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.