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

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

Introduction

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

Prototype

@NotNull
    public static String getShortcutsText(Shortcut @NotNull [] shortcuts) 

Source Link

Usage

From source file:com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.java

License:Apache License

public LocalInspectionsPass(@NotNull PsiFile file, @Nullable Document document, int startOffset, int endOffset,
        @NotNull TextRange priorityRange, boolean ignoreSuppressed,
        @NotNull HighlightInfoProcessor highlightInfoProcessor) {
    super(file.getProject(), document, PRESENTABLE_NAME, file, null, new TextRange(startOffset, endOffset),
            true, highlightInfoProcessor);
    myStartOffset = startOffset;/*from   www.  ja  v a2s.  co m*/
    myEndOffset = endOffset;
    myPriorityRange = priorityRange;
    myIgnoreSuppressed = ignoreSuppressed;
    setId(Pass.LOCAL_INSPECTIONS);

    final KeymapManager keymapManager = KeymapManager.getInstance();
    if (keymapManager != null) {
        final Keymap keymap = keymapManager.getActiveKeymap();
        myShortcutText = keymap == null ? ""
                : "(" + KeymapUtil
                        .getShortcutsText(keymap.getShortcuts(IdeActions.ACTION_SHOW_ERROR_DESCRIPTION)) + ")";
    } else {
        myShortcutText = "";
    }
    InspectionProfileWrapper profileToUse = InspectionProjectProfileManagerImpl.getInstanceImpl(myProject)
            .getProfileWrapper();

    Function<InspectionProfileWrapper, InspectionProfileWrapper> custom = file
            .getUserData(InspectionProfileWrapper.CUSTOMIZATION_KEY);
    if (custom != null) {
        profileToUse = custom.fun(profileToUse);
    }

    myProfileWrapper = profileToUse;
    assert myProfileWrapper != null;
    mySeverityRegistrar = ((SeverityProvider) myProfileWrapper.getInspectionProfile().getProfileManager())
            .getSeverityRegistrar();

    // initial guess
    setProgressLimit(300 * 2);
}

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

License:Apache License

private static String getShortcut() {
    String shortcutText;/*  ww w  .j  a v  a  2 s .  c om*/
    final Shortcut[] shortcuts = KeymapManager.getInstance().getActiveKeymap()
            .getShortcuts(IdeActions.ACTION_SEARCH_EVERYWHERE);
    if (shortcuts.length == 0) {
        shortcutText = "Double " + (SystemInfo.isMac ? MacKeymapUtil.SHIFT : "Shift");
    } else {
        shortcutText = KeymapUtil.getShortcutsText(shortcuts);
    }
    return shortcutText;
}

From source file:com.intellij.ide.favoritesTreeView.TaskDefaultFavoriteListProvider.java

License:Apache License

private void showNotePopup(Project project, final DnDAwareTree tree, final Consumer<String> after,
        final String initText) {
    final JTextArea textArea = new JTextArea(3, 50);
    textArea.setFont(UIUtil.getTreeFont());
    textArea.setText(initText);// w w w.  ja  va2  s.  c  o  m
    final JBScrollPane pane = new JBScrollPane(textArea);
    final ComponentPopupBuilder builder = JBPopupFactory.getInstance()
            .createComponentPopupBuilder(pane, textArea).setCancelOnClickOutside(true)
            .setAdText(KeymapUtil.getShortcutsText(CommonShortcuts.CTRL_ENTER.getShortcuts()) + " to finish")
            .setTitle("Comment").setMovable(true).setRequestFocus(true).setResizable(true).setMayBeParent(true);
    final JBPopup popup = builder.createPopup();
    final JComponent content = popup.getContent();
    final AnAction action = new AnAction() {
        @Override
        public void actionPerformed(AnActionEvent e) {
            popup.closeOk(e.getInputEvent());
            unregisterCustomShortcutSet(content);
            after.consume(textArea.getText());
        }
    };
    action.registerCustomShortcutSet(CommonShortcuts.CTRL_ENTER, content);
    ApplicationManager.getApplication().invokeLater(new Runnable() {
        @Override
        public void run() {
            popup.showInCenterOf(tree);
        }
    }, ModalityState.NON_MODAL, project.getDisposed());
}

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

License:Apache License

/**
 * @param modalityState          - if not null rebuilds list in given {@link ModalityState}
 *///from  w  ww . j  av a2  s .  co m
