Example usage for com.intellij.openapi.ui.popup.util BaseListPopupStep BaseListPopupStep

List of usage examples for com.intellij.openapi.ui.popup.util BaseListPopupStep BaseListPopupStep

Introduction

In this page you can find the example usage for com.intellij.openapi.ui.popup.util BaseListPopupStep BaseListPopupStep.

Prototype

public BaseListPopupStep(@Nullable String title, List<? extends T> values) 

Source Link

Usage

From source file:com.android.tools.idea.gradle.structure.configurables.ui.dependencies.AbstractDependenciesPanel.java

License:Apache License

@NotNull
protected final JPanel createActionsPanel() {
    JPanel actionsPanel = new JPanel(new BorderLayout());

    DefaultActionGroup actions = new DefaultActionGroup();

    AnAction addDependencyAction = new DumbAwareAction("Add Dependency", "", IconUtil.getAddIcon()) {
        @Override//from  w w w.  ja  v  a 2 s  .c  om
        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;
}

From source file:com.android.tools.idea.gradle.structure.editors.ModuleDependenciesPanel.java

License:Apache License

@NotNull
private JComponent createTableWithButtons() {
    myEntryTable.getSelectionModel().addListSelectionListener(e -> {
        if (e.getValueIsAdjusting()) {
            return;
        }/* www. jav a2  s .  c  om*/
        updateButtons();
    });

    final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(myEntryTable);
    decorator.setAddAction(button -> {
        ImmutableList<PopupAction> popupActions = ImmutableList
                .of(new PopupAction(AndroidIcons.MavenLogo, 1, "Library dependency") {
                    @Override
                    public void run() {
                        addExternalDependency();
                    }
                }, new PopupAction(PlatformIcons.LIBRARY_ICON, 2, "Jar dependency") {
                    @Override
                    public void run() {
                        addFileDependency();
                    }
                }, new PopupAction(AllIcons.Nodes.Module, 3, "Module dependency") {
                    @Override
                    public void run() {
                        addModuleDependency();
                    }
                });
        final JBPopup popup = JBPopupFactory.getInstance()
                .createListPopup(new BaseListPopupStep<PopupAction>(null, popupActions) {
                    @Override
                    public Icon getIconFor(PopupAction value) {
                        return value.myIcon;
                    }

                    @Override
                    public boolean hasSubstep(PopupAction value) {
                        return false;
                    }

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

                    @Override
                    public PopupStep onChosen(final PopupAction value, final boolean finalChoice) {
                        return doFinalStep(value);
                    }

                    @Override
                    @NotNull
                    public String getTextFor(PopupAction value) {
                        return "&" + value.myIndex + "  " + value.myTitle;
                    }
                });
        popup.show(button.getPreferredPopupPoint());
    });
    decorator.setRemoveAction(button -> removeSelectedItems());
    decorator.setMoveUpAction(button -> moveSelectedRows(-1));
    decorator.setMoveDownAction(button -> moveSelectedRows(+1));

    final JPanel panel = decorator.createPanel();
    myRemoveButton = ToolbarDecorator.findRemoveButton(panel);
    return panel;
}

From source file:com.android.tools.idea.structure.gradle.ModuleDependenciesPanel.java

License:Apache License

@NotNull
private JComponent createTableWithButtons() {
    myEntryTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override//from  w w w .  j  a  v a 2 s.c  o  m
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
                return;
            }
            updateButtons();
        }
    });

    final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(myEntryTable);
    decorator.setAddAction(new AnActionButtonRunnable() {
        @Override
        public void run(AnActionButton button) {
            ImmutableList<PopupAction> popupActions = ImmutableList
                    .of(new PopupAction(AndroidIcons.MavenLogo, 1, "Library dependency") {
                        @Override
                        public void run() {
                            addExternalDependency();
                        }
                    }, new PopupAction(PlatformIcons.LIBRARY_ICON, 2, "File dependency") {
                        @Override
                        public void run() {
                            addFileDependency();
                        }
                    }, new PopupAction(AllIcons.Nodes.Module, 3, "Module dependency") {
                        @Override
                        public void run() {
                            addModuleDependency();
                        }
                    });
            final JBPopup popup = JBPopupFactory.getInstance()
                    .createListPopup(new BaseListPopupStep<PopupAction>(null, popupActions) {
                        @Override
                        public Icon getIconFor(PopupAction value) {
                            return value.myIcon;
                        }

                        @Override
                        public boolean hasSubstep(PopupAction value) {
                            return false;
                        }

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

                        @Override
                        public PopupStep onChosen(final PopupAction value, final boolean finalChoice) {
                            return doFinalStep(new Runnable() {
                                @Override
                                public void run() {
                                    value.run();
                                }
                            });
                        }

                        @Override
                        @NotNull
                        public String getTextFor(PopupAction value) {
                            return "&" + value.myIndex + "  " + value.myTitle;
                        }
                    });
            popup.show(button.getPreferredPopupPoint());
        }
    });
    decorator.setRemoveAction(new AnActionButtonRunnable() {
        @Override
        public void run(AnActionButton button) {
            removeSelectedItems();
        }
    });
    decorator.setMoveUpAction(new AnActionButtonRunnable() {
        @Override
        public void run(AnActionButton button) {
            moveSelectedRows(-1);
        }
    });
    decorator.setMoveDownAction(new AnActionButtonRunnable() {
        @Override
        public void run(AnActionButton button) {
            moveSelectedRows(+1);
        }
    });

    final JPanel panel = decorator.createPanel();
    myRemoveButton = ToolbarDecorator.findRemoveButton(panel);
    return panel;
}

