IDEA-146427 Do not create a Keymap copy if dialog is cancelled
authorSergey Malenkov <sergey.malenkov@jetbrains.com>
Fri, 6 Nov 2015 16:12:10 +0000 (19:12 +0300)
committerSergey Malenkov <sergey.malenkov@jetbrains.com>
Fri, 6 Nov 2015 17:01:26 +0000 (20:01 +0300)
platform/platform-impl/src/com/intellij/openapi/keymap/impl/ui/KeymapPanel.java
platform/platform-impl/src/com/intellij/openapi/keymap/impl/ui/ShortcutDialog.java

index 3083d4f6508b1b3214ac4b1d2847f726b8937674..702ac30fb01d7569868e39bd946735c4389b6f19 100644 (file)
@@ -467,25 +467,25 @@ public class KeymapPanel extends JPanel implements SearchableConfigurable, Confi
   }
 
   private void addKeyboardShortcut(@NotNull String actionId, @Nullable Shortcut shortcut) {
-    Keymap keymap = createKeymapCopyIfNeeded();
-    addKeyboardShortcut(actionId, shortcut, keymap, this, myQuickLists);
+    Keymap keymapSelected = myEditor.getModel().getSelected();
+    addKeyboardShortcut(actionId, shortcut, keymapSelected, this, myQuickLists);
     repaintLists();
     currentKeymapChanged();
   }
 
   public static void addKeyboardShortcut(@NotNull String actionId,
                                          @Nullable Shortcut shortcut,
-                                         @NotNull Keymap keymap,
+                                         @NotNull Keymap keymapSelected,
                                          @NotNull Component parent,
                                          @NotNull QuickList[] quickLists) {
     KeyboardShortcutDialog dialog = new KeyboardShortcutDialog(parent);
-    KeyboardShortcut keyboardShortcut = dialog.showAndGet(shortcut, actionId, keymap, quickLists);
+    KeyboardShortcut keyboardShortcut = dialog.showAndGet(shortcut, actionId, keymapSelected, quickLists);
     if (keyboardShortcut == null) {
       return;
     }
 
-    Map<String, ArrayList<KeyboardShortcut>> conflicts = keymap.getConflicts(actionId, keyboardShortcut);
-    if (!conflicts.isEmpty()) {
+    Keymap keymap = null;
+    if (dialog.hasConflicts()) {
       int result = Messages.showYesNoCancelDialog(
         parent, 
         KeyMapBundle.message("conflict.shortcut.dialog.message"),
@@ -496,6 +496,8 @@ public class KeymapPanel extends JPanel implements SearchableConfigurable, Confi
         Messages.getWarningIcon());
 
       if (result == Messages.YES) {
+        keymap = createKeymapCopyIfNeededAndPossible(parent, keymapSelected);
+        Map<String, ArrayList<KeyboardShortcut>> conflicts = keymap.getConflicts(actionId, keyboardShortcut);
         for (String id : conflicts.keySet()) {
           for (KeyboardShortcut s : conflicts.get(id)) {
             keymap.removeShortcut(id, s);
@@ -508,6 +510,8 @@ public class KeymapPanel extends JPanel implements SearchableConfigurable, Confi
     }
 
     // if shortcut is already registered to this action, just select it in the list
+
+    if (keymap == null) keymap = createKeymapCopyIfNeededAndPossible(parent, keymapSelected);
     Shortcut[] shortcuts = keymap.getShortcuts(actionId);
     for (Shortcut s : shortcuts) {
       if (s.equals(keyboardShortcut)) {
@@ -527,16 +531,16 @@ public class KeymapPanel extends JPanel implements SearchableConfigurable, Confi
       return;
     }
 
-    Keymap keymap = createKeymapCopyIfNeeded();
+    Keymap keymapSelected = myEditor.getModel().getSelected();
 
     MouseShortcutDialog dialog = new MouseShortcutDialog(this, restrictions.allowMouseDoubleClick);
-    MouseShortcut mouseShortcut = dialog.showAndGet(shortcut, actionId, keymap, myQuickLists);
+    MouseShortcut mouseShortcut = dialog.showAndGet(shortcut, actionId, keymapSelected, myQuickLists);
     if (mouseShortcut == null) {
       return;
     }
 
-    String[] actionIds = keymap.getActionIds(mouseShortcut);
-    if (actionIds.length > 1 || (actionIds.length == 1 && !actionId.equals(actionIds[0]))) {
+    Keymap keymap = null;
+    if (dialog.hasConflicts()) {
       int result = Messages.showYesNoCancelDialog(
         this,
         KeyMapBundle.message("conflict.shortcut.dialog.message"),
@@ -547,6 +551,8 @@ public class KeymapPanel extends JPanel implements SearchableConfigurable, Confi
         Messages.getWarningIcon());
 
       if (result == Messages.YES) {
+        keymap = createKeymapCopyIfNeededAndPossible(this, keymapSelected);
+        String[] actionIds = keymap.getActionIds(mouseShortcut);
         for (String id : actionIds) {
           keymap.removeShortcut(id, mouseShortcut);
         }
@@ -558,6 +564,7 @@ public class KeymapPanel extends JPanel implements SearchableConfigurable, Confi
 
     // if shortcut is already registered to this action, just select it in the list
 
+    if (keymap == null) keymap = createKeymapCopyIfNeededAndPossible(this, keymapSelected);
     Shortcut[] shortcuts = keymap.getShortcuts(actionId);
     for (Shortcut shortcut1 : shortcuts) {
       if (shortcut1.equals(mouseShortcut)) {
@@ -578,6 +585,15 @@ public class KeymapPanel extends JPanel implements SearchableConfigurable, Confi
     myActionsTree.getComponent().repaint();
   }
 
+  @NotNull
+  private static Keymap createKeymapCopyIfNeededAndPossible(Component parent, Keymap keymap) {
+    if (parent instanceof KeymapPanel) {
+      KeymapPanel panel = (KeymapPanel)parent;
+      keymap = panel.createKeymapCopyIfNeeded();
+    }
+    return keymap;
+  }
+
   @NotNull
   private Keymap createKeymapCopyIfNeeded() {
     Keymap keymap = myEditor.getModel().getSelected();
index c3ade8b94c5e4b85b89fbb39df17af24e66dcc83..b0713fe96cfca62cdf121582c759be0042103758 100644 (file)
@@ -74,6 +74,10 @@ abstract class ShortcutDialog<T extends Shortcut> extends DialogWrapper {
     return myGroup == null ? null : myGroup.getActionQualifiedPath(actionId);
   }
 
+  boolean hasConflicts() {
+    return myConflictsPanel.isVisible();
+  }
+
   abstract Collection<String> getConflicts(T shortcut, String actionId, Keymap keymap);
 
   abstract T toShortcut(Object value);