Search in sources :

Example 1 with ListBackgroundUpdaterTask

use of com.intellij.codeInsight.navigation.ListBackgroundUpdaterTask in project intellij-community by JetBrains.

the class PsiElementListNavigator method navigateOrCreatePopup.

/**
   * listUpdaterTask should be started after alarm is initialized so one-item popup won't blink
   */
@Nullable
public static JBPopup navigateOrCreatePopup(@NotNull final NavigatablePsiElement[] targets, final String title, final String findUsagesTitle, final ListCellRenderer listRenderer, @Nullable final ListBackgroundUpdaterTask listUpdaterTask, @NotNull final Consumer<Object[]> consumer) {
    if (targets.length == 0)
        return null;
    if (targets.length == 1 && (listUpdaterTask == null || listUpdaterTask.isFinished())) {
        consumer.consume(targets);
        return null;
    }
    final CollectionListModel<NavigatablePsiElement> model = new CollectionListModel<>(targets);
    final JBList list = new JBList(model);
    HintUpdateSupply.installSimpleHintUpdateSupply(list);
    list.setTransferHandler(new TransferHandler() {

        @Nullable
        @Override
        protected Transferable createTransferable(JComponent c) {
            final Object[] selectedValues = list.getSelectedValues();
            final PsiElement[] copy = new PsiElement[selectedValues.length];
            for (int i = 0; i < selectedValues.length; i++) {
                copy[i] = (PsiElement) selectedValues[i];
            }
            return new PsiCopyPasteManager.MyTransferable(copy);
        }

        @Override
        public int getSourceActions(JComponent c) {
            return COPY;
        }
    });
    list.setCellRenderer(listRenderer);
    list.setFont(EditorUtil.getEditorFont());
    final PopupChooserBuilder builder = new PopupChooserBuilder(list);
    if (listRenderer instanceof PsiElementListCellRenderer) {
        ((PsiElementListCellRenderer) listRenderer).installSpeedSearch(builder);
    }
    PopupChooserBuilder popupChooserBuilder = builder.setTitle(title).setMovable(true).setResizable(true).setItemChoosenCallback(() -> {
        int[] ids = list.getSelectedIndices();
        if (ids == null || ids.length == 0)
            return;
        Object[] selectedElements = list.getSelectedValues();
        consumer.consume(selectedElements);
    }).setCancelCallback(() -> {
        HintUpdateSupply.hideHint(list);
        if (listUpdaterTask != null) {
            listUpdaterTask.cancelTask();
        }
        return true;
    });
    final Ref<UsageView> usageView = new Ref<>();
    if (findUsagesTitle != null) {
        popupChooserBuilder = popupChooserBuilder.setCouldPin(popup -> {
            final List<NavigatablePsiElement> items = model.getItems();
            usageView.set(FindUtil.showInUsageView(null, items.toArray(new PsiElement[items.size()]), findUsagesTitle, targets[0].getProject()));
            popup.cancel();
            return false;
        });
    }
    final JBPopup popup = popupChooserBuilder.createPopup();
    builder.getScrollPane().setBorder(null);
    builder.getScrollPane().setViewportBorder(null);
    if (listUpdaterTask != null) {
        listUpdaterTask.init((AbstractPopup) popup, list, usageView);
    }
    return popup;
}
Also used : EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) Transferable(java.awt.datatransfer.Transferable) PsiCopyPasteManager(com.intellij.ide.PsiCopyPasteManager) PsiElement(com.intellij.psi.PsiElement) Logger(com.intellij.openapi.diagnostic.Logger) ProgressManager(com.intellij.openapi.progress.ProgressManager) JBList(com.intellij.ui.components.JBList) AbstractPopup(com.intellij.ui.popup.AbstractPopup) CollectionListModel(com.intellij.ui.CollectionListModel) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) Editor(com.intellij.openapi.editor.Editor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) MouseEvent(java.awt.event.MouseEvent) ListBackgroundUpdaterTask(com.intellij.codeInsight.navigation.ListBackgroundUpdaterTask) HintUpdateSupply(com.intellij.ui.popup.HintUpdateSupply) UsageView(com.intellij.usages.UsageView) Nullable(org.jetbrains.annotations.Nullable) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) List(java.util.List) FindUtil(com.intellij.find.FindUtil) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) RelativePoint(com.intellij.ui.awt.RelativePoint) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer) Consumer(com.intellij.util.Consumer) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) Transferable(java.awt.datatransfer.Transferable) PsiCopyPasteManager(com.intellij.ide.PsiCopyPasteManager) UsageView(com.intellij.usages.UsageView) Ref(com.intellij.openapi.util.Ref) JBList(com.intellij.ui.components.JBList) JBList(com.intellij.ui.components.JBList) List(java.util.List) CollectionListModel(com.intellij.ui.CollectionListModel) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) JBPopup(com.intellij.openapi.ui.popup.JBPopup) Nullable(org.jetbrains.annotations.Nullable) PsiElement(com.intellij.psi.PsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ListBackgroundUpdaterTask (com.intellij.codeInsight.navigation.ListBackgroundUpdaterTask)1 FindUtil (com.intellij.find.FindUtil)1 PsiCopyPasteManager (com.intellij.ide.PsiCopyPasteManager)1 PsiElementListCellRenderer (com.intellij.ide.util.PsiElementListCellRenderer)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Editor (com.intellij.openapi.editor.Editor)1 EditorUtil (com.intellij.openapi.editor.ex.util.EditorUtil)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 JBPopup (com.intellij.openapi.ui.popup.JBPopup)1 PopupChooserBuilder (com.intellij.openapi.ui.popup.PopupChooserBuilder)1 Ref (com.intellij.openapi.util.Ref)1 NavigatablePsiElement (com.intellij.psi.NavigatablePsiElement)1 PsiElement (com.intellij.psi.PsiElement)1 CollectionListModel (com.intellij.ui.CollectionListModel)1 RelativePoint (com.intellij.ui.awt.RelativePoint)1 JBList (com.intellij.ui.components.JBList)1 AbstractPopup (com.intellij.ui.popup.AbstractPopup)1 HintUpdateSupply (com.intellij.ui.popup.HintUpdateSupply)1 UsageView (com.intellij.usages.UsageView)1 Alarm (com.intellij.util.Alarm)1