Search in sources :

Example 1 with ListPopupImpl

use of com.intellij.ui.popup.list.ListPopupImpl in project intellij-community by JetBrains.

the class SwitchTaskAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    DataContext dataContext = e.getDataContext();
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    assert project != null;
    ListPopupImpl popup = createPopup(dataContext, null, true);
    popup.showCenteredInCurrentWindow(project);
}
Also used : Project(com.intellij.openapi.project.Project) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl)

Example 2 with ListPopupImpl

use of com.intellij.ui.popup.list.ListPopupImpl in project intellij-community by JetBrains.

the class SwitchTaskAction method createPopup.

private static ListPopupImpl createPopup(final DataContext dataContext, @Nullable final Runnable onDispose, boolean withTitle) {
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    final Ref<Boolean> shiftPressed = Ref.create(false);
    final Ref<JComponent> componentRef = Ref.create();
    List<TaskListItem> items = project == null ? Collections.<TaskListItem>emptyList() : createPopupActionGroup(project, shiftPressed, PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext));
    final String title = withTitle ? "Switch to Task" : null;
    ListPopupStep<TaskListItem> step = new MultiSelectionListPopupStep<TaskListItem>(title, items) {

        @Override
        public PopupStep<?> onChosen(List<TaskListItem> selectedValues, boolean finalChoice) {
            if (finalChoice) {
                selectedValues.get(0).select();
                return FINAL_CHOICE;
            }
            ActionGroup group = createActionsStep(selectedValues, project, shiftPressed);
            return JBPopupFactory.getInstance().createActionsStep(group, DataManager.getInstance().getDataContext(componentRef.get()), false, false, null, null, true);
        }

        @Override
        public Icon getIconFor(TaskListItem aValue) {
            return aValue.getIcon();
        }

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

        @Nullable
        @Override
        public ListSeparator getSeparatorAbove(TaskListItem value) {
            return value.getSeparator() == null ? null : new ListSeparator(value.getSeparator());
        }

        @Override
        public boolean hasSubstep(List<TaskListItem> selectedValues) {
            return selectedValues.size() > 1 || selectedValues.get(0).getTask() != null;
        }
    };
    final ListPopupImpl popup = (ListPopupImpl) JBPopupFactory.getInstance().createListPopup(step);
    if (onDispose != null) {
        Disposer.register(popup, new Disposable() {

            @Override
            public void dispose() {
                onDispose.run();
            }
        });
    }
    componentRef.set(popup.getComponent());
    if (items.size() <= 2) {
        return popup;
    }
    popup.setAdText("Press SHIFT to merge with current context");
    popup.registerAction("shiftPressed", KeyStroke.getKeyStroke("shift pressed SHIFT"), new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            shiftPressed.set(true);
            popup.setCaption("Merge with Current Context");
        }
    });
    popup.registerAction("shiftReleased", KeyStroke.getKeyStroke("released SHIFT"), new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            shiftPressed.set(false);
            popup.setCaption("Switch to Task");
        }
    });
    popup.registerAction("invoke", KeyStroke.getKeyStroke("shift ENTER"), new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            popup.handleSelect(true);
        }
    });
    return popup;
}
Also used : Disposable(com.intellij.openapi.Disposable) ActionEvent(java.awt.event.ActionEvent) Project(com.intellij.openapi.project.Project) SimpleActionGroup(com.intellij.tools.SimpleActionGroup) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) LocalChangeList(com.intellij.openapi.vcs.changes.LocalChangeList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with ListPopupImpl

use of com.intellij.ui.popup.list.ListPopupImpl in project intellij-community by JetBrains.

the class BuildArtifactAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = getEventProject(e);
    if (project == null)
        return;
    final List<Artifact> artifacts = ArtifactUtil.getArtifactWithOutputPaths(project);
    if (artifacts.isEmpty())
        return;
    List<ArtifactPopupItem> items = new ArrayList<>();
    if (artifacts.size() > 1) {
        items.add(0, new ArtifactPopupItem(null, "All Artifacts", EmptyIcon.ICON_16));
    }
    Set<Artifact> selectedArtifacts = new HashSet<>(ArtifactsWorkspaceSettings.getInstance(project).getArtifactsToBuild());
    TIntArrayList selectedIndices = new TIntArrayList();
    if (Comparing.haveEqualElements(artifacts, selectedArtifacts) && selectedArtifacts.size() > 1) {
        selectedIndices.add(0);
        selectedArtifacts.clear();
    }
    for (Artifact artifact : artifacts) {
        final ArtifactPopupItem item = new ArtifactPopupItem(artifact, artifact.getName(), artifact.getArtifactType().getIcon());
        if (selectedArtifacts.contains(artifact)) {
            selectedIndices.add(items.size());
        }
        items.add(item);
    }
    final ProjectSettingsService projectSettingsService = ProjectSettingsService.getInstance(project);
    final ArtifactAwareProjectSettingsService settingsService = projectSettingsService instanceof ArtifactAwareProjectSettingsService ? (ArtifactAwareProjectSettingsService) projectSettingsService : null;
    final ChooseArtifactStep step = new ChooseArtifactStep(items, artifacts.get(0), project, settingsService);
    step.setDefaultOptionIndices(selectedIndices.toNativeArray());
    final ListPopupImpl popup = (ListPopupImpl) JBPopupFactory.getInstance().createListPopup(step);
    final KeyStroke editKeyStroke = KeymapUtil.getKeyStroke(CommonShortcuts.getEditSource());
    if (settingsService != null && editKeyStroke != null) {
        popup.registerAction("editArtifact", editKeyStroke, new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Object[] values = popup.getSelectedValues();
                popup.cancel();
                settingsService.openArtifactSettings(values.length > 0 ? ((ArtifactPopupItem) values[0]).getArtifact() : null);
            }
        });
    }
    popup.showCenteredInCurrentWindow(project);
}
Also used : ActionEvent(java.awt.event.ActionEvent) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) TIntArrayList(gnu.trove.TIntArrayList) Artifact(com.intellij.packaging.artifacts.Artifact) TIntArrayList(gnu.trove.TIntArrayList) Project(com.intellij.openapi.project.Project) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) ProjectSettingsService(com.intellij.openapi.roots.ui.configuration.ProjectSettingsService)

