Search in sources :

Example 6 with BaseListPopupStep

use of com.intellij.openapi.ui.popup.util.BaseListPopupStep in project intellij-community by JetBrains.

the class XDebuggerUtilImpl method toggleAndReturnLineBreakpoint.

@NotNull
public static <P extends XBreakpointProperties> Promise<XLineBreakpoint> toggleAndReturnLineBreakpoint(@NotNull final Project project, @NotNull final XLineBreakpointType<P> type, @NotNull final XSourcePosition position, final boolean temporary, @Nullable final Editor editor, boolean canRemove) {
    return new WriteAction<Promise<XLineBreakpoint>>() {

        @Override
        protected void run(@NotNull Result<Promise<XLineBreakpoint>> result) throws Throwable {
            final VirtualFile file = position.getFile();
            final int line = position.getLine();
            final XBreakpointManager breakpointManager = XDebuggerManager.getInstance(project).getBreakpointManager();
            XLineBreakpoint<P> breakpoint = breakpointManager.findBreakpointAtLine(type, file, line);
            if (breakpoint != null) {
                if (!temporary && canRemove) {
                    breakpointManager.removeBreakpoint(breakpoint);
                }
            } else {
                List<? extends XLineBreakpointType<P>.XLineBreakpointVariant<P>> variants = type.computeVariants(project, position);
                if (!variants.isEmpty() && editor != null) {
                    RelativePoint relativePoint = DebuggerUIUtil.getPositionForPopup(editor, line);
                    if (variants.size() > 1 && relativePoint != null) {
                        final AsyncPromise<XLineBreakpoint> res = new AsyncPromise<>();
                        class MySelectionListener implements ListSelectionListener {

                            RangeHighlighter myHighlighter = null;

                            @Override
                            public void valueChanged(ListSelectionEvent e) {
                                if (!e.getValueIsAdjusting()) {
                                    updateHighlighter(((JList) e.getSource()).getSelectedValue());
                                }
                            }

                            public void initialSet(Object value) {
                                if (myHighlighter == null) {
                                    updateHighlighter(value);
                                }
                            }

                            void updateHighlighter(Object value) {
                                clearHighlighter();
                                if (value instanceof XLineBreakpointType.XLineBreakpointVariant) {
                                    TextRange range = ((XLineBreakpointType.XLineBreakpointVariant) value).getHighlightRange();
                                    TextRange lineRange = DocumentUtil.getLineTextRange(editor.getDocument(), line);
                                    if (range != null) {
                                        range = range.intersection(lineRange);
                                    } else {
                                        range = lineRange;
                                    }
                                    if (range != null && !range.isEmpty()) {
                                        EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
                                        TextAttributes attributes = scheme.getAttributes(DebuggerColors.BREAKPOINT_ATTRIBUTES);
                                        myHighlighter = editor.getMarkupModel().addRangeHighlighter(range.getStartOffset(), range.getEndOffset(), DebuggerColors.BREAKPOINT_HIGHLIGHTER_LAYER, attributes, HighlighterTargetArea.EXACT_RANGE);
                                    }
                                }
                            }

                            private void clearHighlighter() {
                                if (myHighlighter != null) {
                                    myHighlighter.dispose();
                                }
                            }
                        }
                        // calculate default item
                        int caretOffset = editor.getCaretModel().getOffset();
                        XLineBreakpointType<P>.XLineBreakpointVariant<P> defaultVariant = null;
                        for (XLineBreakpointType<P>.XLineBreakpointVariant<P> variant : variants) {
                            TextRange range = variant.getHighlightRange();
                            if (range != null && range.contains(caretOffset)) {
                                //noinspection ConstantConditions
                                if (defaultVariant == null || defaultVariant.getHighlightRange().getLength() > range.getLength()) {
                                    defaultVariant = variant;
                                }
                            }
                        }
                        final int defaultIndex = defaultVariant != null ? variants.indexOf(defaultVariant) : 0;
                        final MySelectionListener selectionListener = new MySelectionListener();
                        ListPopupImpl popup = new ListPopupImpl(new BaseListPopupStep<XLineBreakpointType.XLineBreakpointVariant>("Set Breakpoint", variants) {

                            @NotNull
                            @Override
                            public String getTextFor(XLineBreakpointType.XLineBreakpointVariant value) {
                                return value.getText();
                            }

                            @Override
                            public Icon getIconFor(XLineBreakpointType.XLineBreakpointVariant value) {
                                return value.getIcon();
                            }

                            @Override
                            public void canceled() {
                                selectionListener.clearHighlighter();
                            }

                            @Override
                            public PopupStep onChosen(final XLineBreakpointType.XLineBreakpointVariant selectedValue, boolean finalChoice) {
                                selectionListener.clearHighlighter();
                                ApplicationManager.getApplication().runWriteAction(() -> {
                                    P properties = (P) selectedValue.createProperties();
                                    res.setResult(breakpointManager.addLineBreakpoint(type, file.getUrl(), line, properties, temporary));
                                });
                                return FINAL_CHOICE;
                            }

                            @Override
                            public int getDefaultOptionIndex() {
                                return defaultIndex;
                            }
                        }) {

                            @Override
                            protected void afterShow() {
                                super.afterShow();
                                selectionListener.initialSet(getList().getSelectedValue());
                            }
                        };
                        DebuggerUIUtil.registerExtraHandleShortcuts(popup, IdeActions.ACTION_TOGGLE_LINE_BREAKPOINT);
                        popup.setAdText(DebuggerUIUtil.getSelectionShortcutsAdText(IdeActions.ACTION_TOGGLE_LINE_BREAKPOINT));
                        popup.addListSelectionListener(selectionListener);
                        popup.show(relativePoint);
                        result.setResult(res);
                        return;
                    } else {
                        P properties = variants.get(0).createProperties();
                        result.setResult(Promise.resolve(breakpointManager.addLineBreakpoint(type, file.getUrl(), line, properties, temporary)));
                        return;
                    }
                }
                P properties = type.createBreakpointProperties(file, line);
                result.setResult(Promise.resolve(breakpointManager.addLineBreakpoint(type, file.getUrl(), line, properties, temporary)));
                return;
            }
            result.setResult(rejectedPromise());
        }
    }.execute().getResultObject();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ListSelectionEvent(javax.swing.event.ListSelectionEvent) RelativePoint(com.intellij.ui.awt.RelativePoint) AsyncPromise(org.jetbrains.concurrency.AsyncPromise) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) PopupStep(com.intellij.openapi.ui.popup.PopupStep) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) WriteAction(com.intellij.openapi.application.WriteAction) TextRange(com.intellij.openapi.util.TextRange) RelativePoint(com.intellij.ui.awt.RelativePoint) ListSelectionListener(javax.swing.event.ListSelectionListener) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with BaseListPopupStep

