IntelliJ Action System. ========== This tutorial is meant to give general information about the IntelliJ IDEA Action System and lead you through a series of steps which show how to create, register, and customize custom actions and action groups. Action system provides an option to handle certain events in a desired way. Action can either be simply a response to some state, or be bound to UI element and could be invoked on demand. These UI elements include main menu, context menus and toolbars. ---------------- **TODO - links to source** #Working with custom actions. An action is technically a class, derived from the [AnAction] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java) class. To update the state of the action, the method AnAction.update() is periodically called by IDEA. The object of type [AnActionEvent] (https://github.com/JetBrains/intellij-community/blob/ff16ce78a1e0ddb6e67fd1dbc6e6a597e20d483a/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnActionEvent.java) 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. ##Creating actions. To create a new we need to extend [AnAction] () class public class SimpleAction extends AnAction { } The only method of an inheritor of [AnAction] () which needs to be overridden is ```public void actionPerformed(AnActionEvent anActionEvent);``` , and it should contain a part of code to be executed after the action has been invoked. public class SimpleAction extends AnAction { @Override public void actionPerformed(AnActionEvent anActionEvent) { } } ##Registering actions. To register a newly created action, attribute should be added to the section of the plugin configuration file [plugin.xml] (). IntelliJ IDEA has an embedded inspection that spots unregistered actions. !["Action never used" inspection](img/action_never_used.png) To register the action and set up it's attributes press ***Alt + Enter*** while the caret is placed on the action's declaration. !["Register action" quick fix](img/action_never_used.png) Fill the "New Action" form to set up action's parameters such as: action's name and description, a UI component the action is bound to, visual position of the menu item the action is bound to, and a shortcut for invoking the action. In our case the action will be available in the Tools Menu, it will be placed on top, and will have no shortcuts. !["Register action" quick fix](img/new_action.png) After filling the "New Action" form and applying the changes ** section of our [plugin.xml]() file will look like this: ------------ Under construction TODO: rewrite accordingly to the guidelines/review [Link to source code] (https://github.com/JetBrains/intellij-sdk/blob/master/code_samples/plugin_sample/META-INF/plugin.xml) ----------- #How to make an action available and visible? You need to override AnAction.update Default implementation of this method 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). This method can be called frequently, for instance, if an action is added to a toolbar, it will be updated twice a second. This means that this method is supposed to work really fast, no real work should be done at this phase. For example, checking selection in a tree or a list, is considered valid, but working with a file system is not. If you cannot understand the state of the action fast you should do it in the [AnActionEvent] (https://github.com/JetBrains/intellij-community/blob/ff16ce78a1e0ddb6e67fd1dbc6e6a597e20d483a/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnActionEvent.java) method and notify the user that action cannot be executed if it's the case. Parameter e carries information on the invocation place and data available public class SimpleAction extends AnAction { @Override public void actionPerformed(@NotNull AnActionEvent anActionEvent) { } @Override public void update(@NotNull AnActionEvent e) { //Make action visible and available only when project is defined final Project project = e.getProject(); boolean isAvailable = project != null; e.getPresentation().setVisible(isAvailable); e.getPresentation().setEnabled(isAvailable); } } [Link to source code] (https://github.com/JetBrains/intellij-sdk/blob/master/code_samples/plugin_sample/src/org/jetbrains/plugins/sample/SimpleAction.java) ------------- #How to create a custom group of actions? If some part of the functionality requires to implement several actions or actions are simply too many and overload the menu they can be joined into groups. In this case the group will be available as a top-level menu item, action will be represented as drop-down menu items. Grouping can be done by placing attribute into [plugin.xml] (https://github.com/JetBrains/intellij-sdk/blob/master/code_samples/plugin_sample/META-INF/plugin.xml) file. In most of the cases you simply need to leave "class" attribute of the undefined; in this case an instance of [DefaultActionGroup] (https://github.com/JetBrains/intellij-community/blob/master/platform/platform-api/src/com/intellij/openapi/actionSystem/DefaultActionGroup.java) will be created and filled with actions defined within it. See [GroupedToDefaultAction.java] (https://github.com/JetBrains/intellij-sdk/blob/master/code_samples/plugin_sample/src/org/jetbrains/plugins/sample/actions/GroupedToDefaultAction.java) If set of actions, which need to be put into a group, is defined dynamically depending on the context, a custom action group can be created by setting *class* attribute. The class itself, like [DummyActionGroup] (https://github.com/JetBrains/intellij-sdk/blob/master/code_samples/plugin_sample/src/org/jetbrains/plugins/sample/DummyActionGroup.java) in this example, should be derived from [ActionGroup] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/ActionGroup.java) and its [getChildren()](https://github.com/JetBrains/intellij-sdk/blob/master/code_samples/plugin_sample/src/org/jetbrains/plugins/sample/DummyActionGroup.java) method should return an array of [actions] (https://github.com/JetBrains/intellij-sdk/blob/master/code_samples/plugin_sample/src/org/jetbrains/plugins/sample/GroupedAction.java) belonging to this group. -------------- #AnActionEvent [AnActionEvent.java] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnActionEvent.java) contains the information necessary to execute or update an [action] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java), such as [DataContext] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/DataContext.java), [Project] (https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/openapi/project/Project.java), and other instances related to the action execution environment. ------------- #DataContext Class [DataContext.java] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/DataContext.java) allows an action to retrieve information about the context in which it was invoked. It's only method ```@Nullable Object getData(@NonNls String dataId);``` returns an object corresponding to the specified data identifier. Some of the supported data identifiers are defined in class [PlatformDataKeys.java] (https://github.com/JetBrains/intellij-community/blob/master/platform/platform-api/src/com/intellij/openapi/actionSystem/PlatformDataKeys.java)