Child pages
  • IntelliJ IDEA Action System
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Executing and Updating Actions

The system of actions allows plugins to add their own items to IDEA menus and toolbars. An action is a class, derived from the AnAction class, whose actionPerformed method is called when the menu item or toolbar button is selected. For example, one of the action classes is responsible for the "File | Open File..." menu item and for the "Open File" toolbar button.

Actions are organized into groups, which, in turn, can contain other groups. A group of actions can form a toolbar or a menu. Subgroups of the group can form submenus of the menu.

Every action and action group has an unique identifier. Identifiers of standard IDEA actions are defined in the IdeActions class.

Every action can be included in multiple groups, and thus appear in multiple places within the IDEA user interface. Different places where actions can appear are defined by constants in the ActionPlaces interface. For every place where the action appears, a new Presentation is created. Thus, the same action can have different text or icons when it appears in different places of the user interface. Different presentations for the action are created by copying the presentation returned by the AnAction.getTemplatePresentation() method.

To update the state of the action, the method AnAction.update() is periodically called by IDEA. The object of type AnActionEvent passed to this method carries the information about the current context for the action, and in particular, the specific presentation which needs to be updated.

To retrieve the information about the current state of the IDE, including the active project, the selected file, the selection in the editor and so on, the method AnActionEvent.getData() can be used. Different data keys that can be passed to that method are defined in the DataKeys class.

The AnActionEvent instance is also passed to the actionPerformed method.

Registering Actions

There are two main ways to register an action: either by listing it in the <actions> section of the plugin.xml file, or through Java code.

Registering Actions in plugin.xml

Registering actions in plugin.xml is demonstrated in the following example. The example section of plugin.xml demonstrates all elements which can be used in the <actions> section, and describes the meaning of each element.

Registering Actions from Code

To register an action from code, two steps are required. First, an instance of the class derived from AnAction must be passed to the registerAction method of the ActionManager class, to associate the action with an ID. Second, the action needs to be added to one or more groups. To get an instance of an action group by ID, it is necessary to call ActionManager.getAction() and cast the returned value to the DefaultActionGroup class.

Building UI from Actions

If a plugin needs to include a toolbar or popup menu built from a group of actions in its own user interface, that can be accomplished through the ActionPopupMenu and ActionToolbar classes. These objects can be created through calls to ActionManager.createActionPopupMenu and ActionManager.createActionToolbar. To get a Swing component from such an object, simply call the getComponent() method.

  • No labels