use of com.intellij.openapi.ui.popup.PopupChooserBuilder in project intellij-community by JetBrains.
the class CreateParameterForFieldIntention method performForConstructor.
private static void performForConstructor(PsiElement element, final Project project, Editor editor, List<GrField> candidates) {
final GrMethod constructor = PsiTreeUtil.getParentOfType(element, GrMethod.class);
if (candidates.isEmpty())
return;
if (ApplicationManager.getApplication().isUnitTestMode()) {
for (GrField candidate : candidates) {
addParameter(candidate, constructor, project);
}
return;
}
final JList list = new JBList(candidates.toArray(new GrField[candidates.size()]));
list.setCellRenderer(new DefaultPsiElementCellRenderer());
new PopupChooserBuilder(list).setTitle(GroovyIntentionsBundle.message("create.parameter.for.field.intention.name")).setMovable(true).setItemChoosenCallback(() -> {
final Object[] selectedValues = list.getSelectedValues();
CommandProcessor.getInstance().executeCommand(project, () -> {
for (Object selectedValue : selectedValues) {
LOG.assertTrue(((GrField) selectedValue).isValid());
addParameter(((GrField) selectedValue), constructor, project);
}
}, GroovyIntentionsBundle.message("create.parameter.for.field.intention.name"), null);
}).createPopup().showInBestPositionFor(editor);
}
use of com.intellij.openapi.ui.popup.PopupChooserBuilder in project intellij-community by JetBrains.
the class CreateParameterForFieldIntention method performForField.
private static void performForField(PsiElement element, final Project project, Editor editor, List<GrMethod> constructors) {
final GrField field = PsiTreeUtil.getParentOfType(element, GrField.class);
if (constructors.isEmpty())
return;
if (ApplicationManager.getApplication().isUnitTestMode()) {
for (GrMethod constructor : constructors) {
addParameter(field, constructor, project);
}
return;
}
final JList list = new JBList(constructors.toArray(new GrMethod[constructors.size()]));
list.setCellRenderer(new MethodCellRenderer(true));
new PopupChooserBuilder(list).setTitle(GroovyIntentionsBundle.message("create.parameter.for.field.intention.name")).setMovable(true).setItemChoosenCallback(() -> {
final Object[] selectedValues = list.getSelectedValues();
Arrays.sort(selectedValues, (o1, o2) -> ((GrMethod) o2).getParameterList().getParametersCount() - ((GrMethod) o1).getParameterList().getParametersCount());
CommandProcessor.getInstance().executeCommand(project, () -> {
for (Object selectedValue : selectedValues) {
LOG.assertTrue(((GrMethod) selectedValue).isValid());
addParameter(field, ((GrMethod) selectedValue), project);
}
}, GroovyIntentionsBundle.message("create.parameter.for.field.intention.name"), null);
}).createPopup().showInBestPositionFor(editor);
}
use of com.intellij.openapi.ui.popup.PopupChooserBuilder in project intellij-community by JetBrains.
the class MavenSelectProjectPopup method attachToButton.
public static void attachToButton(@NotNull final MavenProjectsManager projectsManager, @NotNull final JButton button, @NotNull final Consumer<MavenProject> callback) {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
List<MavenProject> projectList = projectsManager.getProjects();
if (projectList.isEmpty()) {
JBPopupFactory.getInstance().createMessage("Maven projects not found").showUnderneathOf(button);
return;
}
DefaultMutableTreeNode root = buildTree(projectList);
final Map<MavenProject, String> projectsNameMap = MavenProjectNamer.generateNameMap(projectList);
final Tree projectTree = new Tree(root);
projectTree.setRootVisible(false);
projectTree.setCellRenderer(new NodeRenderer() {
@Override
public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
if (value instanceof DefaultMutableTreeNode) {
MavenProject mavenProject = (MavenProject) ((DefaultMutableTreeNode) value).getUserObject();
value = projectsNameMap.get(mavenProject);
setIcon(MavenIcons.MavenProject);
}
super.customizeCellRenderer(tree, value, selected, expanded, leaf, row, hasFocus);
}
});
new TreeSpeedSearch(projectTree, new Convertor<TreePath, String>() {
@Override
public String convert(TreePath o) {
Object lastPathComponent = o.getLastPathComponent();
if (!(lastPathComponent instanceof DefaultMutableTreeNode))
return null;
Object userObject = ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
//noinspection SuspiciousMethodCalls
return projectsNameMap.get(userObject);
}
});
final Ref<JBPopup> popupRef = new Ref<>();
final Runnable clickCallBack = () -> {
TreePath path = projectTree.getSelectionPath();
if (path == null)
return;
Object lastPathComponent = path.getLastPathComponent();
if (!(lastPathComponent instanceof DefaultMutableTreeNode))
return;
Object object = ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
// may be it's the root
if (object == null)
return;
callback.consume((MavenProject) object);
popupRef.get().closeOk(null);
};
projectTree.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
clickCallBack.run();
e.consume();
}
}
});
JBPopup popup = new PopupChooserBuilder(projectTree).setTitle("Select maven project").setResizable(true).setItemChoosenCallback(clickCallBack).setAutoselectOnMouseMove(true).setCloseOnEnter(false).createPopup();
popupRef.set(popup);
popup.showUnderneathOf(button);
}
private DefaultMutableTreeNode buildTree(List<MavenProject> projectList) {
MavenProject[] projects = projectList.toArray(new MavenProject[projectList.size()]);
Arrays.sort(projects, new MavenProjectNamer.MavenProjectComparator());
Map<MavenProject, DefaultMutableTreeNode> projectsToNode = new HashMap<>();
for (MavenProject mavenProject : projects) {
projectsToNode.put(mavenProject, new DefaultMutableTreeNode(mavenProject));
}
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
for (MavenProject mavenProject : projects) {
DefaultMutableTreeNode parent;
MavenProject aggregator = projectsManager.findAggregator(mavenProject);
if (aggregator != null) {
parent = projectsToNode.get(aggregator);
} else {
parent = root;
}
parent.add(projectsToNode.get(mavenProject));
}
return root;
}
});
}
use of com.intellij.openapi.ui.popup.PopupChooserBuilder in project intellij-community by JetBrains.
the class TagsPanel method showTags.
private void showTags() {
DefaultListModel model = new DefaultListModel();
myList.setModel(model);
for (final String myTag : myTags) {
model.addElement(myTag);
}
new PopupChooserBuilder(myList).setTitle(myPopupTitle).createPopup().showUnderneathOf(myMoreLabel);
}
use of com.intellij.openapi.ui.popup.PopupChooserBuilder 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);
}
Aggregations