List of usage examples for com.intellij.openapi.ui.popup PopupChooserBuilder setTitle
@Override
@NotNull
public PopupChooserBuilder<T> setTitle(@NotNull @Nls(capitalization = Nls.Capitalization.Title) String title)
From source file:altn8.filechooser.AlternateFilePopupChooser.java
License:Apache License
/** * Let user choose from a list of files and do something with it. If only one item is present, the file will be processed * directly without user prompt. Nothing happens with an emtpy list. * * @param title Popup's title// w w w. j a v a 2s. c o m * @param fileGroups List of fileGroups * @param currentProject * @param fileHandler FileHandler to process choosed files */ public static void prompt(String title, List<AlternateFileGroup> fileGroups, Project currentProject, final FileHandler fileHandler) { if (fileGroups != null && !fileGroups.isEmpty()) { // if we have only one group with 1 file... if (fileGroups.size() == 1 && fileGroups.get(0).getFiles().size() == 1) { // ...then open file directly fileHandler.processFile(fileGroups.get(0).getFiles().get(0)); } else { // let user choose... // list of Objects for out JList List<Object> list = new ArrayList<Object>(); // if we have only 1 group, we dont show title, just adding all PsiFiles if (fileGroups.size() == 1) { list.addAll(fileGroups.get(0).getFiles()); } else { // go thru all groups for (AlternateFileGroup fileGroup : fileGroups) { // add basefilename (will be presented as title) and all files list.add(fileGroup.getGroupTitle()); list.addAll(fileGroup.getFiles()); } } final JList valueList = new JList(list.toArray()); valueList.setCellRenderer(new AlternateCellRenderer(currentProject)); valueList.setSelectionModel(new AlternateListSelectionModel(list)); PopupChooserBuilder listPopupBuilder = JBPopupFactory.getInstance() .createListPopupBuilder(valueList); listPopupBuilder.setTitle(title); listPopupBuilder.setItemChoosenCallback(new Runnable() { public void run() { for (Object item : valueList.getSelectedValues()) { if (item instanceof PsiFile) { fileHandler.processFile((PsiFile) item); } } } }); listPopupBuilder.createPopup().showCenteredInCurrentWindow(currentProject); } } }
From source file:com.antoine.ideaplugin.greenrobot.ShowUsagesAction.java
License:Apache License
@NotNull private JBPopup createUsagePopup(@NotNull final List<Usage> usages, @NotNull final UsageInfoToUsageConverter.TargetElementsDescriptor descriptor, @NotNull Set<UsageNode> visibleNodes, @NotNull final FindUsagesHandler handler, final Editor editor, @NotNull final RelativePoint popupPosition, final int maxUsages, @NotNull final UsageViewImpl usageView, @NotNull final FindUsagesOptions options, @NotNull final JTable table, @NotNull final UsageViewPresentation presentation, @NotNull final AsyncProcessIcon processIcon, boolean hadMoreSeparator) { table.setRowHeight(PlatformIcons.CLASS_ICON.getIconHeight() + 2); table.setShowGrid(false);//w w w.ja va2s . c om table.setShowVerticalLines(false); table.setShowHorizontalLines(false); table.setTableHeader(null); table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); table.setIntercellSpacing(new Dimension(0, 0)); PopupChooserBuilder builder = new PopupChooserBuilder(table); final String title = presentation.getTabText(); if (title != null) { String result = getFullTitle(usages, title, hadMoreSeparator, visibleNodes.size() - 1, true); builder.setTitle(result); builder.setAdText(getSecondInvocationTitle(options, handler)); } builder.setMovable(true).setResizable(true); builder.setItemChoosenCallback(new Runnable() { @Override public void run() { int[] selected = table.getSelectedRows(); for (int i : selected) { Object value = table.getValueAt(i, 0); if (value instanceof UsageNode) { Usage usage = ((UsageNode) value).getUsage(); if (usage == MORE_USAGES_SEPARATOR) { appendMoreUsages(editor, popupPosition, handler, maxUsages); return; } navigateAndHint(usage, null, handler, popupPosition, maxUsages, options); } } } }); final JBPopup[] popup = new JBPopup[1]; KeyboardShortcut shortcut = UsageViewImpl.getShowUsagesWithSettingsShortcut(); if (shortcut != null) { new DumbAwareAction() { @Override public void actionPerformed(AnActionEvent e) { popup[0].cancel(); showDialogAndFindUsages(handler, popupPosition, editor, maxUsages); } }.registerCustomShortcutSet(new CustomShortcutSet(shortcut.getFirstKeyStroke()), table); } shortcut = getShowUsagesShortcut(); if (shortcut != null) { new DumbAwareAction() { @Override public void actionPerformed(AnActionEvent e) { popup[0].cancel(); searchEverywhere(options, handler, editor, popupPosition, maxUsages); } }.registerCustomShortcutSet(new CustomShortcutSet(shortcut.getFirstKeyStroke()), table); } InplaceButton settingsButton = createSettingsButton(handler, popupPosition, editor, maxUsages, new Runnable() { @Override public void run() { popup[0].cancel(); } }); ActiveComponent spinningProgress = new ActiveComponent() { @Override public void setActive(boolean active) { } @Override public JComponent getComponent() { return processIcon; } }; builder.setCommandButton(new CompositeActiveComponent(spinningProgress, settingsButton)); DefaultActionGroup toolbar = new DefaultActionGroup(); usageView.addFilteringActions(toolbar); toolbar.add(UsageGroupingRuleProviderImpl.createGroupByFileStructureAction(usageView)); toolbar.add(new AnAction("Open Find Usages Toolwindow", "Show all usages in a separate toolwindow", AllIcons.Toolwindows.ToolWindowFind) { { AnAction action = ActionManager.getInstance().getAction(IdeActions.ACTION_FIND_USAGES); setShortcutSet(action.getShortcutSet()); } @Override public void actionPerformed(AnActionEvent e) { hideHints(); popup[0].cancel(); FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager .getInstance(usageView.getProject())).getFindUsagesManager(); findUsagesManager.findUsages(handler.getPrimaryElements(), handler.getSecondaryElements(), handler, options, FindSettings.getInstance().isSkipResultsWithOneUsage()); } }); ActionToolbar actionToolbar = ActionManager.getInstance() .createActionToolbar(ActionPlaces.USAGE_VIEW_TOOLBAR, toolbar, true); actionToolbar.setReservePlaceAutoPopupIcon(false); final JComponent toolBar = actionToolbar.getComponent(); toolBar.setOpaque(false); builder.setSettingButton(toolBar); popup[0] = builder.createPopup(); JComponent content = popup[0].getContent(); myWidth = (int) (toolBar.getPreferredSize().getWidth() + new JLabel(getFullTitle(usages, title, hadMoreSeparator, visibleNodes.size() - 1, true)) .getPreferredSize().getWidth() + settingsButton.getPreferredSize().getWidth()); myWidth = -1; for (AnAction action : toolbar.getChildren(null)) { action.unregisterCustomShortcutSet(usageView.getComponent()); action.registerCustomShortcutSet(action.getShortcutSet(), content); } return popup[0]; }
From source file:com.intellij.codeInsight.daemon.impl.PsiElementListNavigator.java
License:Apache License
@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) { consumer.consume(targets);/*w w w. j av a 2 s . com*/ return null; } final CollectionListModel<NavigatablePsiElement> model = new CollectionListModel<NavigatablePsiElement>( targets); final JBListWithHintProvider list = new JBListWithHintProvider(model) { @Override protected PsiElement getPsiElementForHint(final Object selectedValue) { return (PsiElement) selectedValue; } }; 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(ChooseByNameBase.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(new Runnable() { @Override public void run() { int[] ids = list.getSelectedIndices(); if (ids == null || ids.length == 0) return; Object[] selectedElements = list.getSelectedValues(); consumer.consume(selectedElements); } }).setCancelCallback(new Computable<Boolean>() { @Override public Boolean compute() { HintUpdateSupply.hideHint(list); return true; } }); final Ref<UsageView> usageView = new Ref<UsageView>(); if (findUsagesTitle != null) { popupChooserBuilder = popupChooserBuilder.setCouldPin(new Processor<JBPopup>() { @Override public boolean process(JBPopup 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); ProgressManager.getInstance().run(listUpdaterTask); } return popup; }
From source file:com.intellij.codeInsight.daemon.impl.quickfix.CreateFromUsageBaseFix.java
License:Apache License
private void chooseTargetClass(List<PsiClass> classes, final Editor editor) { final Project project = classes.get(0).getProject(); final JList list = new JBList(classes); PsiElementListCellRenderer renderer = new PsiClassListCellRenderer(); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setCellRenderer(renderer);//from w w w .ja va 2 s. c o m final PopupChooserBuilder builder = new PopupChooserBuilder(list); renderer.installSpeedSearch(builder); Runnable runnable = new Runnable() { @Override public void run() { int index = list.getSelectedIndex(); if (index < 0) return; final PsiClass aClass = (PsiClass) list.getSelectedValue(); CommandProcessor.getInstance().executeCommand(project, new Runnable() { @Override public void run() { doInvoke(project, aClass); } }, getText(), null); } }; builder.setTitle(QuickFixBundle.message("target.class.chooser.title")).setItemChoosenCallback(runnable) .createPopup().showInBestPositionFor(editor); }
From source file:com.intellij.codeInsight.daemon.impl.quickfix.CreateInnerClassFromUsageFix.java
License:Apache License
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);// www . j a v a 2 s .co m final PopupChooserBuilder builder = new PopupChooserBuilder(list); renderer.installSpeedSearch(builder); Runnable runnable = new Runnable() { @Override public void run() { int index = list.getSelectedIndex(); if (index < 0) return; final PsiClass aClass = (PsiClass) list.getSelectedValue(); CommandProcessor.getInstance().executeCommand(project, new Runnable() { @Override public void run() { ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { try { doInvoke(aClass, superClassName); } catch (IncorrectOperationException e) { LOG.error(e); } } }); } }, getText(), null); } }; builder.setTitle(QuickFixBundle.message("target.class.chooser.title")).setItemChoosenCallback(runnable) .createPopup().showInBestPositionFor(editor); }
From source file:com.intellij.codeInsight.highlighting.ChooseOneOrAllRunnable.java
License:Apache License
@Override public void run() { if (myClasses.length == 1) { //TODO: cdr this place should produce at least warning // selected(myClasses[0]); selected((T[]) ArrayUtil.toObjectArray(myClasses[0].getClass(), myClasses[0])); } else if (myClasses.length > 0) { PsiElementListCellRenderer<T> renderer = createRenderer(); Arrays.sort(myClasses, renderer.getComparator()); if (ApplicationManager.getApplication().isUnitTestMode()) { selected(myClasses);//from ww w . j av a 2 s. co m return; } Vector<Object> model = new Vector<Object>(Arrays.asList(myClasses)); model.insertElementAt(CodeInsightBundle.message("highlight.thrown.exceptions.chooser.all.entry"), 0); myList = new JBList(model); myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); myList.setCellRenderer(renderer); final PopupChooserBuilder builder = new PopupChooserBuilder(myList); renderer.installSpeedSearch(builder); final Runnable callback = new Runnable() { @Override public void run() { int idx = myList.getSelectedIndex(); if (idx < 0) return; if (idx > 0) { selected((T[]) ArrayUtil.toObjectArray(myClasses[idx - 1].getClass(), myClasses[idx - 1])); } else { selected(myClasses); } } }; ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { builder.setTitle(myTitle).setItemChoosenCallback(callback).createPopup() .showInBestPositionFor(myEditor); } }); } }
From source file:com.intellij.codeInsight.intention.impl.ImplementAbstractMethodHandler.java
License:Apache License
public void invoke() { PsiDocumentManager.getInstance(myProject).commitAllDocuments(); final PsiElement[][] result = new PsiElement[1][]; ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() { @Override/*from w w w . jav a 2s. co m*/ public void run() { ApplicationManager.getApplication().runReadAction(new Runnable() { @Override public void run() { final PsiClass psiClass = myMethod.getContainingClass(); if (!psiClass.isValid()) return; if (!psiClass.isEnum()) { result[0] = getClassImplementations(psiClass); } else { final List<PsiElement> enumConstants = new ArrayList<PsiElement>(); for (PsiField field : psiClass.getFields()) { if (field instanceof PsiEnumConstant) { final PsiEnumConstantInitializer initializingClass = ((PsiEnumConstant) field) .getInitializingClass(); if (initializingClass != null) { PsiMethod method = initializingClass.findMethodBySignature(myMethod, true); if (method == null || !method.getContainingClass().equals(initializingClass)) { enumConstants.add(initializingClass); } } else { enumConstants.add(field); } } } result[0] = PsiUtilCore.toPsiElementArray(enumConstants); } } }); } }, CodeInsightBundle.message("intention.implement.abstract.method.searching.for.descendants.progress"), true, myProject); if (result[0] == null) return; if (result[0].length == 0) { Messages.showMessageDialog(myProject, CodeInsightBundle.message("intention.implement.abstract.method.error.no.classes.message"), CodeInsightBundle.message("intention.implement.abstract.method.error.no.classes.title"), Messages.getInformationIcon()); return; } if (result[0].length == 1) { implementInClass(new Object[] { result[0][0] }); return; } final MyPsiElementListCellRenderer elementListCellRenderer = new MyPsiElementListCellRenderer(); elementListCellRenderer.sort(result[0]); myList = new JBList(result[0]); myList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); final Runnable runnable = new Runnable() { @Override public void run() { int index = myList.getSelectedIndex(); if (index < 0) return; implementInClass(myList.getSelectedValues()); } }; myList.setCellRenderer(elementListCellRenderer); final PopupChooserBuilder builder = new PopupChooserBuilder(myList); elementListCellRenderer.installSpeedSearch(builder); builder.setTitle(CodeInsightBundle.message("intention.implement.abstract.method.class.chooser.title")) .setItemChoosenCallback(runnable).createPopup().showInBestPositionFor(myEditor); }
From source file:com.intellij.codeInsight.navigation.GotoTargetHandler.java
License:Apache License
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;/*from w w w.j a v a 2 s .c om*/ } if (targets.length == 1 && additionalActions.isEmpty()) { Navigatable descriptor = targets[0] instanceof Navigatable ? (Navigatable) targets[0] : EditSourceUtil.getDescriptor(targets[0]); if (descriptor != null && descriptor.canNavigate()) { navigateToElement(descriptor); } 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); if (shouldSortTargets()) { Arrays.sort(targets, createComparator(gotoData.renderers, gotoData)); } List<Object> allElements = new ArrayList<Object>(targets.length + additionalActions.size()); Collections.addAll(allElements, targets); allElements.addAll(additionalActions); final JBListWithHintProvider list = new JBListWithHintProvider( new CollectionListModel<Object>(allElements)) { @Override protected PsiElement getPsiElementForHint(final Object selectedValue) { return selectedValue instanceof PsiElement ? (PsiElement) selectedValue : null; } }; list.setFont(ChooseByNameBase.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 = new Runnable() { @Override public void run() { 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(new Function<Object, String>() { @Override public String fun(Object o) { if (o instanceof AdditionalAction) { return ((AdditionalAction) o).getText(); } return getRenderer(o, gotoData.renderers, gotoData).getElementText((PsiElement) o); } }); final Ref<UsageView> usageView = new Ref<UsageView>(); final JBPopup popup = builder.setTitle(title).setItemChoosenCallback(runnable).setMovable(true) .setCancelCallback(new Computable<Boolean>() { @Override public Boolean compute() { HintUpdateSupply.hideHint(list); return true; } }).setCouldPin(new Processor<JBPopup>() { @Override public boolean process(JBPopup popup) { usageView.set(FindUtil.showInUsageView(gotoData.source, gotoData.targets, getFindUsagesTitle(gotoData.source, name, gotoData.targets.length), project)); popup.cancel(); return false; } }).setAdText(getAdText(gotoData.source, targets.length)).createPopup(); builder.getScrollPane().setBorder(null); builder.getScrollPane().setViewportBorder(null); if (gotoData.listUpdaterTask != null) { gotoData.listUpdaterTask.init((AbstractPopup) popup, list, usageView); ProgressManager.getInstance().run(gotoData.listUpdaterTask); } popup.showInBestPositionFor(editor); }
From source file:com.intellij.codeInsight.navigation.NavigationUtil.java
License:Apache License
@NotNull public static <T extends PsiElement> JBPopup getPsiElementPopup(@NotNull T[] elements, @NotNull final PsiElementListCellRenderer<T> renderer, @Nullable final String title, @NotNull final PsiElementProcessor<T> processor, @Nullable final T selection) { final JList list = new JBListWithHintProvider(elements) { @Nullable// ww w . jav a 2 s .c o m @Override protected PsiElement getPsiElementForHint(Object selectedValue) { return (PsiElement) selectedValue; } }; list.setCellRenderer(renderer); list.setFont(ChooseByNameBase.getEditorFont()); if (selection != null) { list.setSelectedValue(selection, true); } final Runnable runnable = new Runnable() { @Override public void run() { int[] ids = list.getSelectedIndices(); if (ids == null || ids.length == 0) return; for (Object element : list.getSelectedValues()) { if (element != null) { processor.execute((T) element); } } } }; PopupChooserBuilder builder = new PopupChooserBuilder(list); if (title != null) { builder.setTitle(title); } renderer.installSpeedSearch(builder, true); JBPopup popup = builder.setItemChoosenCallback(runnable).createPopup(); builder.getScrollPane().setBorder(null); builder.getScrollPane().setViewportBorder(null); return popup; }
From source file:com.intellij.codeInsight.unwrap.UnwrapHandler.java
License:Apache License
private void showPopup(final List<AnAction> options, Editor editor) { final ScopeHighlighter highlighter = new ScopeHighlighter(editor); DefaultListModel m = new DefaultListModel(); for (AnAction a : options) { m.addElement(((MyUnwrapAction) a).getName()); }/*www. ja v a 2 s .co m*/ final JList list = new JBList(m); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setVisibleRowCount(options.size()); list.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { int index = list.getSelectedIndex(); if (index < 0) return; MyUnwrapAction a = (MyUnwrapAction) options.get(index); List<PsiElement> toExtract = new ArrayList<PsiElement>(); PsiElement wholeRange = a.collectAffectedElements(toExtract); highlighter.highlight(wholeRange, toExtract); } }); PopupChooserBuilder builder = JBPopupFactory.getInstance().createListPopupBuilder(list); builder.setTitle(CodeInsightBundle.message("unwrap.popup.title")).setMovable(false).setResizable(false) .setRequestFocus(true).setItemChoosenCallback(new Runnable() { @Override public void run() { MyUnwrapAction a = (MyUnwrapAction) options.get(list.getSelectedIndex()); a.actionPerformed(null); } }).addListener(new JBPopupAdapter() { @Override public void onClosed(LightweightWindowEvent event) { highlighter.dropHighlight(); } }); JBPopup popup = builder.createPopup(); popup.showInBestPositionFor(editor); }