From source file:com.intellij.codeInsight.daemon.impl.actions.AddImportAction.java

License:Apache License

private void chooseClassAndImport() {
    CodeInsightUtil.sortIdenticalShortNameClasses(myTargetClasses, myReference);

    final BaseListPopupStep<PsiClass> step = new BaseListPopupStep<PsiClass>(
            QuickFixBundle.message("class.to.import.chooser.title"), myTargetClasses) {
        @Override// w w w.j  av  a 2s . c  o  m
        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) {
                PsiDocumentManager.getInstance(myProject).commitAllDocuments();
                addImport(myReference, selectedValue);
                return FINAL_CHOICE;
            }

            String qname = selectedValue.getQualifiedName();
            if (qname == null)
                return FINAL_CHOICE;

            List<String> toExclude = getAllExcludableStrings(qname);

            return new BaseListPopupStep<String>(null, toExclude) {
                @NotNull
                @Override
                public String getTextFor(String value) {
                    return "Exclude '" + value + "' from auto-import";
                }

                @Override
                public PopupStep onChosen(String selectedValue, boolean finalChoice) {
                    if (finalChoice) {
                        excludeFromImport(myProject, selectedValue);
                    }

                    return super.onChosen(selectedValue, finalChoice);
                }
            };
        }

        @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 IconDescriptorUpdaters.getIcon(aValue, 0);
        }
    };
    JBPopupFactory.getInstance().createListPopup(step).showInBestPositionFor(myEditor);
}

From source file:com.intellij.codeInsight.daemon.impl.quickfix.StaticImportMethodFix.java

License:Apache License

