private static boolean matchMouseShortcut( final Keymap activeKeymap, final int modifiers, final String actionId) { final MouseShortcut syntheticShortcut = new MouseShortcut(MouseEvent.BUTTON1, modifiers, 1); for (Shortcut shortcut : activeKeymap.getShortcuts(actionId)) { if (shortcut instanceof MouseShortcut) { final MouseShortcut mouseShortcut = (MouseShortcut) shortcut; if (mouseShortcut.getModifiers() == syntheticShortcut.getModifiers()) { return true; } } } return false; }
/** * @param shortcut dialog will be initialized with this <code>shortcut</code>. It can be <code> * null</code> if dialog is used to create new mouse shortcut. */ public MouseShortcutDialog( JComponent parentComponent, MouseShortcut shortcut, @NotNull Keymap keymap, @NotNull String actiondId, @NotNull Group mainGroup) { super(parentComponent, true); setTitle(KeyMapBundle.message("mouse.shortcut.dialog.title")); myKeymap = keymap; myActionId = actiondId; myMainGroup = mainGroup; myRbSingleClick = new JRadioButton(KeyMapBundle.message("mouse.shortcut.dialog.single.click.radio")); myRbDoubleClick = new JRadioButton(KeyMapBundle.message("mouse.shortcut.dialog.double.click.radio")); ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(myRbSingleClick); buttonGroup.add(myRbDoubleClick); myLblPreview = new JLabel(" "); myClickPad = new MyClickPad(); myTarConflicts = new JTextArea(); myTarConflicts.setFocusable(false); myTarConflicts.setEditable(false); myTarConflicts.setBackground(UIUtil.getPanelBackground()); myTarConflicts.setLineWrap(true); myTarConflicts.setWrapStyleWord(true); if (shortcut != null) { if (shortcut.getClickCount() == 1) { myRbSingleClick.setSelected(true); } else { myRbDoubleClick.setSelected(true); } myButton = shortcut.getButton(); myModifiers = shortcut.getModifiers(); } else { myRbSingleClick.setSelected(true); myButton = -1; myModifiers = -1; } updatePreviewAndConflicts(); init(); }
/** * Checks that one of the mouse shortcuts assigned to the provided action has the same modifiers * as provided */ public static boolean matchActionMouseShortcutsModifiers( final Keymap activeKeymap, @JdkConstants.InputEventMask int modifiers, final String actionId) { final MouseShortcut syntheticShortcut = new MouseShortcut(MouseEvent.BUTTON1, modifiers, 1); for (Shortcut shortcut : activeKeymap.getShortcuts(actionId)) { if (shortcut instanceof MouseShortcut) { final MouseShortcut mouseShortcut = (MouseShortcut) shortcut; if (mouseShortcut.getModifiers() == syntheticShortcut.getModifiers()) { return true; } } } return false; }
/** * Checks whether mouse event's button and modifiers match a shortcut configured in active keymap * for given action id. Only shortcuts having click count of 1 can be matched, mouse event's click * count is ignored. */ public static boolean isMouseActionEvent(@NotNull MouseEvent e, @NotNull String actionId) { KeymapManager keymapManager = KeymapManager.getInstance(); if (keymapManager == null) { return false; } Keymap keymap = keymapManager.getActiveKeymap(); if (keymap == null) { return false; } int button = MouseShortcut.getButton(e); int modifiers = e.getModifiersEx(); if (button == MouseEvent.NOBUTTON && e.getID() == MouseEvent.MOUSE_DRAGGED) { // mouse drag events don't have button field set due to some reason if ((modifiers & InputEvent.BUTTON1_DOWN_MASK) != 0) { button = MouseEvent.BUTTON1; } else if ((modifiers & InputEvent.BUTTON2_DOWN_MASK) != 0) { button = MouseEvent.BUTTON2; } } String[] actionIds = keymap.getActionIds(new MouseShortcut(button, modifiers, 1)); if (actionIds == null) { return false; } for (String id : actionIds) { if (actionId.equals(id)) { return true; } } return false; }
public static String getShortcutText(@NotNull Shortcut shortcut) { String s = ""; if (shortcut instanceof KeyboardShortcut) { KeyboardShortcut keyboardShortcut = (KeyboardShortcut) shortcut; String acceleratorText = getKeystrokeText(keyboardShortcut.getFirstKeyStroke()); if (!acceleratorText.isEmpty()) { s = acceleratorText; } acceleratorText = getKeystrokeText(keyboardShortcut.getSecondKeyStroke()); if (!acceleratorText.isEmpty()) { s += ", " + acceleratorText; } } else if (shortcut instanceof MouseShortcut) { MouseShortcut mouseShortcut = (MouseShortcut) shortcut; s = getMouseShortcutText( mouseShortcut.getButton(), mouseShortcut.getModifiers(), mouseShortcut.getClickCount()); } else if (shortcut instanceof KeyboardModifierGestureShortcut) { final KeyboardModifierGestureShortcut gestureShortcut = (KeyboardModifierGestureShortcut) shortcut; s = gestureShortcut.getType() == KeyboardGestureAction.ModifierType.dblClick ? "Press, release and hold " : "Hold "; s += getKeystrokeText(gestureShortcut.getStroke()); } else { throw new IllegalArgumentException( "unknown shortcut class: " + shortcut.getClass().getCanonicalName()); } return s; }
private void fillActionsList( Component component, MouseShortcut mouseShortcut, boolean isModalContext) { myActions.clear(); // here we try to find "local" shortcuts if (component instanceof JComponent) { for (AnAction action : ActionUtil.getActions((JComponent) component)) { for (Shortcut shortcut : action.getShortcutSet().getShortcuts()) { if (mouseShortcut.equals(shortcut) && !myActions.contains(action)) { myActions.add(action); } } } // once we've found a proper local shortcut(s), we exit if (!myActions.isEmpty()) { return; } } // search in main keymap if (KeymapManagerImpl.ourKeymapManagerInitialized) { final KeymapManager keymapManager = KeymapManager.getInstance(); if (keymapManager != null) { final Keymap keymap = keymapManager.getActiveKeymap(); final String[] actionIds = keymap.getActionIds(mouseShortcut); ActionManager actionManager = ActionManager.getInstance(); for (String actionId : actionIds) { AnAction action = actionManager.getAction(actionId); if (action == null) continue; if (isModalContext && !action.isEnabledInModalContext()) continue; if (!myActions.contains(action)) { myActions.add(action); } } } } }
/** Updates all UI controls */ private void updatePreviewAndConflicts() { if (myButton == -1 || myModifiers == -1) { return; } myTarConflicts.setText(null); // Set text into preview area // empty string should have same height myLblPreview.setText( KeymapUtil.getMouseShortcutText(myButton, myModifiers, myRbSingleClick.isSelected() ? 1 : 2) + " "); // Detect conflicts final MouseShortcut mouseShortcut; if (myRbSingleClick.isSelected()) { mouseShortcut = new MouseShortcut(myButton, myModifiers, 1); } else { mouseShortcut = new MouseShortcut(myButton, myModifiers, 2); } StringBuilder buffer = new StringBuilder(); String[] actionIds = myKeymap.getActionIds(mouseShortcut); for (String actionId : actionIds) { if (actionId.equals(myActionId)) { continue; } String actionPath = myMainGroup.getActionQualifiedPath(actionId); // actionPath == null for editor actions having corresponding $-actions if (actionPath == null) { continue; } Shortcut[] shortcuts = myKeymap.getShortcuts(actionId); for (Shortcut shortcut1 : shortcuts) { if (!(shortcut1 instanceof MouseShortcut)) { continue; } MouseShortcut shortcut = (MouseShortcut) shortcut1; if (shortcut.getButton() != mouseShortcut.getButton() || shortcut.getModifiers() != mouseShortcut.getModifiers()) { continue; } if (buffer.length() > 1) { buffer.append('\n'); } buffer.append('['); buffer.append(actionPath); buffer.append(']'); break; } } if (buffer.length() == 0) { myTarConflicts.setForeground(UIUtil.getTextAreaForeground()); myTarConflicts.setText(KeyMapBundle.message("mouse.shortcut.dialog.no.conflicts.area")); } else { myTarConflicts.setForeground(Color.red); myTarConflicts.setText( KeyMapBundle.message("mouse.shortcut.dialog.assigned.to.area", buffer.toString())); } }