protected void initUI(final ChooseByNamePopupComponent.Callback callback, final ModalityState modalityState,
        final boolean allowMultipleSelection) {
    myPreviouslyFocusedComponent = WindowManagerEx.getInstanceEx().getFocusedComponent(myProject);

    myActionListener = callback;
    myTextFieldPanel.setLayout(new BoxLayout(myTextFieldPanel, BoxLayout.Y_AXIS));

    final JPanel hBox = new JPanel();
    hBox.setLayout(new BoxLayout(hBox, BoxLayout.X_AXIS));

    JPanel caption2Tools = new JPanel(new BorderLayout());

    if (myModel.getPromptText() != null) {
        JLabel label = new JLabel(myModel.getPromptText());
        if (UIUtil.isUnderAquaLookAndFeel()) {
            label.setBorder(new CompoundBorder(new EmptyBorder(0, 9, 0, 0), label.getBorder()));
        }
        label.setFont(UIUtil.getLabelFont().deriveFont(Font.BOLD));
        caption2Tools.add(label, BorderLayout.WEST);
    }

    caption2Tools.add(hBox, BorderLayout.EAST);

    myCardContainer.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 4)); // space between checkbox and filter/show all in view buttons

    final String checkBoxName = myModel.getCheckBoxName();
    myCheckBox = new JCheckBox(checkBoxName != null ? checkBoxName + (myCheckBoxShortcut != null
            ? " (" + KeymapUtil.getShortcutsText(myCheckBoxShortcut.getShortcuts()) + ")"
            : "") : "");
    myCheckBox.setAlignmentX(SwingConstants.RIGHT);

    if (!SystemInfo.isMac) {
        myCheckBox.setBorder(null);
    }

    myCheckBox.setSelected(myModel.loadInitialCheckBoxState());

    if (checkBoxName == null) {
        myCheckBox.setVisible(false);
    }

    addCard(myCheckBox, CHECK_BOX_CARD);

    addCard(new HintLabel(myModel.getNotInMessage()), NOT_FOUND_IN_PROJECT_CARD);
    addCard(new HintLabel(IdeBundle.message("label.choosebyname.no.matches.found")), NOT_FOUND_CARD);
    JPanel searching = new JPanel(new BorderLayout(5, 0));
    searching.add(new AsyncProcessIcon("searching"), BorderLayout.WEST);
    searching.add(new HintLabel(IdeBundle.message("label.choosebyname.searching")), BorderLayout.CENTER);
    addCard(searching, SEARCHING_CARD);
    myCard.show(myCardContainer, CHECK_BOX_CARD);

    if (isCheckboxVisible()) {
        hBox.add(myCardContainer);
    }

    final DefaultActionGroup group = new DefaultActionGroup();
    group.add(new ShowFindUsagesAction() {
        @Override
        public PsiElement[][] getElements() {
            final Object[] objects = myListModel.toArray();
            final List<PsiElement> prefixMatchElements = new ArrayList<PsiElement>(objects.length);
            final List<PsiElement> nonPrefixMatchElements = new ArrayList<PsiElement>(objects.length);
            List<PsiElement> curElements = prefixMatchElements;
            for (Object object : objects) {
                if (object instanceof PsiElement) {
                    curElements.add((PsiElement) object);
                } else if (object instanceof DataProvider) {
                    final PsiElement psi = CommonDataKeys.PSI_ELEMENT.getData((DataProvider) object);
                    if (psi != null) {
                        curElements.add(psi);
                    }
                } else if (object == NON_PREFIX_SEPARATOR) {
                    curElements = nonPrefixMatchElements;
                }
            }
            return new PsiElement[][] { PsiUtilCore.toPsiElementArray(prefixMatchElements),
                    PsiUtilCore.toPsiElementArray(nonPrefixMatchElements) };
        }
    });
    final ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN,
            group, true);
    actionToolbar.setLayoutPolicy(ActionToolbar.NOWRAP_LAYOUT_POLICY);
    final JComponent toolbarComponent = actionToolbar.getComponent();
    toolbarComponent.setBorder(null);

    if (myToolArea == null) {
        myToolArea = new JLabel(EmptyIcon.create(1, 24));
    }
    hBox.add(myToolArea);
    hBox.add(toolbarComponent);

    myTextFieldPanel.add(caption2Tools);

    final ActionMap actionMap = new ActionMap();
    actionMap.setParent(myTextField.getActionMap());
    actionMap.put(DefaultEditorKit.copyAction, new AbstractAction() {
        @Override
        public void actionPerformed(@NotNull ActionEvent e) {
            if (myTextField.getSelectedText() != null) {
                actionMap.getParent().get(DefaultEditorKit.copyAction).actionPerformed(e);
                return;
            }
            final Object chosenElement = getChosenElement();
            if (chosenElement instanceof PsiElement) {
                CopyReferenceAction.doCopy((PsiElement) chosenElement, myProject);
            }
        }
    });
    myTextField.setActionMap(actionMap);

    myTextFieldPanel.add(myTextField);
    Font editorFont = getEditorFont();
    myTextField.setFont(editorFont);

    if (checkBoxName != null) {
        if (myCheckBox != null && myCheckBoxShortcut != null) {
            new AnAction("change goto check box", null, null) {
                @RequiredDispatchThread
                @Override
                public void actionPerformed(@NotNull AnActionEvent e) {
                    myCheckBox.setSelected(!myCheckBox.isSelected());
                }
            }.registerCustomShortcutSet(myCheckBoxShortcut, myTextField);
        }
    }

    if (isCloseByFocusLost()) {
        myTextField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(@NotNull final FocusEvent e) {
                cancelListUpdater(); // cancel thread as early as possible
                myHideAlarm.addRequest(new Runnable() {
                    @Override
                    public void run() {
                        JBPopup popup = JBPopupFactory.getInstance().getChildFocusedPopup(e.getComponent());
                        if (popup != null) {
                            popup.addListener(new JBPopupListener.Adapter() {
                                @Override
                                public void onClosed(@NotNull LightweightWindowEvent event) {
                                    if (event.isOk()) {
                                        hideHint();
                                    }
                                }
                            });
                        } else {
                            Component oppositeComponent = e.getOppositeComponent();
                            if (oppositeComponent == myCheckBox) {
                                IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
                                return;
                            }
                            if (oppositeComponent != null && !(oppositeComponent instanceof JFrame)
                                    && myList.isShowing() && (oppositeComponent == myList
                                            || SwingUtilities.isDescendingFrom(myList, oppositeComponent))) {
                                IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);// Otherwise me may skip some KeyEvents
                                return;
                            }

                            if (oppositeComponent != null) {
                                ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(myProject);
                                ToolWindow toolWindow = toolWindowManager
                                        .getToolWindow(toolWindowManager.getActiveToolWindowId());
                                if (toolWindow != null) {
                                    JComponent toolWindowComponent = toolWindow.getComponent();
                                    if (SwingUtilities.isDescendingFrom(oppositeComponent,
                                            toolWindowComponent)) {
                                        return; // Allow toolwindows to gain focus (used by QuickDoc shown in a toolwindow)
                                    }
                                }
                            }

                            EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
                            if (queue instanceof IdeEventQueue) {
                                if (!((IdeEventQueue) queue).wasRootRecentlyClicked(oppositeComponent)) {
                                    Component root = SwingUtilities.getRoot(myTextField);
                                    if (root != null && root.isShowing()) {
                                        IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
                                        return;
                                    }
                                }
                            }

                            hideHint();
                        }
                    }
                }, 5);
            }
        });
    }

    if (myCheckBox != null) {
        myCheckBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(@NotNull ItemEvent e) {
                rebuildList(false);
            }
        });
        myCheckBox.setFocusable(false);
    }

    myTextField.getDocument().addDocumentListener(new DocumentAdapter() {
        @Override
        protected void textChanged(DocumentEvent e) {
            clearPostponedOkAction(false);
            rebuildList(false);
        }
    });

    final Set<KeyStroke> upShortcuts = getShortcuts(IdeActions.ACTION_EDITOR_MOVE_CARET_UP);
    final Set<KeyStroke> downShortcuts = getShortcuts(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN);
    myTextField.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(@NotNull KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER && (e.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
                myClosedByShiftEnter = true;
                close(true);
            }
            if (!myListScrollPane.isVisible()) {
                return;
            }
            final int keyCode;

            // Add support for user-defined 'caret up/down' shortcuts.
            KeyStroke stroke = KeyStroke.getKeyStrokeForEvent(e);
            if (upShortcuts.contains(stroke)) {
                keyCode = KeyEvent.VK_UP;
            } else if (downShortcuts.contains(stroke)) {
                keyCode = KeyEvent.VK_DOWN;
            } else {
                keyCode = e.getKeyCode();
            }
            switch (keyCode) {
            case KeyEvent.VK_DOWN:
                ListScrollingUtil.moveDown(myList, e.getModifiersEx());
                break;
            case KeyEvent.VK_UP:
                ListScrollingUtil.moveUp(myList, e.getModifiersEx());
                break;
            case KeyEvent.VK_PAGE_UP:
                ListScrollingUtil.movePageUp(myList);
                break;
            case KeyEvent.VK_PAGE_DOWN:
                ListScrollingUtil.movePageDown(myList);
                break;
            case KeyEvent.VK_TAB:
                close(true);
                break;
            case KeyEvent.VK_ENTER:
                if (myList.getSelectedValue() == EXTRA_ELEM) {
                    myMaximumListSizeLimit += myListSizeIncreasing;
                    rebuildList(myList.getSelectedIndex(), myRebuildDelay, ModalityState.current(), null);
                    e.consume();
                }
                break;
            }

            if (myList.getSelectedValue() == NON_PREFIX_SEPARATOR) {
                if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_PAGE_UP) {
                    ListScrollingUtil.moveUp(myList, e.getModifiersEx());
                } else {
                    ListScrollingUtil.moveDown(myList, e.getModifiersEx());
                }
            }
        }
    });

    myTextField.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(@NotNull ActionEvent actionEvent) {
            doClose(true);
        }
    });

    myList.setFocusable(false);
    myList.setSelectionMode(allowMultipleSelection ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
            : ListSelectionModel.SINGLE_SELECTION);
    new ClickListener() {
        @Override
        public boolean onClick(@NotNull MouseEvent e, int clickCount) {
            if (!myTextField.hasFocus()) {
                IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
            }

            if (clickCount == 2) {
                int selectedIndex = myList.getSelectedIndex();
                Rectangle selectedCellBounds = myList.getCellBounds(selectedIndex, selectedIndex);

                if (selectedCellBounds != null && selectedCellBounds.contains(e.getPoint())) { // Otherwise it was reselected in the selection listener
                    if (myList.getSelectedValue() == EXTRA_ELEM) {
                        myMaximumListSizeLimit += myListSizeIncreasing;
                        rebuildList(selectedIndex, myRebuildDelay, ModalityState.current(), null);
                    } else {
                        doClose(true);
                    }
                }
                return true;
            }

            return false;
        }
    }.installOn(myList);

    myList.setCellRenderer(myModel.getListCellRenderer());
    myList.setFont(editorFont);

    myList.addListSelectionListener(new ListSelectionListener() {
        private int myPreviousSelectionIndex = 0;

        @Override
        public void valueChanged(@NotNull ListSelectionEvent e) {
            if (myList.getSelectedValue() != NON_PREFIX_SEPARATOR) {
                myPreviousSelectionIndex = myList.getSelectedIndex();
                chosenElementMightChange();
                updateDocumentation();
            } else if (allowMultipleSelection) {
                myList.setSelectedIndex(myPreviousSelectionIndex);
            }
        }
    });

    myListScrollPane = ScrollPaneFactory.createScrollPane(myList);
    myListScrollPane.setViewportBorder(new EmptyBorder(0, 0, 0, 0));

    myTextFieldPanel.setBorder(new EmptyBorder(2, 2, 2, 2));

    showTextFieldPanel();

    myInitialized = true;

    if (modalityState != null) {
        rebuildList(myInitialIndex, 0, modalityState, null);
    }
}

