Search in sources :

Example 1 with PopupChooserBuilder

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

the class CreateParameterForFieldIntention method performForConstructor.

private static void performForConstructor(PsiElement element, final Project project, Editor editor, List<GrField> candidates) {
    final GrMethod constructor = PsiTreeUtil.getParentOfType(element, GrMethod.class);
    if (candidates.isEmpty())
        return;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        for (GrField candidate : candidates) {
            addParameter(candidate, constructor, project);
        }
        return;
    }
    final JList list = new JBList(candidates.toArray(new GrField[candidates.size()]));
    list.setCellRenderer(new DefaultPsiElementCellRenderer());
    new PopupChooserBuilder(list).setTitle(GroovyIntentionsBundle.message("create.parameter.for.field.intention.name")).setMovable(true).setItemChoosenCallback(() -> {
        final Object[] selectedValues = list.getSelectedValues();
        CommandProcessor.getInstance().executeCommand(project, () -> {
            for (Object selectedValue : selectedValues) {
                LOG.assertTrue(((GrField) selectedValue).isValid());
                addParameter(((GrField) selectedValue), constructor, project);
            }
        }, GroovyIntentionsBundle.message("create.parameter.for.field.intention.name"), null);
    }).createPopup().showInBestPositionFor(editor);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) DefaultPsiElementCellRenderer(com.intellij.ide.util.DefaultPsiElementCellRenderer) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) JBList(com.intellij.ui.components.JBList) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder)

Example 2 with PopupChooserBuilder

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

the class CreateParameterForFieldIntention method performForField.

private static void performForField(PsiElement element, final Project project, Editor editor, List<GrMethod> constructors) {
    final GrField field = PsiTreeUtil.getParentOfType(element, GrField.class);
    if (constructors.isEmpty())
        return;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        for (GrMethod constructor : constructors) {
            addParameter(field, constructor, project);
        }
        return;
    }
    final JList list = new JBList(constructors.toArray(new GrMethod[constructors.size()]));
    list.setCellRenderer(new MethodCellRenderer(true));
    new PopupChooserBuilder(list).setTitle(GroovyIntentionsBundle.message("create.parameter.for.field.intention.name")).setMovable(true).setItemChoosenCallback(() -> {
        final Object[] selectedValues = list.getSelectedValues();
        Arrays.sort(selectedValues, (o1, o2) -> ((GrMethod) o2).getParameterList().getParametersCount() - ((GrMethod) o1).getParameterList().getParametersCount());
        CommandProcessor.getInstance().executeCommand(project, () -> {
            for (Object selectedValue : selectedValues) {
                LOG.assertTrue(((GrMethod) selectedValue).isValid());
                addParameter(field, ((GrMethod) selectedValue), project);
            }
        }, GroovyIntentionsBundle.message("create.parameter.for.field.intention.name"), null);
    }).createPopup().showInBestPositionFor(editor);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) JBList(com.intellij.ui.components.JBList) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) MethodCellRenderer(com.intellij.ide.util.MethodCellRenderer)

Example 3 with PopupChooserBuilder

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

the class MavenSelectProjectPopup method attachToButton.

