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