Example usage for com.intellij.openapi.ui.popup PopupChooserBuilder setFilteringEnabled

List of usage examples for com.intellij.openapi.ui.popup PopupChooserBuilder setFilteringEnabled

Introduction

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

Prototype

public PopupChooserBuilder<T> setFilteringEnabled(Function<Object, String> namer) 

Source Link

Usage

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

License:Apache License

private void show(@NotNull final Project project, @NotNull Editor editor, @NotNull PsiFile file,
        @NotNull final GotoData gotoData) {
    final PsiElement[] targets = gotoData.targets;
    final List<AdditionalAction> additionalActions = gotoData.additionalActions;

    if (targets.length == 0 && additionalActions.isEmpty()) {
        HintManager.getInstance().showErrorHint(editor, getNotFoundMessage(project, editor, file));
        return;/*from  w  ww  . j a  va 2s.com*/
    }

    if (targets.length == 1 && additionalActions.isEmpty()) {
        Navigatable descriptor = targets[0] instanceof Navigatable ? (Navigatable) targets[0]
                : EditSourceUtil.getDescriptor(targets[0]);
        if (descriptor != null && descriptor.canNavigate()) {
            navigateToElement(descriptor);
        }
        return;
    }

    for (PsiElement eachTarget : targets) {
        gotoData.renderers.put(eachTarget, createRenderer(gotoData, eachTarget));
    }

    final String name = ((PsiNamedElement) gotoData.source).getName();
    final String title = getChooserTitle(gotoData.source, name, targets.length);

    if (shouldSortTargets()) {
        Arrays.sort(targets, createComparator(gotoData.renderers, gotoData));
    }

    List<Object> allElements = new ArrayList<Object>(targets.length + additionalActions.size());
    Collections.addAll(allElements, targets);
    allElements.addAll(additionalActions);

    final JBListWithHintProvider list = new JBListWithHintProvider(
            new CollectionListModel<Object>(allElements)) {
        @Override
        protected PsiElement getPsiElementForHint(final Object selectedValue) {
            return selectedValue instanceof PsiElement ? (PsiElement) selectedValue : null;
        }
    };

    list.setFont(ChooseByNameBase.getEditorFont());

    list.setCellRenderer(new DefaultListCellRenderer() {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            if (value == null)
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof AdditionalAction) {
                return myActionElementRenderer.getListCellRendererComponent(list, value, index, isSelected,
                        cellHasFocus);
            }
            PsiElementListCellRenderer renderer = getRenderer(value, gotoData.renderers, gotoData);
            return renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        }
    });

    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            int[] ids = list.getSelectedIndices();
            if (ids == null || ids.length == 0)
                return;
            Object[] selectedElements = list.getSelectedValues();
            for (Object element : selectedElements) {
                if (element instanceof AdditionalAction) {
                    ((AdditionalAction) element).execute();
                } else {
                    Navigatable nav = element instanceof Navigatable ? (Navigatable) element
                            : EditSourceUtil.getDescriptor((PsiElement) element);
                    try {
                        if (nav != null && nav.canNavigate()) {
                            navigateToElement(nav);
                        }
                    } catch (IndexNotReadyException e) {
                        DumbService.getInstance(project)
                                .showDumbModeNotification("Navigation is not available while indexing");
                    }
                }
            }
        }
    };

    final PopupChooserBuilder builder = new PopupChooserBuilder(list);
    builder.setFilteringEnabled(new Function<Object, String>() {
        @Override
        public String fun(Object o) {
            if (o instanceof AdditionalAction) {
                return ((AdditionalAction) o).getText();
            }
            return getRenderer(o, gotoData.renderers, gotoData).getElementText((PsiElement) o);
        }
    });

    final Ref<UsageView> usageView = new Ref<UsageView>();
    final JBPopup popup = builder.setTitle(title).setItemChoosenCallback(runnable).setMovable(true)
            .setCancelCallback(new Computable<Boolean>() {
                @Override
                public Boolean compute() {
                    HintUpdateSupply.hideHint(list);
                    return true;
                }
            }).setCouldPin(new Processor<JBPopup>() {
                @Override
                public boolean process(JBPopup popup) {
                    usageView.set(FindUtil.showInUsageView(gotoData.source, gotoData.targets,
                            getFindUsagesTitle(gotoData.source, name, gotoData.targets.length), project));
                    popup.cancel();
                    return false;
                }
            }).setAdText(getAdText(gotoData.source, targets.length)).createPopup();

    builder.getScrollPane().setBorder(null);
    builder.getScrollPane().setViewportBorder(null);

    if (gotoData.listUpdaterTask != null) {
        gotoData.listUpdaterTask.init((AbstractPopup) popup, list, usageView);
        ProgressManager.getInstance().run(gotoData.listUpdaterTask);
    }
    popup.showInBestPositionFor(editor);
}

From source file:com.intellij.ide.util.PsiElementListCellRenderer.java

License:Apache License

public void installSpeedSearch(PopupChooserBuilder builder, final boolean includeContainerText) {
    builder.setFilteringEnabled(new Function<Object, String>() {
        @Override/*from   w  w  w.  j av a 2  s  .co  m*/
        public String fun(Object o) {
            if (o instanceof PsiElement) {
                final String elementText = getElementText((T) o);
                if (includeContainerText) {
                    return elementText + " " + getContainerText((T) o, elementText);
                }
                return elementText;
            } else {
                return o.toString();
            }
        }
    });
}

From source file:org.jboss.forge.plugin.idea.ui.CommandListPopupBuilder.java

License:Open Source License

private JBPopup buildPopup(final JBList list, final Map<Object, String> filterIndex) {
    final PopupChooserBuilder listPopupBuilder = JBPopupFactory.getInstance().createListPopupBuilder(list);
    listPopupBuilder.setTitle("Run a Forge command");
    listPopupBuilder.setResizable(true);
    listPopupBuilder.addListener(new JBPopupAdapter() {
        @Override/*from w w  w .j a v  a2  s.com*/
        public void onClosed(LightweightWindowEvent event) {
            CommandListPopupBuilder.this.active = false;
        }
    });
    listPopupBuilder.setItemChoosenCallback(new Runnable() {
        @Override
        public void run() {
            Object selectedObject = list.getSelectedValue();
            if (selectedObject instanceof UICommand) {
                UICommand selectedCommand = (UICommand) selectedObject;

                // Make sure that this cached command is still enabled
                if (selectedCommand.isEnabled(uiContext)) {
                    openWizard(selectedCommand);
                }
            }
        }
    });
    listPopupBuilder.setFilteringEnabled(new Function<Object, String>() {
        @Override
        public String fun(Object object) {
            return filterIndex.get(object);
        }
    });

    return listPopupBuilder.createPopup();
}