use of com.intellij.ui.popup.list.GroupedItemsListRenderer in project intellij-community by JetBrains.
the class FileTextFieldImpl method showCompletionPopup.
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 ListItemDescriptorAdapter() {
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 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(() -> {
final int caret = myPathTextField.getCaretPosition();
myPathTextField.setSelectionStart(caret);
myPathTextField.setSelectionEnd(caret);
myPathTextField.setFocusTraversalKeysEnabled(true);
IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
IdeFocusManager.getGlobalInstance().requestFocus(getField(), true);
});
return Boolean.TRUE;
}).setItemChoosenCallback(() -> 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));
}
use of com.intellij.ui.popup.list.GroupedItemsListRenderer in project intellij-community by JetBrains.
the class StopAction method actionPerformed.
@Override
public void actionPerformed(final AnActionEvent e) {
final DataContext dataContext = e.getDataContext();
Project project = e.getProject();
List<Pair<TaskInfo, ProgressIndicator>> cancellableProcesses = getCancellableProcesses(project);
List<RunContentDescriptor> stoppableDescriptors = getActiveStoppableDescriptors(dataContext);
if (isPlaceGlobal(e)) {
int todoSize = cancellableProcesses.size() + stoppableDescriptors.size();
if (todoSize == 1) {
if (!stoppableDescriptors.isEmpty()) {
ExecutionManagerImpl.stopProcess(stoppableDescriptors.get(0));
} else {
cancellableProcesses.get(0).second.cancel();
}
return;
}
Pair<List<HandlerItem>, HandlerItem> handlerItems = getItemsList(cancellableProcesses, stoppableDescriptors, getRecentlyStartedContentDescriptor(dataContext));
if (handlerItems == null || handlerItems.first.isEmpty()) {
return;
}
final JBList list = new JBList(handlerItems.first);
if (handlerItems.second != null)
list.setSelectedValue(handlerItems.second, true);
list.setCellRenderer(new GroupedItemsListRenderer(new ListItemDescriptorAdapter() {
@Nullable
@Override
public String getTextFor(Object value) {
return value instanceof HandlerItem ? ((HandlerItem) value).displayName : null;
}
@Nullable
@Override
public Icon getIconFor(Object value) {
return value instanceof HandlerItem ? ((HandlerItem) value).icon : null;
}
@Override
public boolean hasSeparatorAboveOf(Object value) {
return value instanceof HandlerItem && ((HandlerItem) value).hasSeparator;
}
}));
JBPopup popup = JBPopupFactory.getInstance().createListPopupBuilder(list).setMovable(true).setTitle(handlerItems.first.size() == 1 ? "Confirm process stop" : "Stop process").setFilteringEnabled(o -> ((HandlerItem) o).displayName).setItemChoosenCallback(() -> {
List valuesList = list.getSelectedValuesList();
for (Object o : valuesList) {
if (o instanceof HandlerItem)
((HandlerItem) o).stop();
}
}).setRequestFocus(true).createPopup();
InputEvent inputEvent = e.getInputEvent();
Component component = inputEvent != null ? inputEvent.getComponent() : null;
if (component != null && ActionPlaces.MAIN_TOOLBAR.equals(e.getPlace())) {
popup.showUnderneathOf(component);
} else if (project == null) {
popup.showInBestPositionFor(dataContext);
} else {
popup.showCenteredInCurrentWindow(project);
}
} else {
ExecutionManagerImpl.stopProcess(getRecentlyStartedContentDescriptor(dataContext));
}
}
use of com.intellij.ui.popup.list.GroupedItemsListRenderer in project intellij-community by JetBrains.
the class FlatWelcomeFrame method createActionGroupPanel.
public static Pair<JPanel, JBList> createActionGroupPanel(final ActionGroup action, final JComponent parent, final Runnable backAction, @NotNull Disposable parentDisposable) {
JPanel actionsListPanel = new JPanel(new BorderLayout());
actionsListPanel.setBackground(getProjectsBackground());
final List<AnAction> groups = flattenActionGroups(action);
final DefaultListModel<AnAction> model = JBList.createDefaultListModel(ArrayUtil.toObjectArray(groups));
final JBList<AnAction> list = new JBList<>(model);
for (AnAction group : groups) {
if (group instanceof Disposable) {
Disposer.register(parentDisposable, (Disposable) group);
}
}
Disposer.register(parentDisposable, new Disposable() {
@Override
public void dispose() {
model.clear();
}
});
list.setBackground(getProjectsBackground());
list.setCellRenderer(new GroupedItemsListRenderer<AnAction>(new ListItemDescriptorAdapter<AnAction>() {
@Nullable
@Override
public String getTextFor(AnAction value) {
return getActionText(value);
}
@Nullable
@Override
public String getCaptionAboveOf(AnAction value) {
return getParentGroupName(value);
}
@Override
public boolean hasSeparatorAboveOf(AnAction value) {
int index = model.indexOf(value);
final String parentGroupName = getParentGroupName(value);
if (index < 1)
return parentGroupName != null;
AnAction upper = model.get(index - 1);
if (getParentGroupName(upper) == null && parentGroupName != null)
return true;
return !Comparing.equal(getParentGroupName(upper), parentGroupName);
}
}) {
@Override
protected JComponent createItemComponent() {
myTextLabel = new ErrorLabel();
myTextLabel.setOpaque(true);
myTextLabel.setBorder(JBUI.Borders.empty(3, 7));
return myTextLabel;
}
@Override
protected Color getBackground() {
return getProjectsBackground();
}
@Override
protected void customizeComponent(JList<? extends AnAction> list, AnAction value, boolean isSelected) {
if (myTextLabel != null) {
myTextLabel.setText(getActionText(value));
myTextLabel.setIcon(value.getTemplatePresentation().getIcon());
}
}
});
JScrollPane pane = ScrollPaneFactory.createScrollPane(list, true);
pane.setBackground(getProjectsBackground());
actionsListPanel.add(pane, BorderLayout.CENTER);
int width = (int) Math.min(Math.round(list.getPreferredSize().getWidth()), 200);
pane.setPreferredSize(JBUI.size(width + 14, -1));
boolean singleProjectGenerator = list.getModel().getSize() == 1;
final Ref<Component> selected = Ref.create();
final JPanel main = new JPanel(new BorderLayout());
main.add(actionsListPanel, BorderLayout.WEST);
final JComponent back = createBackLabel(backAction, singleProjectGenerator);
if (back != null && !singleProjectGenerator) {
actionsListPanel.add(back, BorderLayout.SOUTH);
}
ListSelectionListener selectionListener = e -> {
if (e.getValueIsAdjusting()) {
return;
}
if (!selected.isNull()) {
main.remove(selected.get());
}
Object value = list.getSelectedValue();
if (value instanceof AbstractActionWithPanel) {
JPanel panel = ((AbstractActionWithPanel) value).createPanel();
panel.setBorder(JBUI.Borders.empty(7, 10));
selected.set(panel);
main.add(selected.get());
if (singleProjectGenerator && back != null) {
JPanel first = UIUtil.uiTraverser(panel).traverse().filter(JPanel.class).filter((it) -> BOTTOM_PANEL.equals(it.getName())).first();
if (first != null) {
first.add(back, BorderLayout.WEST);
}
}
for (JButton button : UIUtil.findComponentsOfType(main, JButton.class)) {
if (button.getClientProperty(DialogWrapper.DEFAULT_ACTION) == Boolean.TRUE) {
parent.getRootPane().setDefaultButton(button);
break;
}
}
main.revalidate();
main.repaint();
}
};
list.addListSelectionListener(selectionListener);
if (backAction != null) {
new AnAction() {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
backAction.run();
}
}.registerCustomShortcutSet(KeyEvent.VK_ESCAPE, 0, main);
}
installQuickSearch(list);
if (singleProjectGenerator) {
actionsListPanel.setPreferredSize(new Dimension(0, 0));
}
return Pair.create(main, list);
}
Aggregations