com.intellij.openapi.actionSystem
Class AnAction

java.lang.Object
  extended by com.intellij.openapi.actionSystem.AnAction
Direct Known Subclasses:
ActionGroup, ActionStub, CloseAction, ComboBoxAction, ComponentWithBrowseButton.MyDoClickAction, DataSourceAddFromServerAction, DefaultExecutionResult.StopAction, EditorAction, IncludeExcludeActionBase, InsertPathAction, ReorderableListController.CustomActionDescription.BaseAction, Separator, ToggleAction, TreeCollapseAllActionBase, TreeExpandAllActionBase

public abstract class AnAction
extends java.lang.Object

Represents an entity that has a state, a presentation and can be performed. For an action to be useful, you need to implement actionPerformed(com.intellij.openapi.actionSystem.AnActionEvent) and optionally to override update(com.intellij.openapi.actionSystem.AnActionEvent). By overriding the update(com.intellij.openapi.actionSystem.AnActionEvent) method you can dynamically change action's presentation depending on the place (for more information on places see ActionPlaces. The same action can have various presentations.

  public class MyAction extends AnAction {
    public MyAction() {
      // ...
    }

    public void update(AnActionEvent e) {
      Presentation presentation = e.getPresentation();
      if (e.getPlace().equals(ActionPlaces.MAIN_MENU)) {
        presentation.setText("My Menu item name");
      } else if (e.getPlace().equals(ActionPlaces.MAIN_TOOLBAR)) {
        presentation.setText("My Toolbar item name");
      }
    }

    public void actionPerformed(AnActionEvent e) { ... }
  }
 

See Also:
AnActionEvent, Presentation, ActionPlaces

Field Summary
static AnAction[] EMPTY_ARRAY
           
static java.lang.String ourClientProperty
           
 
Constructor Summary
AnAction()
          Creates a new action with its text, description and icon set to null.
AnAction(java.lang.String text)
          Creates a new action with the specified text.
AnAction(java.lang.String text, java.lang.String description, javax.swing.Icon icon)
          Constructs a new action with the specified text, description and icon.
 
Method Summary
abstract  void actionPerformed(AnActionEvent e)
          Implement this method to provide your action handler.
 void copyFrom(AnAction sourceAction)
          Copies template presentation and shortcuts set from sourceAction.
static java.lang.String createTooltipText(java.lang.String s, AnAction action)
           
 boolean displayTextInToolbar()
          Override with true returned if your action has to display its text along with the icon when placed in the toolbar
 ShortcutSet getShortcutSet()
          Returns the shortcut set associated with this action.
 Presentation getTemplatePresentation()
          Returns a template presentation that will be used as a template for created presentations.
 boolean isDefaultIcon()
          Returns true if the action has an internal, not user-customized icon.
 boolean isEnabledInModalContext()
           
 void registerCustomShortcutSet(ShortcutSet shortcutSet, javax.swing.JComponent component)
          Registers a set of shortcuts that will be processed when the specified component is the ancestor of focused component.
 void setDefaultIcon(boolean isDefaultIconSet)
          Sets the flag indicating whether the action has an internal or a user-customized icon.
protected  void setEnabledInModalContext(boolean enabledInModalContext)
           
protected  void setShortcutSet(ShortcutSet shortcutSet)
           
 void unregisterCustomShortcutSet(javax.swing.JComponent component)
           
 void update(AnActionEvent e)
          Updates the state of the action.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_ARRAY

public static final AnAction[] EMPTY_ARRAY

ourClientProperty

public static final java.lang.String ourClientProperty
See Also:
Constant Field Values
Constructor Detail

AnAction

public AnAction()
Creates a new action with its text, description and icon set to null.


AnAction

public AnAction(java.lang.String text)
Creates a new action with the specified text. Description and icon are set to null.

Parameters:
text - Serves as a tooltip when the presention is a button and the name of the menu item when the presentation is a menu item.

AnAction

public AnAction(java.lang.String text,
                java.lang.String description,
                javax.swing.Icon icon)
Constructs a new action with the specified text, description and icon.

Parameters:
text - Serves as a tooltip when the presention is a button and the name of the menu item when the presentation is a menu item
description - Describes current action, this description will appear on the status bar when presentation has focus
icon - Action's icon
Method Detail

getShortcutSet

public final ShortcutSet getShortcutSet()
Returns the shortcut set associated with this action.

Returns:
shorcut set associated with this action

registerCustomShortcutSet

public final void registerCustomShortcutSet(ShortcutSet shortcutSet,
                                            javax.swing.JComponent component)
Registers a set of shortcuts that will be processed when the specified component is the ancestor of focused component. Note that the action doesn't have to be registered in action manager in order for that shorcut to work.

Parameters:
shortcutSet - the shortcuts for the action.
component - the component for which the shortcuts will be active.

unregisterCustomShortcutSet

public final void unregisterCustomShortcutSet(javax.swing.JComponent component)

copyFrom

public final void copyFrom(AnAction sourceAction)
Copies template presentation and shortcuts set from sourceAction.

Parameters:
sourceAction - cannot be null

isEnabledInModalContext

public final boolean isEnabledInModalContext()

setEnabledInModalContext

protected final void setEnabledInModalContext(boolean enabledInModalContext)

displayTextInToolbar

public boolean displayTextInToolbar()
Override with true returned if your action has to display its text along with the icon when placed in the toolbar


update

public void update(AnActionEvent e)
Updates the state of the action. Default implementation does nothing. Override this method to provide the ability to dynamically change action's state and(or) presentation depending on the context (For example when your action state depends on the selection you can check for selection and change the state accordingly).

Parameters:
e - Carries information on the invocation place and data available

getTemplatePresentation

public final Presentation getTemplatePresentation()
Returns a template presentation that will be used as a template for created presentations.

Returns:
template presentation

actionPerformed

public abstract void actionPerformed(AnActionEvent e)
Implement this method to provide your action handler.

Parameters:
e - Carries information on the invocation place

setShortcutSet

protected void setShortcutSet(ShortcutSet shortcutSet)

createTooltipText

public static java.lang.String createTooltipText(java.lang.String s,
                                                 AnAction action)

setDefaultIcon

public void setDefaultIcon(boolean isDefaultIconSet)
Sets the flag indicating whether the action has an internal or a user-customized icon.

Parameters:
isDefaultIconSet - true if the icon is internal, false if the icon is customized by the user.

isDefaultIcon

public boolean isDefaultIcon()
Returns true if the action has an internal, not user-customized icon.

Returns:
true if the icon is internal, false if the icon is customized by the user.