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.
This commit is contained in:
Aleksey Pivovarov
2016-03-17 16:50:36 +03:00
parent e0bb49d564
commit 1d770241ca
2 changed files with 14 additions and 1 deletions
@@ -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<AnAction> actionList = (List<AnAction>)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;
}
}
@@ -102,6 +102,7 @@ public class ActionStub extends AnAction{
targetPresentation.setDescription(sourcePresentation.getDescription());
}
targetAction.setShortcutSet(getShortcutSet());
targetAction.markAsGlobal();
}
public String getProjectType() {