Example usage for com.intellij.openapi.keymap KeymapUtil getKeystrokeText

List of usage examples for com.intellij.openapi.keymap KeymapUtil getKeystrokeText

Introduction

In this page you can find the example usage for com.intellij.openapi.keymap KeymapUtil getKeystrokeText.

Prototype

@NotNull
    public static String getKeystrokeText(KeyStroke accelerator) 

Source Link

Usage

From source file:be.janickreynders.shortcuttranslator.ShortcutTranslatorDialog.java

License:Open Source License

private void handleKeyEventAsShortcut(KeyEvent e) {
    KeyStroke stroke = KeyStroke.getKeyStrokeForEvent(e);
    sourceShortcut.setText(KeymapUtil.getKeystrokeText(stroke));

    Set<ShortcutDescription> descriptions = translateShortcut(stroke, getSource(), getTarget());
    displayDescriptions(descriptions);//from   www . j  av  a2s .  c o  m

    pack();
    repaint();
    centerRelativeToParent();
}

From source file:com.facebook.buck.intellij.ideabuck.actions.ChooseTargetAction.java

License:Apache License

@Override
protected void gotoActionPerformed(AnActionEvent e) {
    final Project project = e.getData(CommonDataKeys.PROJECT);
    if (project == null) {
        return;//from w  w  w .j  a  va  2s.c o  m
    }

    final ChooseTargetModel model = new ChooseTargetModel(project);
    GotoActionCallback<String> callback = new GotoActionCallback<String>() {
        @Override
        public void elementChosen(ChooseByNamePopup chooseByNamePopup, Object element) {
            if (element == null) {
                return;
            }

            BuckSettingsProvider buckSettingsProvider = BuckSettingsProvider.getInstance();
            if (buckSettingsProvider == null || buckSettingsProvider.getState() == null) {
                return;
            }

            ChooseTargetItem item = (ChooseTargetItem) element;
            // if the target selected isn't an alias, then it has to have : or end with /...
            if (item.getName().contains("//") && !item.getName().contains(":")
                    && !item.getName().endsWith("/...")) {
                return;
            }

            if (buckSettingsProvider.getState().lastAlias != null) {
                buckSettingsProvider.getState().lastAlias.put(project.getBasePath(), item.getBuildTarget());
            }
            BuckToolWindowFactory.updateBuckToolWindowTitle(project);
        }
    };
    showNavigationPopup(e, model, callback, "Choose Build Target", true, false);

    // Add navigation listener for auto complete
    final ChooseByNamePopup chooseByNamePopup = project
            .getUserData(ChooseByNamePopup.CHOOSE_BY_NAME_POPUP_IN_PROJECT_KEY);
    chooseByNamePopup.getTextField().addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (KeyEvent.VK_RIGHT == e.getKeyCode()) {
                ChooseTargetItem obj = (ChooseTargetItem) chooseByNamePopup.getChosenElement();
                if (obj != null) {
                    chooseByNamePopup.getTextField().setText(obj.getName());
                    chooseByNamePopup.getTextField().repaint();
                }
            } else {
                super.keyPressed(e);
            }
            String adText = chooseByNamePopup.getAdText();
            if (adText != null) {
                chooseByNamePopup.setAdText(adText + " and "
                        + KeymapUtil.getKeystrokeText(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 2))
                        + " to use autocomplete");
            }
        }
    });
}

From source file:com.intellij.execution.actions.ChooseRunConfigurationPopup.java

License:Apache License

@Nullable
protected String getAdText(final Executor alternateExecutor) {
    final PropertiesComponent properties = PropertiesComponent.getInstance();
    if (alternateExecutor != null && !properties.isTrueValue(myAddKey)) {
        return String.format("Hold %s to %s", KeymapUtil.getKeystrokeText(KeyStroke.getKeyStroke("SHIFT")),
                alternateExecutor.getActionName());
    }/*from w w w  .  j  av a  2 s .com*/

    if (!properties.isTrueValue("run.configuration.edit.ad")) {
        return String.format("Press %s to Edit", KeymapUtil.getKeystrokeText(KeyStroke.getKeyStroke("F4")));
    }

    if (!properties.isTrueValue("run.configuration.delete.ad")) {
        return String.format("Press %s to Delete configuration",
                KeymapUtil.getKeystrokeText(KeyStroke.getKeyStroke("DELETE")));
    }

    return null;
}

From source file:com.intellij.ide.actionMacro.ActionMacroManager.java

License:Apache License

