Search in sources :

Example 1 with HeldDownKeyListener

use of com.intellij.ui.HeldDownKeyListener in project intellij-community by JetBrains.

the class GotoActionAction method createPopup.

@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);
    }
    final Disposable disposable = Disposer.newDisposable();
    final ChooseByNamePopup popup = new ChooseByNamePopup(project, model, new GotoActionItemProvider(model), oldPopup, initialText, false, initialIndex) {

        private boolean myPaintInternalInfo;

        @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);
                    }
                    String description = getValueDescription(value);
                    ActionMenu.showDescriptionInStatusBar(true, myList, description);
                }

                @Nullable
                private String getText(@Nullable Object o) {
                    if (o instanceof GotoActionModel.MatchedValue) {
                        GotoActionModel.MatchedValue mv = (GotoActionModel.MatchedValue) o;
                        if (myPaintInternalInfo) {
                            if (mv.value instanceof GotoActionModel.ActionWrapper) {
                                AnAction action = ((GotoActionModel.ActionWrapper) mv.value).getAction();
                                String actionId = ActionManager.getInstance().getId(action);
                                return StringUtil.notNullize(actionId, "class: " + action.getClass().getName());
                            }
                        }
                        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();
                }
            });
            myList.addMouseMotionListener(new MouseMotionAdapter() {

                @Override
                public void mouseMoved(MouseEvent e) {
                    int index = myList.locationToIndex(e.getPoint());
                    if (index == -1)
                        return;
                    Object value = myList.getModel().getElementAt(index);
                    String description = getValueDescription(value);
                    ActionMenu.showDescriptionInStatusBar(true, myList, description);
                }
            });
            if (Registry.is("show.configurables.ids.in.settings")) {
                new HeldDownKeyListener() {

                    @Override
                    protected void heldKeyTriggered(JComponent component, boolean pressed) {
                        myPaintInternalInfo = pressed;
                        // an easy way to repaint the AdText
                        ListSelectionEvent event = new ListSelectionEvent(this, -1, -1, false);
                        for (ListSelectionListener listener : myList.getListSelectionListeners()) {
                            listener.valueChanged(event);
                        }
                    }
                }.installOn(myTextField);
            }
        }

        @Nullable
        private String getValueDescription(@Nullable Object value) {
            if (value instanceof GotoActionModel.MatchedValue) {
                GotoActionModel.MatchedValue mv = (GotoActionModel.MatchedValue) value;
                if (mv.value instanceof GotoActionModel.ActionWrapper) {
                    AnAction action = ((GotoActionModel.ActionWrapper) mv.value).getAction();
                    return action.getTemplatePresentation().getDescription();
                }
            }
            return null;
        }

        @NotNull
        @Override
        protected Set<Object> filter(@NotNull Set<Object> elements) {
            return super.filter(model.sortItems(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);
        }

        @Override
        public void setDisposed(boolean disposedFlag) {
            super.setDisposed(disposedFlag);
            Disposer.dispose(disposable);
            ActionMenu.showDescriptionInStatusBar(true, myList, null);
            for (ListSelectionListener listener : myList.getListSelectionListeners()) {
                myList.removeListSelectionListener(listener);
            }
            UIUtil.dispose(myList);
        }
    };
    ApplicationManager.getApplication().getMessageBus().connect(disposable).subscribe(ProgressWindow.TOPIC, new ProgressWindow.Listener() {

        @Override
        public void progressWindowCreated(ProgressWindow pw) {
            Disposer.register(pw, new Disposable() {

                @Override
                public void dispose() {
                    if (!popup.checkDisposed()) {
                        popup.repaintList();
                    }
                }
            });
        }
    });
    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();
                }
            }
        }
    });
    CustomShortcutSet shortcutSet = new CustomShortcutSet(KeymapManager.getInstance().getActiveKeymap().getShortcuts(IdeActions.ACTION_SHOW_INTENTION_ACTIONS));
    new DumbAwareAction() {

        @Override
        public void actionPerformed(AnActionEvent e) {
            Object o = popup.getChosenElement();
            if (o instanceof GotoActionModel.MatchedValue) {
                Comparable value = ((GotoActionModel.MatchedValue) o).value;
                if (value instanceof GotoActionModel.ActionWrapper) {
                    GotoActionModel.ActionWrapper aw = (GotoActionModel.ActionWrapper) value;
                    boolean available = aw.isAvailable();
                    if (available) {
                        AnAction action = aw.getAction();
                        String id = ActionManager.getInstance().getId(action);
                        KeymapManagerImpl km = ((KeymapManagerImpl) KeymapManager.getInstance());
                        Keymap k = km.getActiveKeymap();
                        if (!k.canModify())
                            return;
                        KeymapPanel.addKeyboardShortcut(id, ActionShortcutRestrictions.getInstance().getForActionId(id), k, component);
                        popup.repaintListImmediate();
                    }
                }
            }
        }
    }.registerCustomShortcutSet(shortcutSet, popup.getTextField(), disposable);
    return popup;
}
Also used : Set(java.util.Set) ListSelectionEvent(javax.swing.event.ListSelectionEvent) NotNull(org.jetbrains.annotations.NotNull) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) GotoActionItemProvider(com.intellij.ide.util.gotoByName.GotoActionItemProvider) ChooseByNamePopup(com.intellij.ide.util.gotoByName.ChooseByNamePopup) Disposable(com.intellij.openapi.Disposable) GotoActionModel(com.intellij.ide.util.gotoByName.GotoActionModel) HeldDownKeyListener(com.intellij.ui.HeldDownKeyListener) ListSelectionListener(javax.swing.event.ListSelectionListener) ProgressWindow(com.intellij.openapi.progress.util.ProgressWindow) BooleanOptionDescription(com.intellij.ide.ui.search.BooleanOptionDescription) ModalityState(com.intellij.openapi.application.ModalityState) Nullable(org.jetbrains.annotations.Nullable) KeymapManagerImpl(com.intellij.openapi.keymap.impl.KeymapManagerImpl) Keymap(com.intellij.openapi.keymap.Keymap) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

BooleanOptionDescription (com.intellij.ide.ui.search.BooleanOptionDescription)1 ChooseByNamePopup (com.intellij.ide.util.gotoByName.ChooseByNamePopup)1 GotoActionItemProvider (com.intellij.ide.util.gotoByName.GotoActionItemProvider)1 GotoActionModel (com.intellij.ide.util.gotoByName.GotoActionModel)1 Disposable (com.intellij.openapi.Disposable)1 ModalityState (com.intellij.openapi.application.ModalityState)1 Keymap (com.intellij.openapi.keymap.Keymap)1 KeymapManagerImpl (com.intellij.openapi.keymap.impl.KeymapManagerImpl)1 ProgressWindow (com.intellij.openapi.progress.util.ProgressWindow)1 DumbAwareAction (com.intellij.openapi.project.DumbAwareAction)1 HeldDownKeyListener (com.intellij.ui.HeldDownKeyListener)1 Set (java.util.Set)1 ListSelectionEvent (javax.swing.event.ListSelectionEvent)1 ListSelectionListener (javax.swing.event.ListSelectionListener)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1