use of com.intellij.openapi.ui.popup.util.BaseListPopupStep in project intellij-community by JetBrains.

the class ExpandStaticImportAction method invoke.

public void invoke(final Project project, final PsiFile file, final Editor editor, PsiElement element) {
    final PsiJavaCodeReferenceElement refExpr = (PsiJavaCodeReferenceElement) element.getParent();
    final PsiImportStaticStatement staticImport = (PsiImportStaticStatement) getImportStaticStatement(refExpr);
    final List<PsiJavaCodeReferenceElement> expressionToExpand = collectReferencesThrough(file, refExpr, staticImport);
    if (expressionToExpand.isEmpty()) {
        expand(refExpr, staticImport);
        staticImport.delete();
    } else {
        if (ApplicationManager.getApplication().isUnitTestMode() || refExpr instanceof PsiImportStaticReferenceElement) {
            replaceAllAndDeleteImport(expressionToExpand, refExpr, staticImport);
        } else {
            final BaseListPopupStep<String> step = new BaseListPopupStep<String>("Multiple Usages of the Static Import Found", REPLACE_THIS_OCCURRENCE, REPLACE_ALL_AND_DELETE_IMPORT) {

                @Override
                public PopupStep onChosen(final String selectedValue, boolean finalChoice) {
                    new WriteCommandAction(project, ExpandStaticImportAction.this.getText()) {

                        @Override
                        protected void run(@NotNull Result result) throws Throwable {
                            if (selectedValue == REPLACE_THIS_OCCURRENCE) {
                                expand(refExpr, staticImport);
                            } else {
                                replaceAllAndDeleteImport(expressionToExpand, refExpr, staticImport);
                            }
                        }
                    }.execute();
                    return FINAL_CHOICE;
                }
            };
            JBPopupFactory.getInstance().createListPopup(step).showInBestPositionFor(editor);
        }
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) Result(com.intellij.openapi.application.Result)

Example 8 with BaseListPopupStep

use of com.intellij.openapi.ui.popup.util.BaseListPopupStep in project intellij-community by JetBrains.

the class AddImportAction method chooseClassAndImport.

private void chooseClassAndImport() {
    CodeInsightUtil.sortIdenticalShortNamedMembers(myTargetClasses, myReference);
    final BaseListPopupStep<PsiClass> step = new BaseListPopupStep<PsiClass>(QuickFixBundle.message("class.to.import.chooser.title"), myTargetClasses) {

        @Override
        public boolean isAutoSelectionEnabled() {
            return false;
        }

        @Override
        public boolean isSpeedSearchEnabled() {
            return true;
        }

        @Override
        public PopupStep onChosen(PsiClass selectedValue, boolean finalChoice) {
            if (selectedValue == null) {
                return FINAL_CHOICE;
            }
            if (finalChoice) {
                return doFinalStep(() -> {
                    PsiDocumentManager.getInstance(myProject).commitAllDocuments();
                    addImport(myReference, selectedValue);
                });
            }
            return getExcludesStep(selectedValue.getQualifiedName(), myProject);
        }

        @Override
        public boolean hasSubstep(PsiClass selectedValue) {
            return true;
        }

        @NotNull
        @Override
        public String getTextFor(PsiClass value) {
            return ObjectUtils.assertNotNull(value.getQualifiedName());
        }

        @Override
        public Icon getIconFor(PsiClass aValue) {
            return aValue.getIcon(0);
        }
    };
    ListPopupImpl popup = new ListPopupImpl(step) {

        @Override
        protected ListCellRenderer getListElementRenderer() {
            final PopupListElementRenderer baseRenderer = (PopupListElementRenderer) super.getListElementRenderer();
            final DefaultPsiElementCellRenderer psiRenderer = new DefaultPsiElementCellRenderer();
            return new ListCellRenderer() {

                @Override
                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                    JPanel panel = new JPanel(new BorderLayout());
                    baseRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    panel.add(baseRenderer.getNextStepLabel(), BorderLayout.EAST);
                    panel.add(psiRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus));
                    return panel;
                }
            };
        }
    };
    NavigationUtil.hidePopupIfDumbModeStarts(popup, myProject);
    popup.showInBestPositionFor(myEditor);
}
Also used : DefaultPsiElementCellRenderer(com.intellij.ide.util.DefaultPsiElementCellRenderer) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) PsiClass(com.intellij.psi.PsiClass) PopupListElementRenderer(com.intellij.ui.popup.list.PopupListElementRenderer)