public ActionMacroManager(ActionManagerEx actionManagerEx) {
    myActionManager = actionManagerEx;//from ww w .  j a  va 2  s .  c om
    myActionManager.addAnActionListener(new AnActionListener() {
        public void beforeActionPerformed(AnAction action, DataContext dataContext, final AnActionEvent event) {
            String id = myActionManager.getId(action);
            if (id == null)
                return;
            //noinspection HardCodedStringLiteral
            if ("StartStopMacroRecording".equals(id)) {
                myLastActionInputEvent.add(event.getInputEvent());
            } else if (myIsRecording) {
                myRecordingMacro.appendAction(id);
                String shortcut = null;
                if (event.getInputEvent() instanceof KeyEvent) {
                    shortcut = KeymapUtil
                            .getKeystrokeText(KeyStroke.getKeyStrokeForEvent((KeyEvent) event.getInputEvent()));
                }
                notifyUser(id + (shortcut != null ? " (" + shortcut + ")" : ""), false);
                myLastActionInputEvent.add(event.getInputEvent());
            }
        }

        public void beforeEditorTyping(char c, DataContext dataContext) {
        }

        public void afterActionPerformed(final AnAction action, final DataContext dataContext,
                final AnActionEvent event) {
        }
    });

    myKeyProcessor = new MyKeyPostpocessor();
    IdeEventQueue.getInstance().addPostprocessor(myKeyProcessor, null);
}

From source file:com.intellij.ide.actions.GotoActionAction.java

License:Apache License

@Nullable
private static ChooseByNamePopup createPopup(@Nullable Project project, @NotNull final GotoActionModel model,
        String initialText, int initialIndex, final Component component, final AnActionEvent e) {
    ChooseByNamePopup oldPopup = project == null ? null
            : project.getUserData(ChooseByNamePopup.CHOOSE_BY_NAME_POPUP_IN_PROJECT_KEY);
    if (oldPopup != null) {
        oldPopup.close(false);//from w w  w  .  ja  v a2 s  .co m
    }
    final ChooseByNamePopup popup = new ChooseByNamePopup(project, model, new GotoActionItemProvider(model),
            oldPopup, initialText, false, initialIndex) {
        @Override
        protected void initUI(Callback callback, ModalityState modalityState, boolean allowMultipleSelection) {
            super.initUI(callback, modalityState, allowMultipleSelection);
            myList.addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    Object value = myList.getSelectedValue();
                    String text = getText(value);
                    if (text != null && myDropdownPopup != null) {
                        myDropdownPopup.setAdText(text, SwingConstants.LEFT);
                    }
                }

                @Nullable
                private String getText(@Nullable Object o) {
                    if (o instanceof GotoActionModel.MatchedValue) {
                        GotoActionModel.MatchedValue mv = (GotoActionModel.MatchedValue) o;
                        if (mv.value instanceof BooleanOptionDescription
                                || mv.value instanceof GotoActionModel.ActionWrapper
                                        && ((GotoActionModel.ActionWrapper) mv.value)
                                                .getAction() instanceof ToggleAction) {
                            return "Press "
                                    + KeymapUtil.getKeystrokeText(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0))
                                    + " to toggle option";
                        }
                    }
                    return getAdText();
                }
            });
        }

        @NotNull
        @Override
        protected Set<Object> filter(@NotNull Set<Object> elements) {
            return super.filter(model.sort(elements));
        }

        @Override
        protected boolean closeForbidden(boolean ok) {
            if (!ok)
                return false;
            Object element = getChosenElement();
            return element instanceof GotoActionModel.MatchedValue
                    && processOptionInplace(((GotoActionModel.MatchedValue) element).value, this, component, e)
                    || super.closeForbidden(true);
        }
    };

    if (project != null) {
        project.putUserData(ChooseByNamePopup.CHOOSE_BY_NAME_POPUP_IN_PROJECT_KEY, popup);
    }
    popup.addMouseClickListener(new MouseAdapter() {
        @Override
        public void mouseClicked(@NotNull MouseEvent me) {
            Object element = popup.getSelectionByPoint(me.getPoint());
            if (element instanceof GotoActionModel.MatchedValue) {
                if (processOptionInplace(((GotoActionModel.MatchedValue) element).value, popup, component, e)) {
                    me.consume();
                }
            }
        }
    });
    return popup;
}

From source file:com.intellij.ide.actions.GotoActionBase.java

License:Apache License

