From 57637b6edb40d811668b668d629b2de3a1203564 Mon Sep 17 00:00:00 2001 From: JohnHake Date: Sun, 12 Aug 2018 13:59:36 -0700 Subject: [PATCH 1/6] Initial refactoring of register_actions. Still has some superflous classes. --- .../resources/META-INF/plugin.xml | 58 ++++++++++---- .../tutorials/actions/BaseActionGroup.java | 26 ------- .../actions/CustomDefaultActionGroup.java | 23 +++++- .../tutorials/actions/DynamicActionGroup.java | 40 ++++++++++ .../tutorials/actions/GroupedAction.java | 14 ---- .../tutorials/actions/SimpleAction.java | 26 ++++++- .../actions/SimplePopDialogAction.java | 78 +++++++++++++++++++ 7 files changed, 205 insertions(+), 60 deletions(-) delete mode 100644 register_actions/src/org/jetbrains/tutorials/actions/BaseActionGroup.java create mode 100644 register_actions/src/org/jetbrains/tutorials/actions/DynamicActionGroup.java delete mode 100644 register_actions/src/org/jetbrains/tutorials/actions/GroupedAction.java create mode 100644 register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java diff --git a/register_actions/resources/META-INF/plugin.xml b/register_actions/resources/META-INF/plugin.xml index 080b2f298..00d4d2952 100644 --- a/register_actions/resources/META-INF/plugin.xml +++ b/register_actions/resources/META-INF/plugin.xml @@ -1,12 +1,12 @@ - org.jetbrains.plugins.sample.RegisterActions + org.jetbrains.tutorials.actions.RegisterActions Sample plugin for working with IntelliJ Action System - 1.0 + 1.1 JetBrains - Illustration of Action System + Illustration of the Action System - Initial commit + Refactor to give users feedback when menu items are selected. @@ -39,7 +39,7 @@ The optional "use-shortcut-of" attribute specifies the ID of the action whose keyboard shortcut this action will use. The optional "description" attribute specifies the text which is displayed in the status bar when the action is focused. The optional "icon" attribute specifies the icon which is displayed on the toolbar button or next to the menu item. --> - - - - + + + + + + - - - + + + + - - + + + \ No newline at end of file diff --git a/register_actions/src/org/jetbrains/tutorials/actions/BaseActionGroup.java b/register_actions/src/org/jetbrains/tutorials/actions/BaseActionGroup.java deleted file mode 100644 index 6907d5648..000000000 --- a/register_actions/src/org/jetbrains/tutorials/actions/BaseActionGroup.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.jetbrains.tutorials.actions; - -import com.intellij.openapi.actionSystem.*; -import org.jetbrains.annotations.NotNull; - -/** - * @author Anna Bulenkova - */ -public class BaseActionGroup extends ActionGroup { - @NotNull - @Override - public AnAction[] getChildren(AnActionEvent anActionEvent) { - return new AnAction[]{new MyAction()}; - } - - class MyAction extends AnAction { - public MyAction() { - super("Dynamically Added Action"); - } - - @Override - public void actionPerformed(@NotNull AnActionEvent anActionEvent) { - //does nothing - } - } -} diff --git a/register_actions/src/org/jetbrains/tutorials/actions/CustomDefaultActionGroup.java b/register_actions/src/org/jetbrains/tutorials/actions/CustomDefaultActionGroup.java index 3211af2b0..cb8bdd357 100644 --- a/register_actions/src/org/jetbrains/tutorials/actions/CustomDefaultActionGroup.java +++ b/register_actions/src/org/jetbrains/tutorials/actions/CustomDefaultActionGroup.java @@ -1,18 +1,37 @@ +/* + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ + package org.jetbrains.tutorials.actions; import com.intellij.icons.AllIcons; -import com.intellij.openapi.actionSystem.*; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.actionSystem.DefaultActionGroup; import com.intellij.openapi.editor.Editor; /** + * Creates an action group to contain menu actions. See plugin.xml declarations. + * @see DefaultActionGroup * @author Anna Bulenkova */ public class CustomDefaultActionGroup extends DefaultActionGroup { + + /** + * Given CustomDefaultActionGroup is derived from AnAction, in this context + * update() determines whether the action group itself should be enabled or disabled. + * Requires an editor to be active in order to enable the group functionality. + * @see com.intellij.openapi.actionSystem.AnAction#update(AnActionEvent) + * @param event Event received when the associated group-id menu is chosen. + */ @Override public void update(AnActionEvent event) { + // Enable/disable depending on whether user is editing Editor editor = event.getData(CommonDataKeys.EDITOR); - event.getPresentation().setVisible(true); event.getPresentation().setEnabled(editor != null); + // Always make visible, even when disabled. Helps to demonstrate the placement. + event.getPresentation().setVisible(true); + // Take this opportunity to set an icon for the menu entry. event.getPresentation().setIcon(AllIcons.General.Error); } } diff --git a/register_actions/src/org/jetbrains/tutorials/actions/DynamicActionGroup.java b/register_actions/src/org/jetbrains/tutorials/actions/DynamicActionGroup.java new file mode 100644 index 000000000..5a4690125 --- /dev/null +++ b/register_actions/src/org/jetbrains/tutorials/actions/DynamicActionGroup.java @@ -0,0 +1,40 @@ +/* + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ + +package org.jetbrains.tutorials.actions; + +import com.intellij.openapi.actionSystem.ActionGroup; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import org.jetbrains.annotations.*; + +/** + * Demonstrates adding an action group to a menu statically in plugin.xml, and then creating a menu item + * within the group at runtime. See plugin.xml for the declaration of DynamicActionGroup, + * and note the group declaration does not contain an action. + * DynamicActionGroup is based on ActionGroup because menu children are determined + * on rules other than just positional constraints. + * + * @author Anna Bulenkova + * @see ActionGroup + */ +public class DynamicActionGroup extends ActionGroup { + + /** + * Returns an array of menu actions for the group. + * + * @param anActionEvent Event received when the associated group-id menu is chosen. + * @return: AnAction[] An instance of AnAction, in this case containing a single instance of the + * SimplePopDialogAction class. + */ + @NotNull + @Override + public AnAction[] getChildren(AnActionEvent anActionEvent) { + return new AnAction[]{ new SimplePopDialogAction("Action Added at Runtime", + "Dynamic Group Action Demo", + null) + }; + } + +} diff --git a/register_actions/src/org/jetbrains/tutorials/actions/GroupedAction.java b/register_actions/src/org/jetbrains/tutorials/actions/GroupedAction.java deleted file mode 100644 index a0d193f5c..000000000 --- a/register_actions/src/org/jetbrains/tutorials/actions/GroupedAction.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.jetbrains.tutorials.actions; - -import com.intellij.openapi.actionSystem.*; - -public class GroupedAction extends AnAction { - @Override - public void update(AnActionEvent event) { - } - - @Override - public void actionPerformed(AnActionEvent event) { - //Does nothing - } -} diff --git a/register_actions/src/org/jetbrains/tutorials/actions/SimpleAction.java b/register_actions/src/org/jetbrains/tutorials/actions/SimpleAction.java index fa76bcb02..04cbe0074 100644 --- a/register_actions/src/org/jetbrains/tutorials/actions/SimpleAction.java +++ b/register_actions/src/org/jetbrains/tutorials/actions/SimpleAction.java @@ -1,3 +1,7 @@ +/* + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ + package org.jetbrains.tutorials.actions; import com.intellij.openapi.actionSystem.AnAction; @@ -7,20 +11,40 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; import com.intellij.pom.Navigatable; + +/** + * Action class to demonstrate how to interact with the IntelliJ Platform when a menu entry is chosen. + * The association between this Java class and the menu structure is declared in this module's plugin.xml file + * @see com.intellij.openapi.actionSystem.AnAction + * @see com.intellij.openapi.actionSystem.AnActionEvent + */ public class SimpleAction extends AnAction { + + /** + * Takes action based on the user choosing the menu item. + * @param anActionEvent Event received when the associated menu item is chosen. + */ @Override public void actionPerformed(AnActionEvent anActionEvent) { + // Using the event, get the project and navigatable objects Project project = anActionEvent.getProject(); Navigatable navigatable = anActionEvent.getData(CommonDataKeys.NAVIGATABLE); if (project != null && navigatable != null) { + // Pop a small dialog Messages.showMessageDialog(project, navigatable.toString(), "Selected Element", Messages.getInformationIcon()); } } + /** + * Determines whether this menu item is suitable for the current context. + * @param anActionEvent Event received when the associated group-id menu is chosen. + */ @Override public void update(AnActionEvent anActionEvent) { + // Using the event, get the project and navigatable objects - they will be needed in actionPerformed() Project project = anActionEvent.getProject(); Navigatable navigatable = anActionEvent.getData(CommonDataKeys.NAVIGATABLE); - anActionEvent.getPresentation().setEnabledAndVisible(project != null && navigatable != null); + // Make the associated menu item both visible and enabled + anActionEvent.getPresentation().setEnabledAndVisible(project != null && navigatable != null); } } diff --git a/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java b/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java new file mode 100644 index 000000000..1db4b3ca0 --- /dev/null +++ b/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java @@ -0,0 +1,78 @@ +/* + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ + +package org.jetbrains.tutorials.actions; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.Messages; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; + +/** + * Action class to demonstrate how to interact with the IntelliJ Platform. + * The only action this class performs is to provide the user with a popup dialog as feedback. + * Typically this class is instantiated by the IntelliJ Platform framework based on declarations + * in the plugin.xml file. But when added at runtime this class is instantiated by an action group. + * @see com.intellij.openapi.actionSystem.AnAction + * @see com.intellij.openapi.actionSystem.AnActionEvent + */ +public class SimplePopDialogAction extends AnAction { + + // Capture the generic message icon for display in the dialog. + private static final Icon ourIcon = Messages.getInformationIcon(); + + /** + * This default constructor is used by the IntelliJ Platform framework to + * instantiate this class based on plugin.xml declarations. Only needed in SimplePopDialogAction + * class because another constructor is overridden. + * @see AnAction#AnAction() + */ + public SimplePopDialogAction() { + super(); + } + + /** + * This constructor is used to support dynamically added menu actions. + * It sets the text, description to be displayed for the menu item. + * Otherwise, the default AnAction constructor is used by the IntelliJ Platform. + * @see AnAction#AnAction(String, String, Icon) + * @param menuText The text to be displayed as a menu item. + * @param menuDescription The description of the menu item. + * @param menuIcon The icon to be used with the menu item. + */ + public SimplePopDialogAction(@Nullable String menuText, @Nullable String menuDescription, @Nullable Icon menuIcon) { + super(menuText, menuDescription, menuIcon); + } + + /** + * Gives the user feedback when the dynamic action menu is chosen. + * Pops a simple message dialog. + * @param anActionEvent Event received when the associated menu item is chosen. + */ + @Override + public void actionPerformed(@NotNull AnActionEvent anActionEvent) { + // Using the event, create and show a dialog + Project currentProject = anActionEvent.getProject(); + String dlgTitle = anActionEvent.getPresentation().getDescription(); + String dlgMessage = anActionEvent.getPresentation().getText() + " Selected!"; + Messages.showMessageDialog(currentProject, dlgMessage, dlgTitle, ourIcon); + } + + /** + * Determines whether this menu item is available for the current context. + * Requires a project to be open. + * @param anActionEvent Event received when the associated group-id menu is chosen. + */ + @Override + public void update(AnActionEvent anActionEvent) { + // Set the availability based on whether a project is open + Project project = anActionEvent.getProject(); + anActionEvent.getPresentation().setEnabledAndVisible(project != null); + } + +} From 442df758f668842ef11e84d45a71b83e3ee53b8a Mon Sep 17 00:00:00 2001 From: JohnHake Date: Mon, 13 Aug 2018 15:32:58 -0700 Subject: [PATCH 2/6] Updated grouping_action document and associated images. A few tweaks to source files for doc purposes. --- register_actions/resources/META-INF/plugin.xml | 2 +- .../jetbrains/tutorials/actions/CustomDefaultActionGroup.java | 2 +- .../src/org/jetbrains/tutorials/actions/DynamicActionGroup.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/register_actions/resources/META-INF/plugin.xml b/register_actions/resources/META-INF/plugin.xml index 00d4d2952..f456a3d11 100644 --- a/register_actions/resources/META-INF/plugin.xml +++ b/register_actions/resources/META-INF/plugin.xml @@ -90,7 +90,7 @@ - Date: Mon, 13 Aug 2018 18:26:44 -0700 Subject: [PATCH 3/6] Refactoring and documentation updates complete. Still needs testing --- .../actions/CustomGroupedAction.java | 10 ---- .../tutorials/actions/DynamicActionGroup.java | 2 +- .../tutorials/actions/SimpleAction.java | 50 ------------------- 3 files changed, 1 insertion(+), 61 deletions(-) delete mode 100644 register_actions/src/org/jetbrains/tutorials/actions/CustomGroupedAction.java delete mode 100644 register_actions/src/org/jetbrains/tutorials/actions/SimpleAction.java diff --git a/register_actions/src/org/jetbrains/tutorials/actions/CustomGroupedAction.java b/register_actions/src/org/jetbrains/tutorials/actions/CustomGroupedAction.java deleted file mode 100644 index 15b309a6a..000000000 --- a/register_actions/src/org/jetbrains/tutorials/actions/CustomGroupedAction.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.jetbrains.tutorials.actions; - -import com.intellij.openapi.actionSystem.*; - -public class CustomGroupedAction extends AnAction { - @Override - public void actionPerformed(AnActionEvent anActionEvent) { - //Does nothing - } -} diff --git a/register_actions/src/org/jetbrains/tutorials/actions/DynamicActionGroup.java b/register_actions/src/org/jetbrains/tutorials/actions/DynamicActionGroup.java index c46df3fc2..f2e521dc4 100644 --- a/register_actions/src/org/jetbrains/tutorials/actions/DynamicActionGroup.java +++ b/register_actions/src/org/jetbrains/tutorials/actions/DynamicActionGroup.java @@ -25,7 +25,7 @@ public class DynamicActionGroup extends ActionGroup { * Returns an array of menu actions for the group. * * @param anActionEvent Event received when the associated group-id menu is chosen. - * @return: AnAction[] An instance of AnAction, in this case containing a single instance of the + * @return AnAction[] An instance of AnAction, in this case containing a single instance of the * SimplePopDialogAction class. */ @NotNull diff --git a/register_actions/src/org/jetbrains/tutorials/actions/SimpleAction.java b/register_actions/src/org/jetbrains/tutorials/actions/SimpleAction.java deleted file mode 100644 index 04cbe0074..000000000 --- a/register_actions/src/org/jetbrains/tutorials/actions/SimpleAction.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. - */ - -package org.jetbrains.tutorials.actions; - -import com.intellij.openapi.actionSystem.AnAction; -import com.intellij.openapi.actionSystem.AnActionEvent; -import com.intellij.openapi.actionSystem.CommonDataKeys; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.ui.Messages; -import com.intellij.pom.Navigatable; - - -/** - * Action class to demonstrate how to interact with the IntelliJ Platform when a menu entry is chosen. - * The association between this Java class and the menu structure is declared in this module's plugin.xml file - * @see com.intellij.openapi.actionSystem.AnAction - * @see com.intellij.openapi.actionSystem.AnActionEvent - */ -public class SimpleAction extends AnAction { - - /** - * Takes action based on the user choosing the menu item. - * @param anActionEvent Event received when the associated menu item is chosen. - */ - @Override - public void actionPerformed(AnActionEvent anActionEvent) { - // Using the event, get the project and navigatable objects - Project project = anActionEvent.getProject(); - Navigatable navigatable = anActionEvent.getData(CommonDataKeys.NAVIGATABLE); - if (project != null && navigatable != null) { - // Pop a small dialog - Messages.showMessageDialog(project, navigatable.toString(), "Selected Element", Messages.getInformationIcon()); - } - } - - /** - * Determines whether this menu item is suitable for the current context. - * @param anActionEvent Event received when the associated group-id menu is chosen. - */ - @Override - public void update(AnActionEvent anActionEvent) { - // Using the event, get the project and navigatable objects - they will be needed in actionPerformed() - Project project = anActionEvent.getProject(); - Navigatable navigatable = anActionEvent.getData(CommonDataKeys.NAVIGATABLE); - // Make the associated menu item both visible and enabled - anActionEvent.getPresentation().setEnabledAndVisible(project != null && navigatable != null); - } -} From 8346970945c6872de93f98fddd1f0211fdafc8b4 Mon Sep 17 00:00:00 2001 From: JohnHake Date: Fri, 17 Aug 2018 08:21:34 -0700 Subject: [PATCH 4/6] Incorporated feedback from pull request. --- register_actions/resources/META-INF/plugin.xml | 2 +- .../tutorials/actions/SimplePopDialogAction.java | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/register_actions/resources/META-INF/plugin.xml b/register_actions/resources/META-INF/plugin.xml index f456a3d11..9cb5b953c 100644 --- a/register_actions/resources/META-INF/plugin.xml +++ b/register_actions/resources/META-INF/plugin.xml @@ -98,7 +98,7 @@ + last in the tools menu. --> diff --git a/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java b/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java index 1db4b3ca0..cc5f40f2a 100644 --- a/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java +++ b/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java @@ -23,9 +23,6 @@ import javax.swing.*; */ public class SimplePopDialogAction extends AnAction { - // Capture the generic message icon for display in the dialog. - private static final Icon ourIcon = Messages.getInformationIcon(); - /** * This default constructor is used by the IntelliJ Platform framework to * instantiate this class based on plugin.xml declarations. Only needed in SimplePopDialogAction @@ -51,7 +48,8 @@ public class SimplePopDialogAction extends AnAction { /** * Gives the user feedback when the dynamic action menu is chosen. - * Pops a simple message dialog. + * Pops a simple message dialog. See the psi_demo plugin for an + * example of how to use AnActionEvent to access Psi data. * @param anActionEvent Event received when the associated menu item is chosen. */ @Override @@ -60,7 +58,7 @@ public class SimplePopDialogAction extends AnAction { Project currentProject = anActionEvent.getProject(); String dlgTitle = anActionEvent.getPresentation().getDescription(); String dlgMessage = anActionEvent.getPresentation().getText() + " Selected!"; - Messages.showMessageDialog(currentProject, dlgMessage, dlgTitle, ourIcon); + Messages.showMessageDialog(currentProject, dlgMessage, dlgTitle, Messages.getInformationIcon()); } /** From b2b2bd1480929b237e7380b9a2c02eb66024b9c1 Mon Sep 17 00:00:00 2001 From: JohnHake Date: Mon, 20 Aug 2018 09:28:30 -0700 Subject: [PATCH 5/6] Added display of Psi element to dialog --- .../tutorials/actions/SimplePopDialogAction.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) mode change 100644 => 100755 register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java diff --git a/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java b/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java old mode 100644 new mode 100755 index cc5f40f2a..a1d104d1b --- a/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java +++ b/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java @@ -6,8 +6,10 @@ package org.jetbrains.tutorials.actions; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; 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; @@ -49,16 +51,21 @@ public class SimplePopDialogAction extends AnAction { /** * Gives the user feedback when the dynamic action menu is chosen. * Pops a simple message dialog. See the psi_demo plugin for an - * example of how to use AnActionEvent to access Psi data. + * example of how to use AnActionEvent to access data. * @param anActionEvent Event received when the associated menu item is chosen. */ @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, Messages.getInformationIcon()); + // 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()); } /** From b3cf5b060c36bccc99bee1933b1844e76fa60ae5 Mon Sep 17 00:00:00 2001 From: JohnHake Date: Mon, 20 Aug 2018 21:45:50 -0700 Subject: [PATCH 6/6] Added use of Navigatable object. Updated doc page to suit. --- .../org/jetbrains/tutorials/actions/SimplePopDialogAction.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java b/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java index a1d104d1b..13de550dd 100755 --- a/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java +++ b/register_actions/src/org/jetbrains/tutorials/actions/SimplePopDialogAction.java @@ -10,8 +10,7 @@ import com.intellij.openapi.actionSystem.CommonDataKeys; 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.*;