Skip to content

Commit

Permalink
Added use of Navigatable object. Updated doc page to suit.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnHake committed Aug 21, 2018
1 parent e76fd91 commit 74724ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.pom.Navigatable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.*;

import javax.swing.*;

Expand Down
23 changes: 20 additions & 3 deletions tutorials/action_system/working_with_custom_actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public class SimplePopDialogAction extends AnAction {
}
```

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


### 1.2. Overriding actionPerformed()

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

For demonstration purposes the `AnActionEvent.getData()` method tests if a [Navigatable](upsource:///platform/core-api/src/com/intellij/pom/Navigatable.java)
object is available, meaning e.g. an element has been selected in the editor. If so, information about
the selected element is opportunistically added to the dialog.

```java
@Override
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
// Using the event, create and show a dialog
Project currentProject = anActionEvent.getProject();
StringBuffer dlgMsg = new StringBuffer(anActionEvent.getPresentation().getText() + " Selected!");
String dlgTitle = anActionEvent.getPresentation().getDescription();
String dlgMessage = anActionEvent.getPresentation().getText() + " Selected!";
Messages.showMessageDialog(currentProject, dlgMessage, dlgTitle, ourIcon);
// If an element is selected in the editor, add info about it.
Navigatable nav = anActionEvent.getData(CommonDataKeys.NAVIGATABLE);
if (nav != null) {
dlgMsg.append(String.format("\nSelected Element: %s", nav.toString()));
}
Messages.showMessageDialog(currentProject, dlgMsg.toString(), dlgTitle, Messages.getInformationIcon());
}
```

Expand All @@ -133,7 +148,9 @@ public void update(AnActionEvent anActionEvent) {
}
```

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

**Note** 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.
Expand Down

0 comments on commit 74724ca

Please sign in to comment.