mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-28 01:07:49 +08:00
[md] syntax highlighting
This commit is contained in:
parent
c568fcaea3
commit
e3090af795
@ -24,19 +24,23 @@ To create a new we need to extend
|
||||
[AnAction] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java)
|
||||
class:
|
||||
|
||||
```java
|
||||
public class SimpleAction extends AnAction {
|
||||
}
|
||||
```
|
||||
|
||||
The only method of an inheritor of
|
||||
[AnAction] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java)
|
||||
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. In this case the action does nothing.
|
||||
|
||||
```java
|
||||
public class SimpleAction extends AnAction {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent anActionEvent) {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-------------
|
||||
|
||||
@ -57,6 +61,7 @@ After filling the "New Action" form and applying the changes *<actions>* section
|
||||
[plugin.xml]()
|
||||
file will look like this:
|
||||
|
||||
```xml
|
||||
<actions>
|
||||
<!-- Add your actions here -->
|
||||
<action id="org.jetbrains.tutorials.actions.SimpleAction" class="org.jetbrains.tutorials.actions.SimpleAction"
|
||||
@ -64,11 +69,13 @@ file will look like this:
|
||||
<add-to-group group-id="ToolsMenu" anchor="first"/>
|
||||
</action>
|
||||
</actions>
|
||||
```
|
||||
|
||||
Full list of action's attributes can also be set manually in
|
||||
[plugin.xml]()
|
||||
configuration file like the following code sample shows:
|
||||
|
||||
```xml
|
||||
<actions>
|
||||
<!-- Add your actions here -->
|
||||
<!-- The <action> element defines an action to register.
|
||||
@ -103,6 +110,7 @@ configuration file like the following code sample shows:
|
||||
<add-to-group group-id="ToolsMenu" anchor="first"/>
|
||||
</action>
|
||||
</actions>
|
||||
```
|
||||
|
||||
After performing the steps described above we need to compile and run the plugin to the the newly created action available as a Tools Menu item.
|
||||

|
||||
@ -113,6 +121,7 @@ After performing the steps described above we need to compile and run the plugin
|
||||
In order to make the action do something we need to implement it's ```public void actionPerformed(AnActionEvent anActionEvent);``` method.
|
||||
In the following example action invokes a dialog that shows information about a selected Project View Item and has no icon and any pre-selected default option:
|
||||
|
||||
```java
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent anActionEvent) {
|
||||
Object navigatable = anActionEvent.getData(CommonDataKeys.NAVIGATABLE);
|
||||
@ -120,6 +129,7 @@ In the following example action invokes a dialog that shows information about a
|
||||
Messages.showDialog(navigatable.toString(), "Selected Element:", new String[]{"OK"}, -1, null);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------
|
||||
|
||||
@ -131,6 +141,7 @@ 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.
|
||||
|
||||
```java
|
||||
public class SimpleAction extends AnAction {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent anActionEvent) {
|
||||
@ -141,10 +152,12 @@ state and(or) presentation depending on the context.
|
||||
public void update(AnActionEvent anActionEvent) {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The following code sample illustrates how to make the action visible and available only when the following conditions are met:
|
||||
there's a project available and there's an item you can navigate to selected in the project view pane:
|
||||
|
||||
```java
|
||||
public class SimpleAction extends AnAction {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent anActionEvent) {
|
||||
@ -160,6 +173,7 @@ there's a project available and there's an item you can navigate to selected in
|
||||
anActionEvent.getPresentation().setEnabledAndVisible(navigatable != null);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Parameter anActionEvent carries information on the invocation place and data available.
|
||||
|
||||
@ -188,25 +202,30 @@ Grouping can be done by extending adding *<group>* attribute to *<actions>*
|
||||
[plugin.xml]()
|
||||
file.
|
||||
|
||||
```xml
|
||||
<actions>
|
||||
<group id="SimpleGroup" text="Custom Action Group" popup="true">
|
||||
</group>
|
||||
</actions>
|
||||
```
|
||||
|
||||
##Binding action groups to UI component.
|
||||
The following sample shows how to place a custom action group on top of the editor popup menu:
|
||||
|
||||
```xml
|
||||
<actions>
|
||||
<group id="SimpleGroup" text="Custom Action Group" popup="true">
|
||||
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
|
||||
</group>
|
||||
</actions>
|
||||
```
|
||||
|
||||
##Adding actions to the group.
|
||||
To create an action we need to extend
|
||||
[AnAction.java]()
|
||||
class:
|
||||
|
||||
```java
|
||||
public class GroupedAction extends AnAction {
|
||||
@Override
|
||||
public void update(AnActionEvent event) {
|
||||
@ -218,9 +237,11 @@ class:
|
||||
//Does nothing
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
And then the actions needs to be registered in the newly created group:
|
||||
|
||||
```xml
|
||||
<action>
|
||||
<group id="SimpleGroup" text="Custom Action Group" popup="true">
|
||||
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
|
||||
@ -229,6 +250,7 @@ And then the actions needs to be registered in the newly created group:
|
||||
</action>
|
||||
</group>
|
||||
</actions>
|
||||
```
|
||||
|
||||
After performing the steps described above the action group nad it's content will be available in the editor popup menu:
|
||||
|
||||
@ -248,11 +270,13 @@ This class is used if a set of actions belonging to the group is fixed, which is
|
||||
|
||||
Firstly, [DefaultActionGroup.java] should be derived:
|
||||
|
||||
```java
|
||||
public class CustomDefaultActionGroup extends DefaultActionGroup {
|
||||
@Override
|
||||
public void update(AnActionEvent event) {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
###Registering action group.
|
||||
As in case with the simple action group, the inheritor of
|
||||
@ -261,28 +285,35 @@ should be declared in
|
||||
[plugin.xml]()
|
||||
file:
|
||||
|
||||
```xml
|
||||
<actions>
|
||||
<group id="CustomDefaultActionGroup" class="org.jetbrains.tutorials.actions.CustomDefaultActionGroup" popup="true"
|
||||
text="DefaultActionGroup Inheritor" description="Default Action Group Demo">
|
||||
<add-to-group group-id="ToolsMenu" anchor="last"/>
|
||||
</group>
|
||||
</actions>
|
||||
```
|
||||
|
||||
###Creating an action.
|
||||
[AnAction.java]()
|
||||
needs to be extended:
|
||||
|
||||
```java
|
||||
public class CustomGroupedAction extends AnAction {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent anActionEvent) {
|
||||
//Does nothing
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
###Adding actions to the group.
|
||||
Action's class should be registered in
|
||||
[plugin.xml]()
|
||||
:
|
||||
|
||||
```xml
|
||||
<actions>
|
||||
<group id="CustomDefaultActionGroup" class="org.jetbrains.tutorials.actions.CustomDefaultActionGroup" popup="true"
|
||||
text="DefaultActionGroup Inheritor" description="Default Action Group Demo">
|
||||
<add-to-group group-id="ToolsMenu" anchor="last"/>
|
||||
@ -290,11 +321,13 @@ Action's class should be registered in
|
||||
text="Custom Grouped Action" description="Custom Grouped Action Demo"/>
|
||||
</group>
|
||||
</actions>
|
||||
```
|
||||
|
||||
###Providing specific behaviour for the group.
|
||||
In this case we override ```public void update(AnActionEvent event);``` method to make the group visible as a *Tools* menu item,
|
||||
however, it will be enabled only if there's an instance of the editor available. Also a custom icon is set up:
|
||||
|
||||
```java
|
||||
public class CustomDefaultActionGroup extends DefaultActionGroup {
|
||||
@Override
|
||||
public void update(AnActionEvent event) {
|
||||
@ -304,6 +337,7 @@ however, it will be enabled only if there's an instance of the editor available.
|
||||
event.getPresentation().setIcon(AllIcons.General.Error);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
After compiling and running the code sample above, *Tools* menu item should contain an extra group of action with a user-defined icon:
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user