use of com.intellij.ide.util.PsiElementListCellRenderer 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;
}
use of com.intellij.ide.util.PsiElementListCellRenderer in project intellij-community by JetBrains.
the class GotoTargetHandler method show.
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;
}
boolean finished = gotoData.listUpdaterTask == null || gotoData.listUpdaterTask.isFinished();
if (targets.length == 1 && additionalActions.isEmpty() && finished) {
navigateToElement(targets[0]);
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, finished);
if (shouldSortTargets()) {
Arrays.sort(targets, createComparator(gotoData.renderers, gotoData));
}
List<Object> allElements = new ArrayList<>(targets.length + additionalActions.size());
Collections.addAll(allElements, targets);
allElements.addAll(additionalActions);
final JBList list = new JBList(new CollectionListModel<>(allElements));
HintUpdateSupply.installSimpleHintUpdateSupply(list);
list.setFont(EditorUtil.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 = () -> {
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(o -> {
if (o instanceof AdditionalAction) {
return ((AdditionalAction) o).getText();
}
return getRenderer(o, gotoData.renderers, gotoData).getElementText((PsiElement) o);
});
final Ref<UsageView> usageView = new Ref<>();
final JBPopup popup = builder.setTitle(title).setItemChoosenCallback(runnable).setMovable(true).setCancelCallback(() -> {
HintUpdateSupply.hideHint(list);
final ListBackgroundUpdaterTask task = gotoData.listUpdaterTask;
if (task != null) {
task.cancelTask();
}
return true;
}).setCouldPin(popup1 -> {
usageView.set(FindUtil.showInUsageView(gotoData.source, gotoData.targets, getFindUsagesTitle(gotoData.source, name, gotoData.targets.length), gotoData.source.getProject()));
popup1.cancel();
return false;
}).setAdText(getAdText(gotoData.source, targets.length)).createPopup();
builder.getScrollPane().setBorder(null);
builder.getScrollPane().setViewportBorder(null);
if (gotoData.listUpdaterTask != null) {
Alarm alarm = new Alarm(popup);
alarm.addRequest(() -> popup.showInBestPositionFor(editor), 300);
gotoData.listUpdaterTask.init((AbstractPopup) popup, list, usageView);
ProgressManager.getInstance().run(gotoData.listUpdaterTask);
} else {
popup.showInBestPositionFor(editor);
}
}
use of com.intellij.ide.util.PsiElementListCellRenderer in project intellij-community by JetBrains.
the class StaticImportMethodQuestionAction method chooseAndImport.
private void chooseAndImport(final Editor editor, final Project project) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
doImport(myCandidates.get(0));
return;
}
final BaseListPopupStep<T> step = new BaseListPopupStep<T>(getPopupTitle(), myCandidates) {
@Override
public boolean isAutoSelectionEnabled() {
return false;
}
@Override
public boolean isSpeedSearchEnabled() {
return true;
}
@Override
public PopupStep onChosen(T selectedValue, boolean finalChoice) {
if (selectedValue == null) {
return FINAL_CHOICE;
}
if (finalChoice) {
return doFinalStep(() -> {
PsiDocumentManager.getInstance(project).commitAllDocuments();
LOG.assertTrue(selectedValue.isValid());
doImport(selectedValue);
});
}
return AddImportAction.getExcludesStep(PsiUtil.getMemberQualifiedName(selectedValue), project);
}
@Override
public boolean hasSubstep(T selectedValue) {
return true;
}
@NotNull
@Override
public String getTextFor(T value) {
return getElementPresentableName(value);
}
@Override
public Icon getIconFor(T aValue) {
return aValue.getIcon(0);
}
};
final ListPopupImpl popup = new ListPopupImpl(step) {
final PopupListElementRenderer rightArrow = new PopupListElementRenderer(this);
@Override
protected ListCellRenderer getListElementRenderer() {
return new PsiElementListCellRenderer<T>() {
public String getElementText(T element) {
return getElementPresentableName(element);
}
public String getContainerText(final T element, final String name) {
return PsiClassListCellRenderer.getContainerTextStatic(element);
}
public int getIconFlags() {
return 0;
}
@Nullable
@Override
protected TextAttributes getNavigationItemAttributes(Object value) {
TextAttributes attrs = super.getNavigationItemAttributes(value);
if (value instanceof PsiDocCommentOwner && !((PsiDocCommentOwner) value).isDeprecated()) {
PsiClass psiClass = ((T) value).getContainingClass();
if (psiClass != null && psiClass.isDeprecated()) {
return TextAttributes.merge(attrs, super.getNavigationItemAttributes(psiClass));
}
}
return attrs;
}
@Override
protected DefaultListCellRenderer getRightCellRenderer(final Object value) {
final DefaultListCellRenderer moduleRenderer = super.getRightCellRenderer(value);
return new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JPanel panel = new JPanel(new BorderLayout());
if (moduleRenderer != null) {
Component moduleComponent = moduleRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (!isSelected) {
moduleComponent.setBackground(getBackgroundColor(value));
}
panel.add(moduleComponent, BorderLayout.CENTER);
}
rightArrow.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
Component rightArrowComponent = rightArrow.getNextStepLabel();
panel.add(rightArrowComponent, BorderLayout.EAST);
return panel;
}
};
}
};
}
};
popup.showInBestPositionFor(editor);
}
use of com.intellij.ide.util.PsiElementListCellRenderer 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);
}
use of com.intellij.ide.util.PsiElementListCellRenderer in project intellij-community by JetBrains.
the class CreateFromUsageBaseFix method chooseTargetClass.
private void chooseTargetClass(List<PsiClass> classes, final Editor editor) {
final PsiClass firstClass = classes.get(0);
final Project project = firstClass.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);
final PsiClass preselection = AnonymousTargetClassPreselectionUtil.getPreselection(classes, firstClass);
if (preselection != null) {
list.setSelectedValue(preselection, true);
}
Runnable runnable = () -> {
int index = list.getSelectedIndex();
if (index < 0)
return;
final PsiClass aClass = (PsiClass) list.getSelectedValue();
AnonymousTargetClassPreselectionUtil.rememberSelection(aClass, firstClass);
CommandProcessor.getInstance().executeCommand(project, () -> doInvoke(project, aClass), getText(), null);
};
builder.setTitle(QuickFixBundle.message("target.class.chooser.title")).setItemChoosenCallback(runnable).createPopup().showInBestPositionFor(editor);
}
Aggregations