Example 4 with ListPopupImpl

use of com.intellij.ui.popup.list.ListPopupImpl in project intellij-community by JetBrains.

the class ExpressionInputComponent method showHistory.

private void showHistory() {
    List<XExpression> expressions = myExpressionEditor.getRecentExpressions();
    if (!expressions.isEmpty()) {
        ListPopupImpl popup = new ListPopupImpl(new BaseListPopupStep<XExpression>(null, expressions) {

            @Override
            public PopupStep onChosen(XExpression selectedValue, boolean finalChoice) {
                myExpressionEditor.setExpression(selectedValue);
                myExpressionEditor.requestFocusInEditor();
                return FINAL_CHOICE;
            }
        }) {

            @Override
            protected ListCellRenderer getListElementRenderer() {
                return new ColoredListCellRenderer<XExpression>() {

                    @Override
                    protected void customizeCellRenderer(@NotNull JList list, XExpression value, int index, boolean selected, boolean hasFocus) {
                        append(value.getExpression());
                    }
                };
            }
        };
        popup.getList().setFont(EditorUtil.getEditorFont());
        popup.showUnderneathOf(myExpressionEditor.getEditorComponent());
    }
}
Also used : ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) XExpression(com.intellij.xdebugger.XExpression) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) NotNull(org.jetbrains.annotations.NotNull) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) PopupStep(com.intellij.openapi.ui.popup.PopupStep)

Example 5 with ListPopupImpl

use of com.intellij.ui.popup.list.ListPopupImpl 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)

Aggregations

ListPopupImpl (com.intellij.ui.popup.list.ListPopupImpl)16 BaseListPopupStep (com.intellij.openapi.ui.popup.util.BaseListPopupStep)10 Project (com.intellij.openapi.project.Project)5 PopupStep (com.intellij.openapi.ui.popup.PopupStep)5 NotNull (org.jetbrains.annotations.NotNull)4 ColoredListCellRenderer (com.intellij.ui.ColoredListCellRenderer)3 RelativePoint (com.intellij.ui.awt.RelativePoint)3 PopupListElementRenderer (com.intellij.ui.popup.list.PopupListElementRenderer)3 ActionEvent (java.awt.event.ActionEvent)3 ListSelectionEvent (javax.swing.event.ListSelectionEvent)3 ListSelectionListener (javax.swing.event.ListSelectionListener)3 DefaultPsiElementCellRenderer (com.intellij.ide.util.DefaultPsiElementCellRenderer)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)2 XExpression (com.intellij.xdebugger.XExpression)2 PsiElementListCellRenderer (com.intellij.ide.util.PsiElementListCellRenderer)1 GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)1 Disposable (com.intellij.openapi.Disposable)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 Result (com.intellij.openapi.application.Result)1 WriteAction (com.intellij.openapi.application.WriteAction)1