Example 9 with BaseListPopupStep

use of com.intellij.openapi.ui.popup.util.BaseListPopupStep in project intellij-community by JetBrains.

the class PopupFactoryImpl method createConfirmation.

@NotNull
@Override
public ListPopup createConfirmation(String title, final String yesText, String noText, final Runnable onYes, final Runnable onNo, int defaultOptionIndex) {
    final BaseListPopupStep<String> step = new BaseListPopupStep<String>(title, new String[] { yesText, noText }) {

        @Override
        public PopupStep onChosen(String selectedValue, final boolean finalChoice) {
            if (selectedValue.equals(yesText)) {
                onYes.run();
            } else {
                onNo.run();
            }
            return FINAL_CHOICE;
        }

        @Override
        public void canceled() {
            onNo.run();
        }

        @Override
        public boolean isMnemonicsNavigationEnabled() {
            return true;
        }
    };
    step.setDefaultOptionIndex(defaultOptionIndex);
    final ApplicationEx app = ApplicationManagerEx.getApplicationEx();
    return app == null || !app.isUnitTestMode() ? new ListPopupImpl(step) : new MockConfirmation(step, yesText);
}
Also used : ApplicationEx(com.intellij.openapi.application.ex.ApplicationEx) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) MockConfirmation(com.intellij.ui.popup.mock.MockConfirmation) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with BaseListPopupStep

