--- title: Action System --- ## 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`](upsource:///platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java) 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 many of the standard IDEA actions are defined in the [IdeActions](upsource:///platform/platform-api/src/com/intellij/openapi/actionSystem/IdeActions.java) 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`](upsource:///platform/platform-api/src/com/intellij/openapi/actionSystem/ActionPlaces.java) interface. For every place where the action appears, a new [`Presentation`](upsource:///platform/platform-api/src/com/intellij/ide/presentation/Presentation.java) 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 [`AnActionEvent`](upsource:///platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnActionEvent.java) object 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 [`CommonDataKeys`](upsource:///platform/editor-ui-api/src/com/intellij/openapi/actionSystem/CommonDataKeys.java)class. The `AnActionEvent` instance is also passed to the `actionPerformed` method. For a step-by-step walkthrough of defining actions, please check out the [action system tutorial](/tutorials/action_system.md). ## Registering Actions There are two main ways to register an action: either by listing it in the `` 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 `` section, and describes the meaning of each element. ```xml ``` ## 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](upsource:///platform/editor-ui-api/src/com/intellij/openapi/actionSystem/ActionManager.java) 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](upsource:///platform/platform-api/src/com/intellij/openapi/actionSystem/DefaultActionGroup.java) class. You can create a plugin that registers actions on IDEA startup using the following procedure. **To register an action on IDEA startup** * Create a new class that implements the [`ApplicationComponent`](upsource:///platform/core-api/src/com/intellij/openapi/components/ApplicationComponent.java) interface. * In this class, override the `getComponentName`, `initComponent`, and `disposeComponent` methods. * Register this class in the `` section of the plugin.xml file. To clarify the above procedure, consider the following sample Java class `MyPluginRegistration` that registers an action defined in a custom `TextBoxes` class and adds a new menu command to the *Window* menu group on the main menu: ```java public class MyPluginRegistration implements ApplicationComponent { // Returns the component name (any unique string value). @NotNull public String getComponentName() { return "MyPlugin"; } // If you register the MyPluginRegistration class in the section of // the plugin.xml file, this method is called on IDEA start-up. public void initComponent() { ActionManager am = ActionManager.getInstance(); TextBoxes action = new TextBoxes(); // Passes an instance of your custom TextBoxes class to the registerAction method of the ActionManager class. am.registerAction("MyPluginAction", action); // Gets an instance of the WindowMenu action group. DefaultActionGroup windowM = (DefaultActionGroup) am.getAction("WindowMenu"); // Adds a separator and a new menu command to the WindowMenu group on the main menu. windowM.addSeparator(); windowM.add(action); } // Disposes system resources. public void disposeComponent() { } } ``` Note, that the sample `TextBoxes` class is described in [Getting Started with Plugin Development](/basics/getting_started.md). To ensure that your plugin is initialized on IDEA start-up, make the following changes to the `` section of the `plugin.xml` file: ```xml MypackageName.MyPluginRegistration ``` ## 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`](upsource:///platform/editor-ui-api/src/com/intellij/openapi/actionSystem/ActionPopupMenu.java) and [`ActionToolbar`](upsource:///platform/editor-ui-api/src/com/intellij/openapi/actionSystem/ActionToolbar.java) 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. If your action toolbar is attached to a specific component (for example, a panel in a tool window), you usually need to call `ActionToolbar.setTargetComponent()` and pass the instance of the related component as a parameter. This ensures that the state of the toolbar buttons depends on the state of the related component, and not on the current focus location within the IDE frame.