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;
}
Aggregations