Search in sources :

Example 26 with PopupChooserBuilder

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

the class HighlightImportedElementsHandler method selectTargets.

@Override
protected void selectTargets(final List<PsiMember> targets, final Consumer<List<PsiMember>> selectionConsumer) {
    if (targets.isEmpty()) {
        selectionConsumer.consume(Collections.<PsiMember>emptyList());
        return;
    }
    if (targets.size() == 1) {
        selectionConsumer.consume(Collections.singletonList(targets.get(0)));
        return;
    }
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        selectionConsumer.consume(targets);
        return;
    }
    Collections.sort(targets, new PsiMemberComparator());
    final List<Object> model = new ArrayList<>();
    model.add(CodeInsightBundle.message("highlight.thrown.exceptions.chooser.all.entry"));
    model.addAll(targets);
    final JList list = new JBList(model);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    final ListCellRenderer renderer = new NavigationItemListCellRenderer();
    list.setCellRenderer(renderer);
    final PopupChooserBuilder builder = new PopupChooserBuilder(list);
    builder.setFilteringEnabled(o -> {
        if (o instanceof PsiMember) {
            final PsiMember member = (PsiMember) o;
            return member.getName();
        }
        return o.toString();
    });
    if (myImportStatic) {
        builder.setTitle(CodeInsightBundle.message("highlight.imported.members.chooser.title"));
    } else {
        builder.setTitle(CodeInsightBundle.message("highlight.imported.classes.chooser.title"));
    }
    builder.setItemChoosenCallback(() -> {
        final int index = list.getSelectedIndex();
        if (index == 0) {
            selectionConsumer.consume(targets);
        } else {
            selectionConsumer.consume(Collections.singletonList(targets.get(index - 1)));
        }
    });
    final JBPopup popup = builder.createPopup();
    popup.showInBestPositionFor(myEditor);
}
Also used : NavigationItemListCellRenderer(com.intellij.ide.util.NavigationItemListCellRenderer) NavigationItemListCellRenderer(com.intellij.ide.util.NavigationItemListCellRenderer) JBList(com.intellij.ui.components.JBList) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) JBPopup(com.intellij.openapi.ui.popup.JBPopup)

Example 27 with PopupChooserBuilder

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

the class CopyAbstractMethodImplementationHandler method invoke.

public void invoke() {
    ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> ApplicationManager.getApplication().runReadAction(() -> searchExistingImplementations()), CodeInsightBundle.message("searching.for.implementations"), false, myProject);
    if (mySourceMethods.isEmpty()) {
        Messages.showErrorDialog(myProject, CodeInsightBundle.message("copy.abstract.method.no.existing.implementations.found"), CodeInsightBundle.message("copy.abstract.method.title"));
        return;
    }
    if (mySourceMethods.size() == 1) {
        copyImplementation(mySourceMethods.get(0));
    } else {
        Collections.sort(mySourceMethods, (o1, o2) -> {
            PsiClass c1 = o1.getContainingClass();
            PsiClass c2 = o2.getContainingClass();
            return Comparing.compare(c1.getName(), c2.getName());
        });
        final PsiMethod[] methodArray = mySourceMethods.toArray(new PsiMethod[mySourceMethods.size()]);
        final JList list = new JBList(methodArray);
        list.setCellRenderer(new MethodCellRenderer(true));
        final Runnable runnable = () -> {
            int index = list.getSelectedIndex();
            if (index < 0)
                return;
            PsiMethod element = (PsiMethod) list.getSelectedValue();
            copyImplementation(element);
        };
        new PopupChooserBuilder(list).setTitle(CodeInsightBundle.message("copy.abstract.method.popup.title")).setItemChoosenCallback(runnable).createPopup().showInBestPositionFor(myEditor);
    }
}
Also used : JBList(com.intellij.ui.components.JBList) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) MethodCellRenderer(com.intellij.ide.util.MethodCellRenderer)

Example 28 with PopupChooserBuilder

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

the class ImportNSAction method execute.