private void chooseAndImport(Editor editor, final Project project) {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        doImport(candidates.get(0));/*from   www. j  av a  2s  . c  o  m*/
        return;
    }
    final BaseListPopupStep<PsiMethod> step = new BaseListPopupStep<PsiMethod>(
            QuickFixBundle.message("class.to.import.chooser.title"), candidates) {

        @Override
        public PopupStep onChosen(PsiMethod selectedValue, boolean finalChoice) {
            if (selectedValue == null) {
                return FINAL_CHOICE;
            }

            if (finalChoice) {
                PsiDocumentManager.getInstance(project).commitAllDocuments();
                LOG.assertTrue(selectedValue.isValid());
                doImport(selectedValue);
                return FINAL_CHOICE;
            }

            String qname = getMemberQualifiedName(selectedValue);
            if (qname == null)
                return FINAL_CHOICE;
            List<String> excludableStrings = AddImportAction.getAllExcludableStrings(qname);
            return new BaseListPopupStep<String>(null, excludableStrings) {
                @NotNull
                @Override
                public String getTextFor(String value) {
                    return "Exclude '" + value + "' from auto-import";
                }

                @Override
                public PopupStep onChosen(String selectedValue, boolean finalChoice) {
                    if (finalChoice) {
                        AddImportAction.excludeFromImport(project, selectedValue);
                    }

                    return super.onChosen(selectedValue, finalChoice);
                }
            };
        }

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

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

        @Override
        public Icon getIconFor(PsiMethod aValue) {
            return IconDescriptorUpdaters.getIcon(aValue, 0);
        }
    };

    final ListPopupImpl popup = new ListPopupImpl(step) {
        final PopupListElementRenderer rightArrow = new PopupListElementRenderer(this);

        @Override
        protected ListCellRenderer getListElementRenderer() {
            return new MethodCellRenderer(true, PsiFormatUtilBase.SHOW_NAME) {
                @Override
                protected DefaultListCellRenderer getRightCellRenderer(final Object value) {
                    final DefaultListCellRenderer moduleRenderer = super.getRightCellRenderer(value);
                    return new DefaultListCellRenderer() {
                        @Override
                        public Component getListCellRendererComponent(JList list, Object value, int index,
                                boolean isSelected, boolean cellHasFocus) {
                            JPanel panel = new JPanel(new BorderLayout());
                            if (moduleRenderer != null) {
                                Component moduleComponent = moduleRenderer.getListCellRendererComponent(list,
                                        value, index, isSelected, cellHasFocus);
                                if (!isSelected) {
                                    moduleComponent.setBackground(getBackgroundColor(value));
                                }
                                panel.add(moduleComponent, BorderLayout.CENTER);
                            }
                            rightArrow.getListCellRendererComponent(list, value, index, isSelected,
                                    cellHasFocus);
                            Component rightArrowComponent = rightArrow.getNextStepLabel();
                            panel.add(rightArrowComponent, BorderLayout.EAST);
                            return panel;
                        }
                    };
                }
            };
        }
    };
    popup.showInBestPositionFor(editor);
}

From source file:com.intellij.codeInsight.ExternalAnnotationsManagerImpl.java

License:Apache License

private void chooseRootAndAnnotateExternally(@NotNull final PsiModifierListOwner listOwner,
        @NotNull final String annotationFQName, @NotNull final PsiFile fromFile, @NotNull final Project project,
        @NotNull final String packageName, @NotNull VirtualFile[] roots,
        @Nullable final PsiNameValuePair[] value) {
    if (roots.length > 1) {
        JBPopupFactory.getInstance()/*  w  w w.j  av a 2  s  .co m*/
                .createListPopup(new BaseListPopupStep<VirtualFile>("Annotation Roots", roots) {
                    @Override
                    public void canceled() {
                        notifyAfterAnnotationChanging(listOwner, annotationFQName, false);
                    }

                    @Override
                    public PopupStep onChosen(@NotNull final VirtualFile file, final boolean finalChoice) {
                        annotateExternally(file, listOwner, project, packageName, annotationFQName, fromFile,
                                value);
                        return FINAL_CHOICE;
                    }

                    @NotNull
                    @Override
                    public String getTextFor(@NotNull final VirtualFile value) {
                        return value.getPresentableUrl();
                    }

                    @Override
                    public Icon getIconFor(final VirtualFile aValue) {
                        return AllIcons.Modules.Annotation;
                    }
                }).showInBestPositionFor(DataManager.getInstance().getDataContext());
    } else {
        annotateExternally(roots[0], listOwner, project, packageName, annotationFQName, fromFile, value);
    }
}

From source file:com.intellij.codeInsight.highlighting.HighlightSuppressedWarningsHandler.java

License:Apache License

@Override
protected void selectTargets(List<PsiLiteralExpression> targets,
        final Consumer<List<PsiLiteralExpression>> selectionConsumer) {
    if (targets.size() == 1) {
        selectionConsumer.consume(targets);
    } else {/*w  w  w  . j a  v  a2 s.  c om*/
        JBPopupFactory.getInstance().createListPopup(new BaseListPopupStep<PsiLiteralExpression>(
                "Choose Inspections to Highlight Suppressed Problems from", targets) {
            @Override
            public PopupStep onChosen(PsiLiteralExpression selectedValue, boolean finalChoice) {
                selectionConsumer.consume(Collections.singletonList(selectedValue));
                return FINAL_CHOICE;
            }

            @NotNull
            @Override
            public String getTextFor(PsiLiteralExpression value) {
                final Object o = value.getValue();
                LOG.assertTrue(o instanceof String);
                return (String) o;
            }
        }).showInBestPositionFor(myEditor);
    }
}

