Class SystemAction

java.lang.Object
org.openide.util.SharedClassObject
org.openide.util.actions.SystemAction
All Implemented Interfaces:
ActionListener, Externalizable, Serializable, EventListener, Action, HelpCtx.Provider
Direct Known Subclasses:
BooleanStateAction, CallableSystemAction

public abstract class SystemAction extends SharedClassObject implements Action, HelpCtx.Provider
A base class for user-visible actions.

Implements the Swing Action interface to enable use with the Swing action model.

An action class is a singleton, i.e. should generally contain no instance state. Rather, subclassing and use of abstract protected methods should be used to create variants of the action.

While it is possible to subclass this class directly--for example, if your "action" is really a placeholder for a popup menu that shows other actions--most people will prefer to use one of the subclasses, which are more convenient.

See Also:
  • Field Details

    • PROP_ENABLED

      public static final String PROP_ENABLED
      Name of property indicating whether or not the action is enabled.
      See Also:
    • PROP_ICON

      public static final String PROP_ICON
      Name of property for the action's display icon.
      See Also:
  • Constructor Details

    • SystemAction

      public SystemAction()
  • Method Details

    • get

      public static <T extends SystemAction> T get(Class<T> actionClass)
      Obtain a singleton instance of the action with a specified class. If there already is a instance then it is returned, otherwise a new one is created.
      Parameters:
      actionClass - the class of the action to find
      Returns:
      the singleton action instance
      Throws:
      ClassCastException - if the class is not SystemAction
      IllegalArgumentException - if the instance cannot be created
    • getName

      public abstract String getName()
      Get a human presentable name of the action. This may be presented as an item in a menu.

      Using the normal menu presenters, an included ampersand before a letter will be treated as the name of a mnemonic.

      Returns:
      the name of the action
    • getHelpCtx

      public abstract HelpCtx getHelpCtx()
      Get a help context for the action.
      Specified by:
      getHelpCtx in interface HelpCtx.Provider
      Returns:
      the help context for this action
    • isEnabled

      public boolean isEnabled()
      Test whether the action is currently enabled.
      Specified by:
      isEnabled in interface Action
      Returns:
      true if so
    • setEnabled

      public void setEnabled(boolean value)
      Set whether the action should be enabled.
      Specified by:
      setEnabled in interface Action
      Parameters:
      value - true to enable it
    • putValue

      public final void putValue(String name, Object value)
      Set a property in the singleton. This property is common for all instances of the same class.
      Specified by:
      putValue in interface Action
      Parameters:
      name - the name of the property
      value - the value
    • getValue

      public final Object getValue(String name)
      Get a property in the singleton. Values are shared among all instances of the same class. The special tokens Action.NAME, Action.SMALL_ICON and "iconBase" are also recognized and delegated to getName(), getIcon() and iconResource() resp.
      Specified by:
      getValue in interface Action
      Parameters:
      name - the name of the property
      Returns:
      the value
    • actionPerformed

      public abstract void actionPerformed(ActionEvent ev)
      Actually perform the action. Specified in ActionListener.actionPerformed(ActionEvent).

      In some cases, the implementation may have an empty body, if the presenters handle the performing of the action in a different way than by calling this method.

      Since 4.11, will be performed directly in the event thread.

      Specified by:
      actionPerformed in interface ActionListener
      Parameters:
      ev - the event triggering the action
    • initialize

      protected void initialize()
      Initialize the action. The default implementation just enabled it.
      Overrides:
      initialize in class SharedClassObject
    • clearSharedData

      protected boolean clearSharedData()
      Indicate whether action state should be cleared after the last action of this class is deleted.
      Overrides:
      clearSharedData in class SharedClassObject
      Returns:
      false in the default implementation
    • setIcon

      public final void setIcon(Icon icon)
      Set the action's display icon.
      Parameters:
      icon - the icon
    • getIcon

      public final Icon getIcon()
      Get the action's display icon.
      Returns:
      the icon
      Throws:
      IllegalStateException - if an icon could not be created
    • getIcon

      public final Icon getIcon(boolean createLabel)
      Get the action's display icon, possibly creating a text label.
      Parameters:
      createLabel - if true, create a textual icon if otherwise there would be none; if false, create a blank icon
      Returns:
      an icon
      Throws:
      IllegalStateException - if an icon could not be created
    • iconResource

      protected String iconResource()
      Specify the proper resource name for the action's icon. May be overridden by subclasses; the default is to have no icon. Typically this should be a 16x16 color GIF. Do not use relative paths nor an initial slash. If e.g. myIcon.gif is accompanied with myIcon_pressed.gif, myIcon_disabled.gif and/or myIcon_rollover.gif these images are used to call methods on JButton.setPressedIcon(), JButton.setDisabledIcon() and/or JButton.setRolloverIcon() with appropriate images. Please check Actions.connect for additional info how this is achieved (using special "iconBase" key for getValue). As of APIs version 3.24, this path will be used for a localized search automatically. If you do not want an icon, do not override this to return a blank icon. Leave it null, but call putValue("noIconInMenu", Boolean.TRUE) to make sure that no extra space is allotted for an icon in the menu item.
      Returns:
      the resource name for the icon, e.g. com/mycom/mymodule/myIcon.gif; or null to have no icon (make a text label)
    • createToolbarPresenter

      public static JToolBar createToolbarPresenter(SystemAction[] actions)
      Create the default toolbar representation of an array of actions. Null items in the array will add a separator to the toolbar.
      Parameters:
      actions - actions to show in the generated toolbar
      Returns:
      a toolbar instance displaying them
    • linkActions

      public static SystemAction[] linkActions(SystemAction[] actions1, SystemAction[] actions2)
      Concatenate two arrays of actions.
      Parameters:
      actions1 - first array of actions to link
      actions2 - second array of actions to link
      Returns:
      an array of both sets of actions in the same order
    • createPopupMenu

      @Deprecated public static JPopupMenu createPopupMenu(SystemAction[] actions)
      Create the default popup menu representation of an array of actions.
      Parameters:
      actions - actions to show in the generated menu
      Returns:
      a popup menu displaying them