@Override
public boolean execute() {
    final String[] strings = ArrayUtil.toStringArray(myNamespaces);
    final JList list = new JBList(strings);
    list.setCellRenderer(XmlNSRenderer.INSTANCE);
    list.setSelectedIndex(0);
    final int offset = myElement.getTextOffset();
    final RangeMarker marker = myEditor.getDocument().createRangeMarker(offset, offset);
    final Runnable runnable = () -> {
        final String namespace = (String) list.getSelectedValue();
        if (namespace != null) {
            final Project project = myFile.getProject();
            new WriteCommandAction.Simple(project, myFile) {

                @Override
                protected void run() throws Throwable {
                    final XmlNamespaceHelper extension = XmlNamespaceHelper.getHelper(myFile);
                    final String prefix = extension.getNamespacePrefix(myElement);
                    extension.insertNamespaceDeclaration(myFile, myEditor, Collections.singleton(namespace), prefix, new XmlNamespaceHelper.Runner<String, IncorrectOperationException>() {

                        @Override
                        public void run(final String s) throws IncorrectOperationException {
                            PsiDocumentManager.getInstance(myFile.getProject()).doPostponedOperationsAndUnblockDocument(myEditor.getDocument());
                            PsiElement element = myFile.findElementAt(marker.getStartOffset());
                            if (element != null) {
                                extension.qualifyWithPrefix(s, element, myEditor.getDocument());
                            }
                        }
                    });
                }
            }.execute();
        }
    };
    if (list.getModel().getSize() == 1) {
        runnable.run();
    } else {
        new PopupChooserBuilder(list).setTitle(myTitle).setItemChoosenCallback(runnable).createPopup().showInBestPositionFor(myEditor);
    }
    return true;
}
Also used : XmlNamespaceHelper(com.intellij.xml.XmlNamespaceHelper) RangeMarker(com.intellij.openapi.editor.RangeMarker) Project(com.intellij.openapi.project.Project) JBList(com.intellij.ui.components.JBList) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) PsiElement(com.intellij.psi.PsiElement)

Example 29 with PopupChooserBuilder

use of com.intellij.openapi.ui.popup.PopupChooserBuilder in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoImportPackageQuickFix method perform.

private void perform(@NotNull List<String> packagesToImport, @NotNull PsiFile file, @Nullable Editor editor) {
    LOG.assertTrue(editor != null || packagesToImport.size() == 1, "Cannot invoke fix with ambiguous imports on null editor");
    if (packagesToImport.size() > 1 && editor != null) {
        JBList list = new JBList(packagesToImport);
        list.installCellRenderer(o -> {
            JBLabel label = new JBLabel(o.toString(), GoIcons.PACKAGE, SwingConstants.LEFT);
            label.setBorder(IdeBorderFactory.createEmptyBorder(2, 4, 2, 4));
            return label;
        });
        PopupChooserBuilder builder = JBPopupFactory.getInstance().createListPopupBuilder(list).setRequestFocus(true).setTitle("Package to import").setItemChoosenCallback(() -> {
            int i = list.getSelectedIndex();
            if (i < 0)
                return;
            perform(file, packagesToImport.get(i));
        }).setFilteringEnabled(o -> o instanceof String ? (String) o : o.toString());
        JBPopup popup = builder.createPopup();
        builder.getScrollPane().setBorder(null);
        builder.getScrollPane().setViewportBorder(null);
        popup.showInBestPositionFor(editor);
    } else if (packagesToImport.size() == 1) {
        perform(file, getFirstItem(packagesToImport));
    } else {
        String packages = StringUtil.join(packagesToImport, ",");
        throw new IncorrectOperationException("Cannot invoke fix with ambiguous imports on editor ()" + editor + ". Packages: " + packages);
    }
}
Also used : JBLabel(com.intellij.ui.components.JBLabel) JBList(com.intellij.ui.components.JBList) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) JBPopup(com.intellij.openapi.ui.popup.JBPopup)

Example 30 with PopupChooserBuilder

use of com.intellij.openapi.ui.popup.PopupChooserBuilder in project ballerina by ballerina-lang.

the class BallerinaImportPackageQuickFix method performImport.

private void performImport(@NotNull List<String> packagesToImport, @NotNull PsiFile file, @Nullable Editor editor) {
    if (packagesToImport.size() > 1 && editor != null) {
        JBList<String> list = new JBList<>(packagesToImport);
        list.installCellRenderer(o -> {
            JBLabel label = new JBLabel(o.toString(), BallerinaIcons.PACKAGE, SwingConstants.LEFT);
            label.setBorder(IdeBorderFactory.createEmptyBorder(2, 4, 2, 4));
            return label;
        });
        PopupChooserBuilder popupChooserBuilder = JBPopupFactory.getInstance().createListPopupBuilder(list).setRequestFocus(true).setTitle("Package to import").setItemChoosenCallback(() -> {
            int i = list.getSelectedIndex();
            if (i < 0) {
                return;
            }
            performImport(file, packagesToImport.get(i));
        }).setFilteringEnabled(item -> item instanceof String ? (String) item : item.toString());
        JBPopup popup = popupChooserBuilder.createPopup();
        popupChooserBuilder.getScrollPane().setBorder(null);
        popupChooserBuilder.getScrollPane().setViewportBorder(null);
        popup.showInBestPositionFor(editor);
    }
}
Also used : JBLabel(com.intellij.ui.components.JBLabel) JBList(com.intellij.ui.components.JBList) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) JBPopup(com.intellij.openapi.ui.popup.JBPopup)

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