Search in sources :

Example 1 with GroupedItemsListRenderer

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));
}
Also used : GroupedItemsListRenderer(com.intellij.ui.popup.list.GroupedItemsListRenderer) JBList(com.intellij.ui.components.JBList)

Example 2 with GroupedItemsListRenderer

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));
    }
}
Also used : InputEvent(java.awt.event.InputEvent) AllIcons(com.intellij.icons.AllIcons) ExecutionManager(com.intellij.execution.ExecutionManager) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) ExecutionBundle(com.intellij.execution.ExecutionBundle) JBList(com.intellij.ui.components.JBList) TaskInfo(com.intellij.openapi.progress.TaskInfo) StringUtil(com.intellij.openapi.util.text.StringUtil) ListItemDescriptorAdapter(com.intellij.openapi.ui.popup.ListItemDescriptorAdapter) RunProfile(com.intellij.execution.configurations.RunProfile) JBPopup(com.intellij.openapi.ui.popup.JBPopup) ProcessHandler(com.intellij.execution.process.ProcessHandler) KillableProcess(com.intellij.execution.KillableProcess) java.awt(java.awt) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) List(java.util.List) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) GroupedItemsListRenderer(com.intellij.ui.popup.list.GroupedItemsListRenderer) Pair(com.intellij.openapi.util.Pair) ExecutionManagerImpl(com.intellij.execution.impl.ExecutionManagerImpl) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) IconUtil(com.intellij.util.IconUtil) javax.swing(javax.swing) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) ListItemDescriptorAdapter(com.intellij.openapi.ui.popup.ListItemDescriptorAdapter) Project(com.intellij.openapi.project.Project) GroupedItemsListRenderer(com.intellij.ui.popup.list.GroupedItemsListRenderer) ArrayList(java.util.ArrayList) JBList(com.intellij.ui.components.JBList) List(java.util.List) JBList(com.intellij.ui.components.JBList) InputEvent(java.awt.event.InputEvent) JBPopup(com.intellij.openapi.ui.popup.JBPopup) Pair(com.intellij.openapi.util.Pair)

Example 3 with GroupedItemsListRenderer

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);
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) UIUtil(com.intellij.util.ui.UIUtil) AllIcons(com.intellij.icons.AllIcons) URL(java.net.URL) JBTextField(com.intellij.ui.components.JBTextField) ProjectManager(com.intellij.openapi.project.ProjectManager) JBUI(com.intellij.util.ui.JBUI) ListDataEvent(javax.swing.event.ListDataEvent) ApplicationNamesInfo(com.intellij.openapi.application.ApplicationNamesInfo) ApplicationInfoEx(com.intellij.openapi.application.ex.ApplicationInfoEx) Logger(com.intellij.openapi.diagnostic.Logger) NonOpaquePanel(com.intellij.ui.components.panels.NonOpaquePanel) ListSelectionEvent(javax.swing.event.ListSelectionEvent) EmptyIcon(com.intellij.util.ui.EmptyIcon) JBSlidingPanel(com.intellij.ui.components.JBSlidingPanel) RecentProjectsManager(com.intellij.ide.RecentProjectsManager) AccessibleContext(javax.accessibility.AccessibleContext) JBProtocolCommand(com.intellij.openapi.application.JBProtocolCommand) Accessible(javax.accessibility.Accessible) com.intellij.ui(com.intellij.ui) MouseEventAdapter(com.intellij.util.ui.MouseEventAdapter) NotificationType(com.intellij.notification.NotificationType) IdeGlassPaneImpl(com.intellij.openapi.wm.impl.IdeGlassPaneImpl) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) com.intellij.openapi.wm(com.intellij.openapi.wm) java.awt.event(java.awt.event) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Registry(com.intellij.openapi.util.registry.Registry) CustomLineBorder(com.intellij.ui.border.CustomLineBorder) NotNull(org.jetbrains.annotations.NotNull) UsageTrigger(com.intellij.internal.statistic.UsageTrigger) ArrayUtil(com.intellij.util.ArrayUtil) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) PopupFactoryImpl(com.intellij.ui.popup.PopupFactoryImpl) UiNotifyConnector.doWhenFirstShown(com.intellij.util.ui.update.UiNotifyConnector.doWhenFirstShown) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) Project(com.intellij.openapi.project.Project) AccessibleRole(javax.accessibility.AccessibleRole) DataManager(com.intellij.ide.DataManager) IdeNotificationArea(com.intellij.notification.impl.IdeNotificationArea) JBList(com.intellij.ui.components.JBList) ActionLink(com.intellij.ui.components.labels.ActionLink) ListItemDescriptorAdapter(com.intellij.openapi.ui.popup.ListItemDescriptorAdapter) Convertor(com.intellij.util.containers.Convertor) Disposable(com.intellij.openapi.Disposable) File(java.io.File) java.awt(java.awt) Consumer(java.util.function.Consumer) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) ListDataListener(javax.swing.event.ListDataListener) ProjectManagerAdapter(com.intellij.openapi.project.ProjectManagerAdapter) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) GroupedItemsListRenderer(com.intellij.ui.popup.list.GroupedItemsListRenderer) MnemonicHelper(com.intellij.openapi.MnemonicHelper) AccessibleContextAccessor(com.intellij.util.ui.accessibility.AccessibleContextAccessor) ListSelectionListener(javax.swing.event.ListSelectionListener) AccessibleContextDelegate(com.intellij.util.ui.accessibility.AccessibleContextDelegate) TransactionGuard(com.intellij.openapi.application.TransactionGuard) javax.swing(javax.swing) InputStream(java.io.InputStream) Disposable(com.intellij.openapi.Disposable) ListItemDescriptorAdapter(com.intellij.openapi.ui.popup.ListItemDescriptorAdapter) ListSelectionListener(javax.swing.event.ListSelectionListener) JBList(com.intellij.ui.components.JBList)

Aggregations

JBList (com.intellij.ui.components.JBList)3 GroupedItemsListRenderer (com.intellij.ui.popup.list.GroupedItemsListRenderer)3 AllIcons (com.intellij.icons.AllIcons)2 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)2 DumbAwareAction (com.intellij.openapi.project.DumbAwareAction)2 Project (com.intellij.openapi.project.Project)2 JBPopupFactory (com.intellij.openapi.ui.popup.JBPopupFactory)2 ListItemDescriptorAdapter (com.intellij.openapi.ui.popup.ListItemDescriptorAdapter)2 java.awt (java.awt)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 javax.swing (javax.swing)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 ExecutionBundle (com.intellij.execution.ExecutionBundle)1 ExecutionManager (com.intellij.execution.ExecutionManager)1 KillableProcess (com.intellij.execution.KillableProcess)1 RunProfile (com.intellij.execution.configurations.RunProfile)1 ExecutionManagerImpl (com.intellij.execution.impl.ExecutionManagerImpl)1 ProcessHandler (com.intellij.execution.process.ProcessHandler)1