@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 @Override protected PsiElement getPsiElementForHint(Object selectedValue) { return (PsiElement) selectedValue; } }; list.setCellRenderer(renderer); list.setFont(EditorUtil.getEditorFont()); if (selection != null) { list.setSelectedValue(selection, true); } final Runnable runnable = () -> { 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; }
private static void chooseAndPerform( Editor editor, List<TestFramework> frameworks, final Consumer<TestFramework> consumer) { if (frameworks.size() == 1) { consumer.consume(frameworks.get(0)); return; } final JList list = new JBList(frameworks.toArray(new TestFramework[frameworks.size()])); list.setCellRenderer( new DefaultListCellRenderer() { @Override public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Component result = super.getListCellRendererComponent(list, "", index, isSelected, cellHasFocus); if (value == null) return result; TestFramework framework = (TestFramework) value; setIcon(framework.getIcon()); setText(framework.getName()); return result; } }); PopupChooserBuilder builder = new PopupChooserBuilder(list); builder.setFilteringEnabled( new Function<Object, String>() { @Override public String fun(Object o) { return ((TestFramework) o).getName(); } }); builder .setTitle("Choose Framework") .setItemChoosenCallback( new Runnable() { @Override public void run() { consumer.consume((TestFramework) list.getSelectedValue()); } }) .setMovable(true) .createPopup() .showInBestPositionFor(editor); }
public static void selectContentDescriptor( final @NotNull Editor editor, @NotNull Collection<RunContentDescriptor> consoles, String selectDialogTitle, final Consumer<RunContentDescriptor> descriptorConsumer) { if (consoles.size() == 1) { RunContentDescriptor descriptor = consoles.iterator().next(); descriptorConsumer.consume(descriptor); descriptorToFront(editor, descriptor); } else if (consoles.size() > 1) { final JList list = new JBList(consoles); final Icon icon = DefaultRunExecutor.getRunExecutorInstance().getIcon(); list.setCellRenderer( new ListCellRendererWrapper<RunContentDescriptor>(list.getCellRenderer()) { @Override public void customize( final JList list, final RunContentDescriptor value, final int index, final boolean selected, final boolean hasFocus) { setText(value.getDisplayName()); setIcon(icon); } }); final PopupChooserBuilder builder = new PopupChooserBuilder(list); builder.setTitle(selectDialogTitle); builder .setItemChoosenCallback( new Runnable() { @Override public void run() { final Object selectedValue = list.getSelectedValue(); if (selectedValue instanceof RunContentDescriptor) { RunContentDescriptor descriptor = (RunContentDescriptor) selectedValue; descriptorConsumer.consume(descriptor); descriptorToFront(editor, descriptor); } } }) .createPopup() .showInBestPositionFor(editor); } }
@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); 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]; }
public static void navigate(final Project project, final FileUsageList usagesList) { if (usagesList == null) { WindowManager.getInstance().getStatusBar(project).setInfo("No navigation resolved"); } else { if (usagesList.files.size() == 1 && usagesList.files.get(0).usageList.size() == 1) { final FileUsage fileUsage = usagesList.files.get(0); final OurUsage ourUsage = fileUsage.usageList.get(0); doNavigate(fileUsage, ourUsage, project); } else { final List<OurUsage> ourUsages = new ArrayList<OurUsage>(5); for (FileUsage fileUsage : usagesList.files) { for (OurUsage usage : fileUsage.usageList) { ourUsages.add(usage); } } final JList list = new JList( new AbstractListModel() { public int getSize() { return ourUsages.size(); } public Object getElementAt(int index) { return ourUsages.get(index); } }); list.setCellRenderer( new DefaultListCellRenderer() { public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { final OurUsage usage = ((OurUsage) value); String text = usage.getContextText() + " in " + usage.fileUsage.getFileLocaton(); return super.getListCellRendererComponent( list, text, index, isSelected, cellHasFocus); } }); DataContext dataContext = DataManager.getInstance().getDataContext(); PopupChooserBuilder builder = JBPopupFactory.getInstance().createListPopupBuilder(list); builder .setTitle("Select Target") .setItemChoosenCallback( new Runnable() { public void run() { int selectedIndex = list.getSelectedIndex(); if (selectedIndex == -1) return; final OurUsage selectedUsage = (OurUsage) list.getSelectedValue(); for (FileUsage fileUsage : usagesList.files) { for (OurUsage usage : fileUsage.usageList) { if (selectedUsage == usage) { doNavigate(fileUsage, usage, project); break; } } } } }) .createPopup() .showInBestPositionFor(dataContext); } } }
private void showCompletionPopup( final CompletionResult result, int position, boolean isExplicit) { if (myList == null) { myList = new JBList(); myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); myList.setCellRenderer( new GroupedItemsListRenderer( new ListItemDescriptor() { public String getTextFor(final Object value) { final LookupFile file = (LookupFile) value; if (file.getMacro() != null) { return file.getMacro(); } else { return (myCurrentCompletion != null && myCurrentCompletion.myKidsAfterSeparator.contains(file) ? myFinder.getSeparator() : "") + file.getName(); } } public String getTooltipFor(final Object value) { return null; } public Icon getIconFor(final Object value) { final LookupFile file = (LookupFile) value; return file.getIcon(); } @Nullable private Separator getSeparatorAboveOf(Object value) { if (myCurrentCompletion == null) return null; final LookupFile file = (LookupFile) value; final int fileIndex = myCurrentCompletion.myToComplete.indexOf(file); if (fileIndex > 0 && !myCurrentCompletion.myMacros.contains(file)) { final LookupFile prev = myCurrentCompletion.myToComplete.get(fileIndex - 1); if (myCurrentCompletion.myMacros.contains(prev)) { return new Separator(""); } } if (myCurrentCompletion.myKidsAfterSeparator.indexOf(file) == 0 && myCurrentCompletion.mySiblings.size() > 0) { final LookupFile parent = file.getParent(); return parent == null ? new Separator("") : new Separator(parent.getName()); } if (myCurrentCompletion.myMacros.size() > 0 && fileIndex == 0) { return new Separator( IdeBundle.message("file.chooser.completion.path.variables.text")); } return null; } public boolean hasSeparatorAboveOf(final Object value) { return getSeparatorAboveOf(value) != null; } public String getCaptionAboveOf(final Object value) { final FileTextFieldImpl.Separator separator = getSeparatorAboveOf(value); return separator != null ? separator.getText() : null; } })); } if (myCurrentPopup != null) { closePopup(); } myCurrentCompletion = result; myCurrentCompletionsPos = position; if (myCurrentCompletion.myToComplete.size() == 0) { showNoSuggestions(isExplicit); return; } myList.setModel( new AbstractListModel() { public int getSize() { return myCurrentCompletion.myToComplete.size(); } public Object getElementAt(final int index) { return myCurrentCompletion.myToComplete.get(index); } }); myList.getSelectionModel().clearSelection(); final PopupChooserBuilder builder = JBPopupFactory.getInstance().createListPopupBuilder(myList); builder.addListener( new JBPopupListener() { public void beforeShown(LightweightWindowEvent event) { myPathTextField.registerKeyboardAction( myCancelAction, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); for (Action each : myDisabledTextActions) { each.setEnabled(false); } } public void onClosed(LightweightWindowEvent event) { myPathTextField.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0)); for (Action each : myDisabledTextActions) { each.setEnabled(true); } } }); myCurrentPopup = builder .setRequestFocus(false) .setAdText(getAdText(myCurrentCompletion)) .setAutoSelectIfEmpty(false) .setResizable(false) .setCancelCallback( new Computable<Boolean>() { public Boolean compute() { final int caret = myPathTextField.getCaretPosition(); myPathTextField.setSelectionStart(caret); myPathTextField.setSelectionEnd(caret); myPathTextField.setFocusTraversalKeysEnabled(true); SwingUtilities.invokeLater( new Runnable() { public void run() { getField().requestFocus(); } }); return Boolean.TRUE; } }) .setItemChoosenCallback( new Runnable() { public void run() { processChosenFromCompletion(false); } }) .setCancelKeyEnabled(false) .setAlpha(0.1f) .setFocusOwners(new Component[] {myPathTextField}) .createPopup(); if (result.myPreselected != null) { myList.setSelectedValue(result.myPreselected, false); } myPathTextField.setFocusTraversalKeysEnabled(false); myCurrentPopup.showInScreenCoordinates(getField(), getLocationForCaret(myPathTextField)); }
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; } 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(); boolean finished = gotoData.listUpdaterTask == null || gotoData.listUpdaterTask.isFinished(); final String title = getChooserTitle(gotoData.source, name, targets.length, finished); 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(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<UsageView>(); 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), project)); popup1.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); }
private void show(Project project, Editor editor, PsiFile file, 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; } 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)); } String name = ((PsiNamedElement) gotoData.source).getName(); 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.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); if (nav != null && nav.canNavigate()) { navigateToElement(nav); } } } } }; 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 JBPopup popup = builder .setTitle(title) .setItemChoosenCallback(runnable) .setMovable(true) .setCancelCallback( new Computable<Boolean>() { @Override public Boolean compute() { list.hideHint(); return true; } }) .setAdText(getAdText(gotoData.source, targets.length)) .createPopup(); if (gotoData.listUpdaterTask != null) { gotoData.listUpdaterTask.init((AbstractPopup) popup, list); ProgressManager.getInstance().run(gotoData.listUpdaterTask); } popup.showInBestPositionFor(editor); }