Search in sources :

Example 1 with ShortcutRestrictions

use of com.intellij.openapi.keymap.impl.ShortcutRestrictions in project intellij-community by JetBrains.

the class KeymapPanel method createEditActionGroup.

@NotNull
private DefaultActionGroup createEditActionGroup(@NotNull final String actionId) {
    DefaultActionGroup group = new DefaultActionGroup();
    final ShortcutRestrictions restrictions = ActionShortcutRestrictions.getInstance().getForActionId(actionId);
    if (restrictions.allowKeyboardShortcut) {
        group.add(new DumbAwareAction("Add Keyboard Shortcut") {

            @Override
            public void actionPerformed(@NotNull AnActionEvent e) {
                Keymap keymapSelected = myEditor.getModel().getSelected();
                assert keymapSelected != null;
                addKeyboardShortcut(actionId, restrictions, keymapSelected, KeymapPanel.this, myQuickLists);
                repaintLists();
                currentKeymapChanged();
            }
        });
    }
    if (restrictions.allowMouseShortcut) {
        group.add(new DumbAwareAction("Add Mouse Shortcut") {

            @Override
            public void actionPerformed(@NotNull AnActionEvent e) {
                Keymap keymapSelected = myEditor.getModel().getSelected();
                assert keymapSelected != null;
                addMouseShortcut(actionId, restrictions, keymapSelected, KeymapPanel.this, myQuickLists);
                repaintLists();
                currentKeymapChanged();
            }
        });
    }
    if (Registry.is("actionSystem.enableAbbreviations") && restrictions.allowAbbreviation) {
        group.add(new DumbAwareAction("Add Abbreviation") {

            @Override
            public void actionPerformed(@NotNull AnActionEvent e) {
                String abbr = Messages.showInputDialog("Enter new abbreviation:", "Abbreviation", null);
                if (abbr != null) {
                    AbbreviationManager.getInstance().register(abbr, myActionsTree.getSelectedActionId());
                    repaintLists();
                }
            }

            @Override
            public void update(@NotNull AnActionEvent e) {
                e.getPresentation().setEnabledAndVisible(myActionsTree.getSelectedActionId() != null);
            }
        });
    }
    group.addSeparator();
    Keymap keymap = myEditor.getModel().getSelected();
    assert keymap != null;
    for (final Shortcut shortcut : keymap.getShortcuts(actionId)) {
        group.add(new DumbAwareAction("Remove " + KeymapUtil.getShortcutText(shortcut)) {

            @Override
            public void actionPerformed(@NotNull AnActionEvent e) {
                Keymap keymap = createKeymapCopyIfNeeded();
                keymap.removeShortcut(actionId, shortcut);
                if (StringUtil.startsWithChar(actionId, '$')) {
                    keymap.removeShortcut(KeyMapBundle.message("editor.shortcut", actionId.substring(1)), shortcut);
                }
                repaintLists();
                currentKeymapChanged();
            }
        });
    }
    if (Registry.is("actionSystem.enableAbbreviations")) {
        for (final String abbreviation : AbbreviationManager.getInstance().getAbbreviations(actionId)) {
            group.addAction(new DumbAwareAction("Remove Abbreviation '" + abbreviation + "'") {

                @Override
                public void actionPerformed(@NotNull AnActionEvent e) {
                    AbbreviationManager.getInstance().remove(abbreviation, actionId);
                    repaintLists();
                }
            });
        }
    }
    group.add(new Separator());
    group.add(new DumbAwareAction("Reset Shortcuts") {

        @Override
        public void actionPerformed(@NotNull AnActionEvent e) {
            ((KeymapImpl) createKeymapCopyIfNeeded()).clearOwnActionsId(actionId);
            currentKeymapChanged();
            repaintLists();
        }

        @Override
        public void update(@NotNull AnActionEvent e) {
            e.getPresentation().setVisible(((KeymapImpl) myEditor.getModel().getSelected()).hasOwnActionId(actionId));
        }
    });
    return group;
}
Also used : KeymapImpl(com.intellij.openapi.keymap.impl.KeymapImpl) ActionShortcutRestrictions(com.intellij.openapi.keymap.impl.ActionShortcutRestrictions) ShortcutRestrictions(com.intellij.openapi.keymap.impl.ShortcutRestrictions) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ShortcutRestrictions

use of com.intellij.openapi.keymap.impl.ShortcutRestrictions in project intellij-community by JetBrains.