public static void attachToButton(@NotNull final MavenProjectsManager projectsManager, @NotNull final JButton button, @NotNull final Consumer<MavenProject> callback) {
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            List<MavenProject> projectList = projectsManager.getProjects();
            if (projectList.isEmpty()) {
                JBPopupFactory.getInstance().createMessage("Maven projects not found").showUnderneathOf(button);
                return;
            }
            DefaultMutableTreeNode root = buildTree(projectList);
            final Map<MavenProject, String> projectsNameMap = MavenProjectNamer.generateNameMap(projectList);
            final Tree projectTree = new Tree(root);
            projectTree.setRootVisible(false);
            projectTree.setCellRenderer(new NodeRenderer() {

                @Override
                public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                    if (value instanceof DefaultMutableTreeNode) {
                        MavenProject mavenProject = (MavenProject) ((DefaultMutableTreeNode) value).getUserObject();
                        value = projectsNameMap.get(mavenProject);
                        setIcon(MavenIcons.MavenProject);
                    }
                    super.customizeCellRenderer(tree, value, selected, expanded, leaf, row, hasFocus);
                }
            });
            new TreeSpeedSearch(projectTree, new Convertor<TreePath, String>() {

                @Override
                public String convert(TreePath o) {
                    Object lastPathComponent = o.getLastPathComponent();
                    if (!(lastPathComponent instanceof DefaultMutableTreeNode))
                        return null;
                    Object userObject = ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
                    //noinspection SuspiciousMethodCalls
                    return projectsNameMap.get(userObject);
                }
            });
            final Ref<JBPopup> popupRef = new Ref<>();
            final Runnable clickCallBack = () -> {
                TreePath path = projectTree.getSelectionPath();
                if (path == null)
                    return;
                Object lastPathComponent = path.getLastPathComponent();
                if (!(lastPathComponent instanceof DefaultMutableTreeNode))
                    return;
                Object object = ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
                // may be it's the root
                if (object == null)
                    return;
                callback.consume((MavenProject) object);
                popupRef.get().closeOk(null);
            };
            projectTree.addKeyListener(new KeyAdapter() {

                @Override
                public void keyPressed(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        clickCallBack.run();
                        e.consume();
                    }
                }
            });
            JBPopup popup = new PopupChooserBuilder(projectTree).setTitle("Select maven project").setResizable(true).setItemChoosenCallback(clickCallBack).setAutoselectOnMouseMove(true).setCloseOnEnter(false).createPopup();
            popupRef.set(popup);
            popup.showUnderneathOf(button);
        }

        private DefaultMutableTreeNode buildTree(List<MavenProject> projectList) {
            MavenProject[] projects = projectList.toArray(new MavenProject[projectList.size()]);
            Arrays.sort(projects, new MavenProjectNamer.MavenProjectComparator());
            Map<MavenProject, DefaultMutableTreeNode> projectsToNode = new HashMap<>();
            for (MavenProject mavenProject : projects) {
                projectsToNode.put(mavenProject, new DefaultMutableTreeNode(mavenProject));
            }
            DefaultMutableTreeNode root = new DefaultMutableTreeNode();
            for (MavenProject mavenProject : projects) {
                DefaultMutableTreeNode parent;
                MavenProject aggregator = projectsManager.findAggregator(mavenProject);
                if (aggregator != null) {
                    parent = projectsToNode.get(aggregator);
                } else {
                    parent = root;
                }
                parent.add(projectsToNode.get(mavenProject));
            }
            return root;
        }
    });
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ActionEvent(java.awt.event.ActionEvent) TreeSpeedSearch(com.intellij.ui.TreeSpeedSearch) KeyAdapter(java.awt.event.KeyAdapter) Convertor(com.intellij.util.containers.Convertor) NodeRenderer(com.intellij.ide.util.treeView.NodeRenderer) KeyEvent(java.awt.event.KeyEvent) Ref(com.intellij.openapi.util.Ref) ActionListener(java.awt.event.ActionListener) MavenProject(org.jetbrains.idea.maven.project.MavenProject) TreePath(javax.swing.tree.TreePath) Tree(com.intellij.ui.treeStructure.Tree) List(java.util.List) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) JBPopup(com.intellij.openapi.ui.popup.JBPopup) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with PopupChooserBuilder

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

the class TagsPanel method showTags.

private void showTags() {
    DefaultListModel model = new DefaultListModel();
    myList.setModel(model);
    for (final String myTag : myTags) {
        model.addElement(myTag);
    }
    new PopupChooserBuilder(myList).setTitle(myPopupTitle).createPopup().showUnderneathOf(myMoreLabel);
}
Also used : PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder)

Example 5 with PopupChooserBuilder

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

the class CreateInnerClassFromUsageFix method chooseTargetClass.

private void chooseTargetClass(PsiClass[] classes, final Editor editor, final String superClassName) {
    final Project project = classes[0].getProject();
    final JList list = new JBList(classes);
    PsiElementListCellRenderer renderer = new PsiClassListCellRenderer();
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setCellRenderer(renderer);
    final PopupChooserBuilder builder = new PopupChooserBuilder(list);
    renderer.installSpeedSearch(builder);
    Runnable runnable = () -> {
        int index = list.getSelectedIndex();
        if (index < 0)
            return;
        doInvoke((PsiClass) list.getSelectedValue(), superClassName);
    };
    builder.setTitle(QuickFixBundle.message("target.class.chooser.title")).setItemChoosenCallback(runnable).createPopup().showInBestPositionFor(editor);
}
Also used : Project(com.intellij.openapi.project.Project) JBList(com.intellij.ui.components.JBList) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer)

Aggregations

PopupChooserBuilder (com.intellij.openapi.ui.popup.PopupChooserBuilder)31 JBList (com.intellij.ui.components.JBList)24 JBPopup (com.intellij.openapi.ui.popup.JBPopup)15 PsiElementListCellRenderer (com.intellij.ide.util.PsiElementListCellRenderer)5 Project (com.intellij.openapi.project.Project)4 Ref (com.intellij.openapi.util.Ref)4 RelativePoint (com.intellij.ui.awt.RelativePoint)4 NotNull (org.jetbrains.annotations.NotNull)4 MethodCellRenderer (com.intellij.ide.util.MethodCellRenderer)3 PsiClassListCellRenderer (com.intellij.ide.util.PsiClassListCellRenderer)3 PsiElement (com.intellij.psi.PsiElement)3 JBLabel (com.intellij.ui.components.JBLabel)3 Editor (com.intellij.openapi.editor.Editor)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 ColoredListCellRenderer (com.intellij.ui.ColoredListCellRenderer)2 TreeTableView (com.intellij.ui.dualView.TreeTableView)2 UsageView (com.intellij.usages.UsageView)2 Alarm (com.intellij.util.Alarm)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 ColumnInfo (com.intellij.util.ui.ColumnInfo)2