use of com.intellij.openapi.ui.popup.util.BaseListPopupStep in project android by JetBrains.

the class AbstractDependenciesPanel method createActionsPanel.

@NotNull
protected final JPanel createActionsPanel() {
    JPanel actionsPanel = new JPanel(new BorderLayout());
    DefaultActionGroup actions = new DefaultActionGroup();
    AnAction addDependencyAction = new DumbAwareAction("Add Dependency", "", IconUtil.getAddIcon()) {

        @Override
        public void actionPerformed(AnActionEvent e) {
            JBPopup popup = JBPopupFactory.getInstance().createListPopup(new BaseListPopupStep<AbstractPopupAction>(null, getPopupActions()) {

                @Override
                public Icon getIconFor(AbstractPopupAction action) {
                    return action.icon;
                }

                @Override
                public boolean isMnemonicsNavigationEnabled() {
                    return true;
                }

                @Override
                public PopupStep onChosen(AbstractPopupAction action, boolean finalChoice) {
                    return doFinalStep(action::execute);
                }

                @Override
                @NotNull
                public String getTextFor(AbstractPopupAction action) {
                    return "&" + action.index + "  " + action.text;
                }
            });
            popup.show(new RelativePoint(actionsPanel, new Point(0, actionsPanel.getHeight() - 1)));
        }
    };
    actions.add(addDependencyAction);
    List<AnAction> extraToolbarActions = getExtraToolbarActions();
    if (!extraToolbarActions.isEmpty()) {
        actions.addSeparator();
        actions.addAll(extraToolbarActions);
    }
    ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar("TOP", actions, true);
    JComponent toolbarComponent = toolbar.getComponent();
    toolbarComponent.setBorder(IdeBorderFactory.createBorder(SideBorder.BOTTOM));
    actionsPanel.add(toolbarComponent, BorderLayout.CENTER);
    return actionsPanel;
}
Also used : RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) NotNull(org.jetbrains.annotations.NotNull) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) PopupStep(com.intellij.openapi.ui.popup.PopupStep) JBPopup(com.intellij.openapi.ui.popup.JBPopup) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

BaseListPopupStep (com.intellij.openapi.ui.popup.util.BaseListPopupStep)33 PopupStep (com.intellij.openapi.ui.popup.PopupStep)18 NotNull (org.jetbrains.annotations.NotNull)15 ListPopup (com.intellij.openapi.ui.popup.ListPopup)10 ListPopupImpl (com.intellij.ui.popup.list.ListPopupImpl)10 RelativePoint (com.intellij.ui.awt.RelativePoint)8 Project (com.intellij.openapi.project.Project)7 JBPopupFactory (com.intellij.openapi.ui.popup.JBPopupFactory)5 JBPopup (com.intellij.openapi.ui.popup.JBPopup)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 PsiFile (com.intellij.psi.PsiFile)4 Nullable (org.jetbrains.annotations.Nullable)3 AllIcons (com.intellij.icons.AllIcons)2 DefaultPsiElementCellRenderer (com.intellij.ide.util.DefaultPsiElementCellRenderer)2 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 Editor (com.intellij.openapi.editor.Editor)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)2 FileChooser (com.intellij.openapi.fileChooser.FileChooser)2 FileChooserDescriptor (com.intellij.openapi.fileChooser.FileChooserDescriptor)2