From source file:com.intellij.codeInsight.intention.impl.DeannotateIntentionAction.java

License:Apache License

@Override
public void invoke(@NotNull final Project project, Editor editor, final PsiFile file)
        throws IncorrectOperationException {
    final PsiModifierListOwner listOwner = getContainer(editor, file);
    LOG.assertTrue(listOwner != null);//from w w  w  .j av  a 2  s .co m
    final ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(project);
    final PsiAnnotation[] externalAnnotations = annotationsManager.findExternalAnnotations(listOwner);
    LOG.assertTrue(externalAnnotations != null && externalAnnotations.length > 0);
    if (externalAnnotations.length == 1) {
        deannotate(externalAnnotations[0], project, file, annotationsManager, listOwner);
        return;
    }
    JBPopupFactory.getInstance().createListPopup(new BaseListPopupStep<PsiAnnotation>(
            CodeInsightBundle.message("deannotate.intention.chooser.title"), externalAnnotations) {
        @Override
        public PopupStep onChosen(final PsiAnnotation selectedValue, final boolean finalChoice) {
            deannotate(selectedValue, project, file, annotationsManager, listOwner);
            return PopupStep.FINAL_CHOICE;
        }

        @Override
        @NotNull
        public String getTextFor(final PsiAnnotation value) {
            final String qualifiedName = value.getQualifiedName();
            LOG.assertTrue(qualifiedName != null);
            return qualifiedName;
        }
    }).showInBestPositionFor(editor);
}

From source file:com.intellij.codeInsight.intention.impl.ExpandStaticImportAction.java

License:Apache License