From source file:com.intellij.vcs.log.ui.filter.MultilinePopupBuilder.java

License:Apache License

@NotNull
JBPopup createPopup() {/*from  w  w w .  j  av  a  2  s.co  m*/
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(myTextField, BorderLayout.CENTER);
    ComponentPopupBuilder builder = JBPopupFactory.getInstance().createComponentPopupBuilder(panel, myTextField)
            .setCancelOnClickOutside(true)
            .setAdText(KeymapUtil.getShortcutsText(CommonShortcuts.CTRL_ENTER.getShortcuts()) + " to finish")
            .setRequestFocus(true).setResizable(true).setMayBeParent(true);

    final JBPopup popup = builder.createPopup();
    popup.setMinimumSize(new Dimension(200, 90));
    AnAction okAction = new DumbAwareAction() {
        @Override
        public void actionPerformed(@NotNull AnActionEvent e) {
            unregisterCustomShortcutSet(popup.getContent());
            popup.closeOk(e.getInputEvent());
        }
    };
    okAction.registerCustomShortcutSet(CommonShortcuts.CTRL_ENTER, popup.getContent());
    return popup;
}

From source file:org.jetbrains.idea.maven.tasks.MavenShortcutsManager.java

License:Apache License

public String getDescription(MavenProject project, String goal) {
    String actionId = getActionId(project.getPath(), goal);
    if (actionId == null) {
        return "";
    }//w w w.j a  va  2  s  . c  om

    Keymap activeKeymap = KeymapManager.getInstance().getActiveKeymap();
    Shortcut[] shortcuts = activeKeymap.getShortcuts(actionId);
    if (shortcuts == null || shortcuts.length == 0) {
        return "";
    }

    return KeymapUtil.getShortcutsText(shortcuts);
}