org.openide.util.actions
Class SystemAction

java.lang.Object
  extended by org.openide.util.SharedClassObject
      extended by org.openide.util.actions.SystemAction
All Implemented Interfaces:
java.awt.event.ActionListener, java.io.Externalizable, java.io.Serializable, java.util.EventListener, javax.swing.Action, HelpCtx.Provider
Direct Known Subclasses:
BooleanStateAction, CallableSystemAction

public abstract class SystemAction
extends SharedClassObject
implements javax.swing.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.

Author:
Ian Formanek, Jaroslav Tulach
See Also:
Serialized Form

Field Summary
static java.lang.String PROP_ENABLED
          Name of property indicating whether or not the action is enabled.
static java.lang.String PROP_ICON
          Name of property for the action's display icon.
 
Fields inherited from interface javax.swing.Action
ACCELERATOR_KEY, ACTION_COMMAND_KEY, DEFAULT, LONG_DESCRIPTION, MNEMONIC_KEY, NAME, SHORT_DESCRIPTION, SMALL_ICON
 
Constructor Summary
SystemAction()
           
 
Method Summary
abstract  void actionPerformed(java.awt.event.ActionEvent ev)
          Actually perform the action.
protected  boolean clearSharedData()
          Indicate whether action state should be cleared after the last action of this class is deleted.
static javax.swing.JPopupMenu createPopupMenu(SystemAction[] actions)
          Deprecated. Use Utilities.actionsToPopup(javax.swing.Action[], org.openide.util.Lookup)
static javax.swing.JToolBar createToolbarPresenter(SystemAction[] actions)
          Create the default toolbar representation of an array of actions.
static
<T extends SystemAction>
T
get(java.lang.Class<T> actionClass)
          Obtain a singleton instance of the action with a specified class.
abstract  HelpCtx getHelpCtx()
          Get a help context for the action.
 javax.swing.Icon getIcon()
          Get the action's display icon.
 javax.swing.Icon getIcon(boolean createLabel)
          Get the action's display icon, possibly creating a text label.
abstract  java.lang.String getName()
          Get a human presentable name of the action.
 java.lang.Object getValue(java.lang.String name)
          Get a property in the singleton.
protected  java.lang.String iconResource()
          Specify the proper resource name for the action's icon.
protected  void initialize()
          Initialize the action.
 boolean isEnabled()
          Test whether the action is currently enabled.
static SystemAction[] linkActions(SystemAction[] actions1, SystemAction[] actions2)
          Concatenate two arrays of actions.
 void putValue(java.lang.String name, java.lang.Object value)
          Set a property in the singleton.
 void setEnabled(boolean value)
          Set whether the action should be enabled.
 void setIcon(javax.swing.Icon icon)
          Set the action's display icon.
 
Methods inherited from class org.openide.util.SharedClassObject
addNotify, addPropertyChangeListener, equals, finalize, findObject, findObject, firePropertyChange, getLock, getProperty, hashCode, putProperty, putProperty, readExternal, removeNotify, removePropertyChangeListener, reset, writeExternal, writeReplace
 
Methods inherited from class java.lang.Object
clone, getClass, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.Action
addPropertyChangeListener, removePropertyChangeListener
 

Field Detail

PROP_ENABLED

public static final java.lang.String PROP_ENABLED
Name of property indicating whether or not the action is enabled.

See Also:
Constant Field Values

PROP_ICON

public static final java.lang.String PROP_ICON
Name of property for the action's display icon.

See Also:
Constant Field Values
Constructor Detail

SystemAction

public SystemAction()
Method Detail

get

public static <T extends SystemAction> T get(java.lang.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:
java.lang.ClassCastException - if the class is not SystemAction
java.lang.IllegalArgumentException - if the instance cannot be created

getName

public abstract java.lang.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 javax.swing.Action
Returns:
true if so

setEnabled

public void setEnabled(boolean value)
Set whether the action should be enabled.

Specified by:
setEnabled in interface javax.swing.Action
Parameters:
value - true to enable it

putValue

public final void putValue(java.lang.String name,
                           java.lang.Object value)
Set a property in the singleton. This property is common for all instances of the same class.

Specified by:
putValue in interface javax.swing.Action
Parameters:
name - the name of the property
value - the value

getValue

public final java.lang.Object getValue(java.lang.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 javax.swing.Action
Parameters:
name - the name of the property
Returns:
the value

actionPerformed

public abstract void actionPerformed(java.awt.event.ActionEvent ev)
Actually perform the action. Specified in ActionListener.actionPerformed(java.awt.event.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 java.awt.event.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(javax.swing.Icon icon)
Set the action's display icon.

Parameters:
icon - the icon

getIcon

public final javax.swing.Icon getIcon()
Get the action's display icon.

Returns:
the icon
Throws:
java.lang.IllegalStateException - if an icon could not be created

getIcon

public final javax.swing.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:
java.lang.IllegalStateException - if an icon could not be created

iconResource

protected java.lang.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 javax.swing.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 javax.swing.JPopupMenu createPopupMenu(SystemAction[] actions)
Deprecated. Use Utilities.actionsToPopup(javax.swing.Action[], org.openide.util.Lookup)

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