public void invoke(final Project project, final PsiFile file, final Editor editor, PsiElement element) {
    if (!FileModificationService.getInstance().preparePsiElementForWrite(element))
        return;/*  w w  w .ja va2  s.c o  m*/

    final PsiJavaCodeReferenceElement refExpr = (PsiJavaCodeReferenceElement) element.getParent();
    final PsiImportStaticStatement staticImport = (PsiImportStaticStatement) refExpr.advancedResolve(true)
            .getCurrentFileResolveScope();
    final List<PsiJavaCodeReferenceElement> expressionToExpand = collectReferencesThrough(file, refExpr,
            staticImport);

    if (expressionToExpand.isEmpty()) {
        expand(refExpr, staticImport);
        staticImport.delete();
    } else {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            replaceAllAndDeleteImport(expressionToExpand, refExpr, staticImport);
        } else {
            final BaseListPopupStep<String> step = new BaseListPopupStep<String>("Multiple Similar Calls Found",
                    new String[] { 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(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);
        }
    }
}

From source file:com.intellij.codeInsight.navigation.NavigationUtil.java

License:Apache License

private static JBPopup getPsiElementPopup(final Object[] elements,
        final Map<PsiElement, GotoRelatedItem> itemsMap, final String title,
        final Processor<Object> processor) {

    final Ref<Boolean> hasMnemonic = Ref.create(false);
    final DefaultPsiElementCellRenderer renderer = new DefaultPsiElementCellRenderer() {
        {//from   w w w.j a  v a  2 s.c om
            setFocusBorderEnabled(false);
        }

        @Override
        public String getElementText(PsiElement element) {
            String customName = itemsMap.get(element).getCustomName();
            return (customName != null ? customName : super.getElementText(element));
        }

        @Override
        protected Icon getIcon(PsiElement element) {
            Icon customIcon = itemsMap.get(element).getCustomIcon();
            return customIcon != null ? customIcon : super.getIcon(element);
        }

        @Override
        public String getContainerText(PsiElement element, String name) {
            String customContainerName = itemsMap.get(element).getCustomContainerName();

            if (customContainerName != null) {
                return customContainerName;
            }
            PsiFile file = element.getContainingFile();
            return file != null && !getElementText(element).equals(file.getName()) ? "(" + file.getName() + ")"
                    : null;
        }

        @Override
        protected DefaultListCellRenderer getRightCellRenderer(Object value) {
            return null;
        }

        @Override
        protected boolean customizeNonPsiElementLeftRenderer(ColoredListCellRenderer renderer, JList list,
                Object value, int index, boolean selected, boolean hasFocus) {
            final GotoRelatedItem item = (GotoRelatedItem) value;
            Color color = list.getForeground();
            final SimpleTextAttributes nameAttributes = new SimpleTextAttributes(Font.PLAIN, color);
            final String name = item.getCustomName();
            if (name == null)
                return false;
            renderer.append(name, nameAttributes);
            renderer.setIcon(item.getCustomIcon());
            return true;
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            final JPanel component = (JPanel) super.getListCellRendererComponent(list, value, index, isSelected,
                    cellHasFocus);
            if (!hasMnemonic.get())
                return component;

            final JPanel panelWithMnemonic = new JPanel(new BorderLayout());
            final int mnemonic = getMnemonic(value, itemsMap);
            final JLabel label = new JLabel("");
            if (mnemonic != -1) {
                label.setText(mnemonic + ".");
                label.setDisplayedMnemonicIndex(0);
            }
            label.setPreferredSize(new JLabel("8.").getPreferredSize());

            final JComponent leftRenderer = (JComponent) component.getComponents()[0];
            component.remove(leftRenderer);
            panelWithMnemonic.setBorder(BorderFactory.createEmptyBorder(0, 7, 0, 0));
            panelWithMnemonic.setBackground(leftRenderer.getBackground());
            label.setBackground(leftRenderer.getBackground());
            panelWithMnemonic.add(label, BorderLayout.WEST);
            panelWithMnemonic.add(leftRenderer, BorderLayout.CENTER);
            component.add(panelWithMnemonic);
            return component;
        }
    };
    final ListPopupImpl popup = new ListPopupImpl(
            new BaseListPopupStep<Object>(title, Arrays.asList(elements)) {
                @Override
                public boolean isSpeedSearchEnabled() {
                    return true;
                }

                @Override
                public String getIndexedString(Object value) {
                    if (value instanceof GotoRelatedItem) {
                        //noinspection ConstantConditions
                        return ((GotoRelatedItem) value).getCustomName();
                    }
                    final PsiElement element = (PsiElement) value;
                    return renderer.getElementText(element) + " " + renderer.getContainerText(element, null);
                }

                @Override
                public PopupStep onChosen(Object selectedValue, boolean finalChoice) {
                    processor.process(selectedValue);
                    return super.onChosen(selectedValue, finalChoice);
                }
            }) {
    };
    popup.getList().setCellRenderer(new PopupListElementRenderer(popup) {
        Map<Object, String> separators = new HashMap<Object, String>();
        {
            final ListModel model = popup.getList().getModel();
            String current = null;
            boolean hasTitle = false;
            for (int i = 0; i < model.getSize(); i++) {
                final Object element = model.getElementAt(i);
                final GotoRelatedItem item = itemsMap.get(element);
                if (item != null && !StringUtil.equals(current, item.getGroup())) {
                    current = item.getGroup();
                    separators.put(element, current);
                    if (!hasTitle && !StringUtil.isEmpty(current)) {
                        hasTitle = true;
                    }
                }
            }

            if (!hasTitle) {
                separators.remove(model.getElementAt(0));
            }
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            final Component component = renderer.getListCellRendererComponent(list, value, index, isSelected,
                    cellHasFocus);
            final String separator = separators.get(value);

            if (separator != null) {
                JPanel panel = new JPanel(new BorderLayout());
                panel.add(component, BorderLayout.CENTER);
                final SeparatorWithText sep = new SeparatorWithText() {
                    @Override
                    protected void paintComponent(Graphics g) {
                        g.setColor(new JBColor(Color.WHITE, UIUtil.getSeparatorColor()));
                        g.fillRect(0, 0, getWidth(), getHeight());
                        super.paintComponent(g);
                    }
                };
                sep.setCaption(separator);
                panel.add(sep, BorderLayout.NORTH);
                return panel;
            }
            return component;
        }
    });

    popup.setMinimumSize(new Dimension(200, -1));

    for (Object item : elements) {
        final int mnemonic = getMnemonic(item, itemsMap);
        if (mnemonic != -1) {
            final Action action = createNumberAction(mnemonic, popup, itemsMap, processor);
            popup.registerAction(mnemonic + "Action", KeyStroke.getKeyStroke(String.valueOf(mnemonic)), action);
            popup.registerAction(mnemonic + "Action",
                    KeyStroke.getKeyStroke("NUMPAD" + String.valueOf(mnemonic)), action);
            hasMnemonic.set(true);
        }
    }
    return popup;
}