the class ActionsTreeTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // create dummy actions
    myActionWithoutTextAndDescription = new MyAction(null, null);
    myActionWithTextOnly = new MyAction("some text", null);
    myActionWithTextAndDescription = new MyAction("text", "description");
    myActionExistent = new MyAction("text", "description");
    myActionWithUseShortcutOfExistent = new MyAction("text", "description");
    myActionWithUseShortcutOfExistentRedefined = new MyAction("text", "description");
    myActionWithUseShortcutOfExistentRedefinedInParent = new MyAction("text", "description");
    myActionWithUseShortcutOfNonExistent = new MyAction("text", "description");
    myActionWithFixedShortcuts = new MyAction("text", "description");
    ActionManager actionManager = ActionManager.getInstance();
    actionManager.registerAction(ACTION_WITHOUT_TEXT_AND_DESCRIPTION, myActionWithoutTextAndDescription);
    actionManager.registerAction(ACTION_WITH_TEXT_ONLY, myActionWithTextOnly);
    actionManager.registerAction(ACTION_WITH_TEXT_AND_DESCRIPTION, myActionWithTextAndDescription);
    actionManager.registerAction(EXISTENT_ACTION, myActionExistent);
    actionManager.registerAction(ACTION_WITH_USE_SHORTCUT_OF_EXISTENT_ACTION, myActionWithUseShortcutOfExistent);
    actionManager.registerAction(ACTION_WITH_USE_SHORTCUT_OF_EXISTENT_ACTION_REDEFINED, myActionWithUseShortcutOfExistentRedefined);
    actionManager.registerAction(ACTION_WITH_USE_SHORTCUT_OF_EXISTENT_ACTION_REDEFINED_IN_PARENT, myActionWithUseShortcutOfExistentRedefinedInParent);
    actionManager.registerAction(ACTION_WITH_USE_SHORTCUT_OF_NON_EXISTENT_ACTION, myActionWithUseShortcutOfNonExistent);
    actionManager.registerAction(ACTION_WITH_FIXED_SHORTCUTS, myActionWithFixedShortcuts);
    KeymapManagerEx.getInstanceEx().bindShortcuts(EXISTENT_ACTION, ACTION_WITH_USE_SHORTCUT_OF_EXISTENT_ACTION);
    KeymapManagerEx.getInstanceEx().bindShortcuts(EXISTENT_ACTION, ACTION_WITH_USE_SHORTCUT_OF_EXISTENT_ACTION_REDEFINED);
    KeymapManagerEx.getInstanceEx().bindShortcuts(EXISTENT_ACTION, ACTION_WITH_USE_SHORTCUT_OF_EXISTENT_ACTION_REDEFINED_IN_PARENT);
    KeymapManagerEx.getInstanceEx().bindShortcuts(NON_EXISTENT_ACTION, ACTION_WITH_USE_SHORTCUT_OF_NON_EXISTENT_ACTION);
    mySavedRestrictions = ActionShortcutRestrictions.getInstance();
    setRestrictions(new ActionShortcutRestrictions() {

        @NotNull
        @Override
        public ShortcutRestrictions getForActionId(String actionId) {
            return ACTION_WITH_FIXED_SHORTCUTS.equals(actionId) ? new ShortcutRestrictions(false, false, false, false, false, false) : ShortcutRestrictions.NO_RESTRICTIONS;
        }
    });
    assertEquals("$Delete", KeymapManagerEx.getInstanceEx().getActionBinding(ACTION_EDITOR_DELETE_WITH_SHORTCUT));
    assertEquals("$Cut", KeymapManagerEx.getInstanceEx().getActionBinding(ACTION_EDITOR_CUT_WITHOUT_SHORTCUT));
    assertNotNull(actionManager.getAction(ACTION_EDITOR_DELETE_WITH_SHORTCUT));
    assertNotNull(actionManager.getAction(ACTION_EDITOR_CUT_WITHOUT_SHORTCUT));
    DefaultActionGroup group = (DefaultActionGroup) actionManager.getAction(IdeActions.GROUP_EDITOR);
    group.addAll(myActionWithoutTextAndDescription, myActionWithTextOnly, myActionWithTextAndDescription, myActionExistent, myActionWithUseShortcutOfExistent, myActionWithUseShortcutOfExistentRedefined, myActionWithUseShortcutOfExistentRedefinedInParent, myActionWithUseShortcutOfNonExistent, myActionWithFixedShortcuts);
    // populate action tree
    myActionsTree = new ActionsTree();
    KeyboardShortcut shortcut1 = new KeyboardShortcut(KeyStroke.getKeyStroke('1'), null);
    KeyboardShortcut shortcut2 = new KeyboardShortcut(KeyStroke.getKeyStroke('2'), null);
    KeymapImpl parent = new KeymapImpl();
    parent.addShortcut(ACTION_WITH_USE_SHORTCUT_OF_EXISTENT_ACTION_REDEFINED_IN_PARENT, shortcut1);
    parent.setName("parent");
    parent.setCanModify(false);
    KeymapImpl child = parent.deriveKeymap("child");
    child.addShortcut(ACTION_WITH_USE_SHORTCUT_OF_EXISTENT_ACTION_REDEFINED, shortcut2);
    child.addShortcut(ACTION_EDITOR_DELETE_WITH_SHORTCUT, shortcut2);
    myActionsTree.reset(child, new QuickList[0]);
}
Also used : KeymapImpl(com.intellij.openapi.keymap.impl.KeymapImpl) ActionShortcutRestrictions(com.intellij.openapi.keymap.impl.ActionShortcutRestrictions) ShortcutRestrictions(com.intellij.openapi.keymap.impl.ShortcutRestrictions) NotNull(org.jetbrains.annotations.NotNull) ActionShortcutRestrictions(com.intellij.openapi.keymap.impl.ActionShortcutRestrictions)

Aggregations

ActionShortcutRestrictions (com.intellij.openapi.keymap.impl.ActionShortcutRestrictions)2 KeymapImpl (com.intellij.openapi.keymap.impl.KeymapImpl)2 ShortcutRestrictions (com.intellij.openapi.keymap.impl.ShortcutRestrictions)2 NotNull (org.jetbrains.annotations.NotNull)2 DumbAwareAction (com.intellij.openapi.project.DumbAwareAction)1