Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 74724ca

Browse files
author
JohnHake
committed
Added use of Navigatable object. Updated doc page to suit.
1 parent e76fd91 commit 74724ca

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

code_samples/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
import com.intellij.openapi.project.Project;
1111
import com.intellij.openapi.ui.Messages;
1212
import com.intellij.pom.Navigatable;
13-
import org.jetbrains.annotations.NotNull;
14-
import org.jetbrains.annotations.Nullable;
13+
import org.jetbrains.annotations.*;
1514

1615
import javax.swing.*;
1716

tutorials/action_system/working_with_custom_actions.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ public class SimplePopDialogAction extends AnAction {
2323
}
2424
```
2525

26+
**Note** the `SimplePopDialogAction` does not have class fields of any kind. This is because an instance of `AnAction` class
27+
exists for the entire lifetime of the application. If `AnAction` class uses a field to store data which has a shorter
28+
lifetime, and doesn't clear this data promptly, the data will be leaked. For example, any `AnAction` data that exists
29+
only within the context of a `Project` will cause the `Project` to be kept in memory after the user has closed it.
30+
31+
2632
### 1.2. Overriding actionPerformed()
2733

2834
The [AnAction](upsource:///platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java)
@@ -103,14 +109,23 @@ In order to make the action do something we need to add code to the `SimplePopDi
103109
The following code gets information from the `anActionEvent` input parameter and constructs a simple message dialog.
104110
A generic icon, and the `description` and `text` attributes from the invoking menu action are displayed.
105111

112+
For demonstration purposes the `AnActionEvent.getData()` method tests if a [Navigatable](upsource:///platform/core-api/src/com/intellij/pom/Navigatable.java)
113+
object is available, meaning e.g. an element has been selected in the editor. If so, information about
114+
the selected element is opportunistically added to the dialog.
115+
106116
```java
107117
@Override
108118
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
109119
// Using the event, create and show a dialog
110120
Project currentProject = anActionEvent.getProject();
121+
StringBuffer dlgMsg = new StringBuffer(anActionEvent.getPresentation().getText() + " Selected!");
111122
String dlgTitle = anActionEvent.getPresentation().getDescription();
112-
String dlgMessage = anActionEvent.getPresentation().getText() + " Selected!";
113-
Messages.showMessageDialog(currentProject, dlgMessage, dlgTitle, ourIcon);
123+
// If an element is selected in the editor, add info about it.
124+
Navigatable nav = anActionEvent.getData(CommonDataKeys.NAVIGATABLE);
125+
if (nav != null) {
126+
dlgMsg.append(String.format("\nSelected Element: %s", nav.toString()));
127+
}
128+
Messages.showMessageDialog(currentProject, dlgMsg.toString(), dlgTitle, Messages.getInformationIcon());
114129
}
115130
```
116131

@@ -133,7 +148,9 @@ public void update(AnActionEvent anActionEvent) {
133148
}
134149
```
135150

136-
Parameter `anActionEvent` carries information on the invocation place and data available.
151+
Parameter `anActionEvent` carries information on the invocation place and data available. Note the `update()` method does
152+
not check to see if a [Navigatable](upsource:///platform/core-api/src/com/intellij/pom/Navigatable.java) object is available
153+
before enabling `SimplePopDialogAction`. This is done for the purposes of demonstration code.
137154

138155
**Note** This method can be called frequently: for instance, if an action is added to a toolbar, it will be updated twice a second.
139156
This means that this method is supposed to _work really fast_; no real work should be done at this phase.

0 commit comments

Comments
 (0)