mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-29 17:57:53 +08:00
Consolidate to 1 Actions Tutorial
This commit is contained in:
parent
d9a7328f08
commit
f97a69a193
@ -24,7 +24,6 @@
|
|||||||
* [Using DevKit](basics/getting_started/using_dev_kit.md)
|
* [Using DevKit](basics/getting_started/using_dev_kit.md)
|
||||||
* [Setting Up a Development Environment](basics/getting_started/setting_up_environment.md)
|
* [Setting Up a Development Environment](basics/getting_started/setting_up_environment.md)
|
||||||
* [Creating a Plugin Project](basics/getting_started/creating_plugin_project.md)
|
* [Creating a Plugin Project](basics/getting_started/creating_plugin_project.md)
|
||||||
* [Creating an Action](basics/getting_started/creating_an_action.md)
|
|
||||||
* [Running and Debugging a Plugin](basics/getting_started/running_and_debugging_a_plugin.md)
|
* [Running and Debugging a Plugin](basics/getting_started/running_and_debugging_a_plugin.md)
|
||||||
* [Deploying a Plugin](basics/getting_started/deploying_plugin.md)
|
* [Deploying a Plugin](basics/getting_started/deploying_plugin.md)
|
||||||
* [Publishing a Plugin](basics/getting_started/publishing_plugin.md)
|
* [Publishing a Plugin](basics/getting_started/publishing_plugin.md)
|
||||||
|
@ -36,7 +36,7 @@ The Gradle workflow offers these advantages:
|
|||||||
* [Developing plugins using DevKit](getting_started/using_dev_kit.md)
|
* [Developing plugins using DevKit](getting_started/using_dev_kit.md)
|
||||||
* [Setting Up a Development Environment](getting_started/setting_up_environment.md)
|
* [Setting Up a Development Environment](getting_started/setting_up_environment.md)
|
||||||
* [Creating a Plugin Project](getting_started/creating_plugin_project.md)
|
* [Creating a Plugin Project](getting_started/creating_plugin_project.md)
|
||||||
* [Creating an Action](getting_started/creating_an_action.md)
|
* [Creating an Action](/tutorials/action_system/working_with_custom_actions.md)
|
||||||
* [Running and Debugging a Plugin](getting_started/running_and_debugging_a_plugin.md)
|
* [Running and Debugging a Plugin](getting_started/running_and_debugging_a_plugin.md)
|
||||||
* [Deploying a Plugin](getting_started/deploying_plugin.md)
|
* [Deploying a Plugin](getting_started/deploying_plugin.md)
|
||||||
* [Publishing a plugin to plugin repository](getting_started/publishing_plugin.md)
|
* [Publishing a plugin to plugin repository](getting_started/publishing_plugin.md)
|
||||||
|
@ -1,102 +0,0 @@
|
|||||||
---
|
|
||||||
title: Creating an action
|
|
||||||
---
|
|
||||||
|
|
||||||
Your plugins can customize the IntelliJ Platform UI by adding new items to the menus and toolbars. The IntelliJ Platform provides the class [`AnAction`](upsource:///platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java), whose `actionPerformed` method is called each time you select a menu item or click a toolbar button.
|
|
||||||
|
|
||||||
To create custom actions in the *IntelliJ Platform*, you should perform two basic steps:
|
|
||||||
|
|
||||||
1. In your plugin, define an action or a system of actions that add their own items to menus and toolbars.
|
|
||||||
2. Register your actions.
|
|
||||||
|
|
||||||
This topic outlines the above steps. For detailed information and samples, refer to [IntelliJ Platform Action System](/basics/action_system.md).
|
|
||||||
|
|
||||||
### Defining actions
|
|
||||||
|
|
||||||
An action is a class derived from the `AnAction` class. To define your action, in your plugin, create a Java class derived from the `AnAction` class. In this class, override the `actionPerformed` method to be called when a menu item or a toolbar button is selected.
|
|
||||||
|
|
||||||
To clarify this procedure, consider the following code snippet that defines the `TextBoxes` class derived from the `AnAction` class:
|
|
||||||
|
|
||||||
```java
|
|
||||||
public class TextBoxes extends AnAction {
|
|
||||||
// If you register the action from Java code, this constructor is used to set the menu item name
|
|
||||||
// (optionally, you can specify the menu description and an icon to display next to the menu item).
|
|
||||||
// You can omit this constructor when registering the action in the plugin.xml file.
|
|
||||||
public TextBoxes() {
|
|
||||||
// Set the menu item name.
|
|
||||||
super("Text _Boxes");
|
|
||||||
// Set the menu item name, description and icon.
|
|
||||||
// super("Text _Boxes","Item description",IconLoader.getIcon("/Mypackage/icon.png"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void actionPerformed(AnActionEvent event) {
|
|
||||||
Project project = event.getData(PlatformDataKeys.PROJECT);
|
|
||||||
String txt= Messages.showInputDialog(project, "What is your name?", "Input your name", Messages.getQuestionIcon());
|
|
||||||
Messages.showMessageDialog(project, "Hello, " + txt + "!\n I am glad to see you.", "Information", Messages.getInformationIcon());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Note that optionally, you can define a set of classes derived from the `AnAction` class. In this case, your plugin will define a system of actions.
|
|
||||||
|
|
||||||
### Registering actions
|
|
||||||
|
|
||||||
Once you have defined an action or a system of actions, you must register them to specify the menu items or toolbar buttons associated with actions. You can register actions in one of the following ways:
|
|
||||||
|
|
||||||
* Register actions in the `<actions>` section of the `plugin.xml` file.
|
|
||||||
* Register actions from Java code.
|
|
||||||
|
|
||||||
This section provides some examples that illustrate how to register actions. For more information, refer to [IntelliJ Platform Action System](/basics/action_system.md).
|
|
||||||
|
|
||||||
#### Registering actions in the plugin.xml file
|
|
||||||
|
|
||||||
To register your actions, make appropriate changes to the `<actions>` section of the plugin.xml file for your IDEA project. The following fragment of the plugin.xml file adds the Sample Menu group (item) to the main menu. Clicking this item allows you to access **Sample Menu \| Text Boxes and Sample Menu \| Show Dialog** menu commands:
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
```xml
|
|
||||||
<actions>
|
|
||||||
<group id="MyPlugin.SampleMenu" text="_Sample Menu" description="Sample menu">
|
|
||||||
<add-to-group group-id="MainMenu" anchor="last" />
|
|
||||||
<action id="Myplugin.Textboxes" class="Mypackage.TextBoxes" text="Text _Boxes" description="A test menu item" />
|
|
||||||
<action id="Myplugin.Dialogs" class="Mypackage.MyShowDialog" text="Show _Dialog" description="A test menu item" />
|
|
||||||
</group>
|
|
||||||
</actions>
|
|
||||||
```
|
|
||||||
|
|
||||||
This fragment of the plugin.xml file demonstrates only some elements you can use in the `<actions>` section to register your actions. For information about all elements designed to register your actions, refer to [IntelliJ Platform Action System](/basics/action_system.md).
|
|
||||||
|
|
||||||
#### Registering actions from Java code
|
|
||||||
|
|
||||||
Alternatively, you can register your actions from Java code. For more information and samples that illustrate how to register actions from Java code, see [IntelliJ Platform Action System](/basics/action_system.md).
|
|
||||||
|
|
||||||
### Quick creation of actions
|
|
||||||
|
|
||||||
The IntelliJ Platform provides the **New Action** wizard that suggests a simplified way of creating actions, with all the required infrastructure. The wizard helps you declare the action class and automatically makes appropriate changes to the `<actions>` section of the plugin.xml file.
|
|
||||||
|
|
||||||
Note that you can use this wizard only to add a new action to an existing action group on the main menu or toolbar. If you want to create a new action group, and then add an action to this group, follow instructions earlier in this document.
|
|
||||||
|
|
||||||
**To create and register an action with the New Action wizard**
|
|
||||||
|
|
||||||
1. In your project, on the context menu of the destination package click **New** or press **Alt + Insert**.
|
|
||||||
2. On the **New** menu, click **Action**.
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
3. On the **New Action** page that opens, fill in the following fields, and then click **OK**:
|
|
||||||
|
|
||||||
* **Action ID**: Enter the unique ID of the action. Recommended format: `PluginName.ID`
|
|
||||||
* **Class Name**: Enter the name of the action class to be created.
|
|
||||||
* **Name**: Enter the name of the menu item or tooltip for toolbar button associated with action.
|
|
||||||
* **Description**: Optionally, enter the action description. The IDEA status bar indicates this description when focusing the action.
|
|
||||||
* In the **Add to Group** area, under **Groups**, **Actions** and **Anchor**, specify the action group to which to add a newly created action, and the position of the newly created action relative to other existing actions.
|
|
||||||
* In the **Keyboard Shortcuts** area, optionally, specify the first and second keystrokes of the action.
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
The IntelliJ Platform generates a `.java` file with the specified class name, registers the newly created action in the plugin.xml file, adds a node to the module tree view, and opens the created action class file in the editor.
|
|
||||||
|
|
||||||
## More Information
|
|
||||||
|
|
||||||
For more information on working with actions, please check out the [action system documentation](/basics/action_system.md)
|
|
||||||
and the [actions tutorial](/tutorials/action_system.md).
|
|
@ -1,5 +1,7 @@
|
|||||||
---
|
---
|
||||||
title: Creating a Plugin Project
|
title: Creating a Plugin Project
|
||||||
|
redirect_from:
|
||||||
|
- /basics/getting_started/creating_an_action.html
|
||||||
---
|
---
|
||||||
|
|
||||||
> **NOTE** For new projects, it is recommend to use [Gradle](/tutorials/build_system.md).
|
> **NOTE** For new projects, it is recommend to use [Gradle](/tutorials/build_system.md).
|
||||||
@ -32,3 +34,6 @@ For more information, refer to the [IntelliJ IDEA Web Help](https://www.jetbrain
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
### Adding Code to the Project
|
||||||
|
Before running the new project add some code to provide simple functionality.
|
||||||
|
See the [Creating Actions](/tutorials/action_system/working_with_custom_actions.md) tutorial for step-by-step instructions for adding a menu action.
|
||||||
|
@ -11,7 +11,7 @@ In this section:
|
|||||||
|
|
||||||
* [Setting Up a Development Environment](setting_up_environment.md)
|
* [Setting Up a Development Environment](setting_up_environment.md)
|
||||||
* [Creating a Plugin Project](creating_plugin_project.md)
|
* [Creating a Plugin Project](creating_plugin_project.md)
|
||||||
* [Creating an Action](creating_an_action.md)
|
* [Creating an Action](/tutorials/action_system/working_with_custom_actions.md)
|
||||||
* [Running and Debugging a Plugin](running_and_debugging_a_plugin.md)
|
* [Running and Debugging a Plugin](running_and_debugging_a_plugin.md)
|
||||||
* [Deploying a Plugin](deploying_plugin.md)
|
* [Deploying a Plugin](deploying_plugin.md)
|
||||||
* [Publishing a plugin to plugin repository](publishing_plugin.md)
|
* [Publishing a plugin to plugin repository](publishing_plugin.md)
|
||||||
|
@ -166,43 +166,20 @@ Converting a DevKit-based plugin project to a Gradle-based plugin project can be
|
|||||||
|
|
||||||
|
|
||||||
## Running a Simple Gradle-Based IntelliJ Platform Plugin
|
## Running a Simple Gradle-Based IntelliJ Platform Plugin
|
||||||
Before running [`my_gradle_project`](#components-of-a-wizard-generated-gradle-intellij-platform-plugin), some code needs to be added to provide simple functionality.
|
Gradle projects are run from the IDE's Gradle Tool window.
|
||||||
* Using the code below, add a `HelloAction.java` class to the `src/main/java/` folder.
|
|
||||||
For the sake of simplicity, no Java package is being used in this example.
|
|
||||||
|
|
||||||
```java
|
### Adding Code to the Project
|
||||||
import com.intellij.openapi.actionSystem.*;
|
Before running [`my_gradle_project`](#components-of-a-wizard-generated-gradle-intellij-platform-plugin), some code could be added to provide simple functionality.
|
||||||
import com.intellij.openapi.project.Project;
|
See the [Creating Actions](/tutorials/action_system/working_with_custom_actions.md) tutorial for step-by-step instructions for adding a menu action.
|
||||||
import com.intellij.openapi.ui.Messages;
|
|
||||||
|
|
||||||
public class HelloAction extends AnAction {
|
|
||||||
public HelloAction() {
|
|
||||||
super("Hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void actionPerformed(AnActionEvent event) {
|
|
||||||
Project project = event.getProject();
|
|
||||||
Messages.showMessageDialog(project, "Hello world!", "Greeting", Messages.getInformationIcon());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
* Add the code below to the `<actions>` section of the `plugin.xml` file:
|
### Executing the plugin
|
||||||
|
Open the Gradle tool window and search for the `runIde` task:
|
||||||
```xml
|
* If it’s not in the list, hit the [Refresh](https://www.jetbrains.com/help/idea/jetgradle-tool-window.html#1eeec055) button at the top of the Gradle window.
|
||||||
<group id="MyPlugin.SampleMenu" text="Greeting" description="Greeting menu">
|
* Or [Create a new Gradle Run Configuration](https://www.jetbrains.com/help/idea/create-run-debug-configuration-gradle-tasks.html).
|
||||||
<add-to-group group-id="MainMenu" anchor="last"/>
|
|
||||||
<action id="Myplugin.Textboxes" class="HelloAction" text="Hello" description="Says hello"/>
|
|
||||||
</group>
|
|
||||||
```
|
|
||||||
|
|
||||||
* Open the Gradle tool window and search for the `runIde` task.
|
|
||||||
If it’s not in the list, hit the [Refresh](https://www.jetbrains.com/help/idea/jetgradle-tool-window.html#1eeec055) button at the top of the Gradle window.
|
|
||||||
Or [Create a new Gradle Run Configuration](https://www.jetbrains.com/help/idea/create-run-debug-configuration-gradle-tasks.html).
|
|
||||||
|
|
||||||
{:width="398px"}
|
{:width="398px"}
|
||||||
|
|
||||||
* Double-click on the _runIde_ task to execute it.
|
Double-click on the _runIde_ task to execute it.
|
||||||
See the IntelliJ IDEA help for more information about [Working with Gradle tasks](https://www.jetbrains.com/help/idea/gradle.html#96bba6c3).
|
See the IntelliJ IDEA help for more information about [Working with Gradle tasks](https://www.jetbrains.com/help/idea/gradle.html#96bba6c3).
|
||||||
|
|
||||||
Finally, when `my_gradle_plugin` launches in the IDE development instance, there should be a new **Greeting** main menu to the right of the **Help** menu.
|
Finally, when `my_gradle_plugin` launches in the IDE development instance, there should be a new menu under the **Tools** menu.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user