protected <T> void showNavigationPopup(final GotoActionCallback<T> callback,
        @Nullable final String findUsagesTitle, final ChooseByNamePopup popup,
        final boolean allowMultipleSelection) {

    final Class startedAction = myInAction;
    LOG.assertTrue(startedAction != null);

    popup.setCheckBoxShortcut(getShortcutSet());
    popup.setFindUsagesTitle(findUsagesTitle);
    final ChooseByNameFilter<T> filter = callback.createFilter(popup);

    if (historyEnabled() && popup.getAdText() == null) {
        popup.setAdText("Press "
                + KeymapUtil.getKeystrokeText(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.CTRL_MASK))
                + " or "
                + KeymapUtil.getKeystrokeText(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_MASK))
                + " to navigate through the history");
    }/*w  w w . j  a  va  2 s  .  c  om*/

    popup.invoke(new ChooseByNamePopupComponent.Callback() {
        @Override
        public void onClose() {
            //noinspection ConstantConditions
            if (startedAction != null && startedAction.equals(myInAction)) {
                String text = popup.getEnteredText();
                ourLastStrings.put(myInAction, Pair.create(text, popup.getSelectedIndex()));
                updateHistory(text);
                myInAction = null;
            }
            if (filter != null) {
                filter.close();
            }
        }

        private void updateHistory(@Nullable String text) {
            if (!StringUtil.isEmptyOrSpaces(text)) {
                List<String> history = ourHistory.get(myInAction);
                if (history == null)
                    history = ContainerUtil.newArrayList();
                if (!text.equals(ContainerUtil.getFirstItem(history))) {
                    history.add(0, text);
                }
                ourHistory.put(myInAction, history);
            }
        }

        @Override
        public void elementChosen(Object element) {
            callback.elementChosen(popup, element);
        }
    }, ModalityState.current(), allowMultipleSelection);

    final JTextField editor = popup.getTextField();

    final DocumentAdapter historyResetListener = new DocumentAdapter() {
        @Override
        protected void textChanged(DocumentEvent e) {
            myHistoryIndex = 0;
        }
    };

    abstract class HistoryAction extends DumbAwareAction {
        @Override
        public void update(@NotNull AnActionEvent e) {
            e.getPresentation().setEnabled(historyEnabled());
        }

        void setText(@NotNull List<String> strings) {
            javax.swing.text.Document document = editor.getDocument();
            document.removeDocumentListener(historyResetListener);
            editor.setText(strings.get(myHistoryIndex));
            document.addDocumentListener(historyResetListener);
            editor.selectAll();
        }
    }

    editor.getDocument().addDocumentListener(historyResetListener);

    new HistoryAction() {
        @Override
        public void actionPerformed(@NotNull AnActionEvent e) {
            List<String> strings = ourHistory.get(myInAction);
            setText(strings);
            myHistoryIndex = myHistoryIndex >= strings.size() - 1 ? 0 : myHistoryIndex + 1;
        }

    }.registerCustomShortcutSet(CustomShortcutSet.fromString("ctrl UP"), editor);

    new HistoryAction() {
        @Override
        public void actionPerformed(@NotNull AnActionEvent e) {
            List<String> strings = ourHistory.get(myInAction);
            setText(strings);
            myHistoryIndex = myHistoryIndex <= 0 ? strings.size() - 1 : myHistoryIndex - 1;
        }
    }.registerCustomShortcutSet(CustomShortcutSet.fromString("ctrl DOWN"), editor);
}

From source file:com.intellij.ide.util.gotoByName.ChooseByNamePopup.java

License:Apache License

protected ChooseByNamePopup(@Nullable final Project project, @NotNull ChooseByNameModel model,
        @NotNull ChooseByNameItemProvider provider, @Nullable ChooseByNamePopup oldPopup,
        @Nullable final String predefinedText, boolean mayRequestOpenInCurrentWindow, int initialIndex) {
    super(project, model, provider, oldPopup != null ? oldPopup.getEnteredText() : predefinedText,
            initialIndex);/*from www  .j a v a  2  s  .  c o m*/
    myOldPopup = oldPopup;
    if (oldPopup != null) { //inherit old focus owner
        myOldFocusOwner = oldPopup.myPreviouslyFocusedComponent;
    }
    myMayRequestCurrentWindow = mayRequestOpenInCurrentWindow;
    myAdText = myMayRequestCurrentWindow
            ? "Press "
                    + KeymapUtil
                            .getKeystrokeText(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_MASK))
                    + " to open in current window"
            : null;
}