From 1d770241caf5545d9840a332db6cae68b4cb09b8 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Mon, 15 Feb 2016 13:16:32 +0300 Subject: [PATCH] ui: warn about shortcuts being overwritten for global actions this is a common mistake, as the code `ActionManager.getInstance().getAction("ActionId").registerCustomShortcutSet(shortcut, component);` will erase user-defined shortcuts from this action. --- .../intellij/openapi/actionSystem/AnAction.java | 14 +++++++++++++- .../intellij/openapi/actionSystem/ActionStub.java | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java b/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java index 2707505c395c..5cd2c2148ac6 100644 --- a/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java +++ b/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java @@ -16,6 +16,8 @@ package com.intellij.openapi.actionSystem; import com.intellij.openapi.Disposable; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.PossiblyDumbAware; import com.intellij.openapi.project.Project; @@ -63,6 +65,8 @@ import java.util.List; * @see com.intellij.openapi.actionSystem.ActionPlaces */ public abstract class AnAction implements PossiblyDumbAware { + private static final Logger LOG = Logger.getInstance(AnAction.class); + public static final AnAction[] EMPTY_ARRAY = new AnAction[0]; @NonNls public static final String ourClientProperty = "AnAction.shortcutSet"; @@ -74,6 +78,7 @@ public abstract class AnAction implements PossiblyDumbAware { private static final ShortcutSet ourEmptyShortcutSet = new CustomShortcutSet(); private boolean myIsDefaultIcon = true; private boolean myWorksInInjected; + private boolean myIsGlobal; // action is registered in ActionManager /** @@ -149,7 +154,7 @@ public abstract class AnAction implements PossiblyDumbAware { } public final void registerCustomShortcutSet(@NotNull ShortcutSet shortcutSet, @Nullable final JComponent component, @Nullable Disposable parentDisposable) { - myShortcutSet = shortcutSet; + setShortcutSet(shortcutSet); if (component != null){ @SuppressWarnings("unchecked") List actionList = (List)component.getClientProperty(ourClientProperty); @@ -273,6 +278,9 @@ public abstract class AnAction implements PossiblyDumbAware { public abstract void actionPerformed(AnActionEvent e); protected void setShortcutSet(ShortcutSet shortcutSet) { + if (myIsGlobal && myShortcutSet != shortcutSet) { + LOG.error("Shortcuts of global AnActions should not be changed outside of KeymapManager"); + } myShortcutSet = shortcutSet; } @@ -325,4 +333,8 @@ public abstract class AnAction implements PossiblyDumbAware { public String toString() { return getTemplatePresentation().toString(); } + + void markAsGlobal() { + myIsGlobal = true; + } } diff --git a/platform/platform-api/src/com/intellij/openapi/actionSystem/ActionStub.java b/platform/platform-api/src/com/intellij/openapi/actionSystem/ActionStub.java index c96ce2aa3371..916b03c44ae2 100644 --- a/platform/platform-api/src/com/intellij/openapi/actionSystem/ActionStub.java +++ b/platform/platform-api/src/com/intellij/openapi/actionSystem/ActionStub.java @@ -102,6 +102,7 @@ public class ActionStub extends AnAction{ targetPresentation.setDescription(sourcePresentation.getDescription()); } targetAction.setShortcutSet(getShortcutSet()); + targetAction.markAsGlobal(); } public String getProjectType() {