Search in sources :

Example 1 with PopupListElementRenderer

use of com.intellij.ui.popup.list.PopupListElementRenderer in project intellij-community by JetBrains.

the class AddImportAction method chooseClassAndImport.

private void chooseClassAndImport() {
    CodeInsightUtil.sortIdenticalShortNamedMembers(myTargetClasses, myReference);
    final BaseListPopupStep<PsiClass> step = new BaseListPopupStep<PsiClass>(QuickFixBundle.message("class.to.import.chooser.title"), myTargetClasses) {

        @Override
        public boolean isAutoSelectionEnabled() {
            return false;
        }

        @Override
        public boolean isSpeedSearchEnabled() {
            return true;
        }

        @Override
        public PopupStep onChosen(PsiClass selectedValue, boolean finalChoice) {
            if (selectedValue == null) {
                return FINAL_CHOICE;
            }
            if (finalChoice) {
                return doFinalStep(() -> {
                    PsiDocumentManager.getInstance(myProject).commitAllDocuments();
                    addImport(myReference, selectedValue);
                });
            }
            return getExcludesStep(selectedValue.getQualifiedName(), myProject);
        }

        @Override
        public boolean hasSubstep(PsiClass selectedValue) {
            return true;
        }

        @NotNull
        @Override
        public String getTextFor(PsiClass value) {
            return ObjectUtils.assertNotNull(value.getQualifiedName());
        }

        @Override
        public Icon getIconFor(PsiClass aValue) {
            return aValue.getIcon(0);
        }
    };
    ListPopupImpl popup = new ListPopupImpl(step) {

        @Override
        protected ListCellRenderer getListElementRenderer() {
            final PopupListElementRenderer baseRenderer = (PopupListElementRenderer) super.getListElementRenderer();
            final DefaultPsiElementCellRenderer psiRenderer = new DefaultPsiElementCellRenderer();
            return new ListCellRenderer() {

                @Override
                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                    JPanel panel = new JPanel(new BorderLayout());
                    baseRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    panel.add(baseRenderer.getNextStepLabel(), BorderLayout.EAST);
                    panel.add(psiRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus));
                    return panel;
                }
            };
        }
    };
    NavigationUtil.hidePopupIfDumbModeStarts(popup, myProject);
    popup.showInBestPositionFor(myEditor);
}
Also used : DefaultPsiElementCellRenderer(com.intellij.ide.util.DefaultPsiElementCellRenderer) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) PsiClass(com.intellij.psi.PsiClass) PopupListElementRenderer(com.intellij.ui.popup.list.PopupListElementRenderer)

Example 2 with PopupListElementRenderer

use of com.intellij.ui.popup.list.PopupListElementRenderer in project intellij-community by JetBrains.

the class StaticImportMethodQuestionAction method chooseAndImport.

private void chooseAndImport(final Editor editor, final Project project) {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        doImport(myCandidates.get(0));
        return;
    }
    final BaseListPopupStep<T> step = new BaseListPopupStep<T>(getPopupTitle(), myCandidates) {

        @Override
        public boolean isAutoSelectionEnabled() {
            return false;
        }

        @Override
        public boolean isSpeedSearchEnabled() {
            return true;
        }

        @Override
        public PopupStep onChosen(T selectedValue, boolean finalChoice) {
            if (selectedValue == null) {
                return FINAL_CHOICE;
            }
            if (finalChoice) {
                return doFinalStep(() -> {
                    PsiDocumentManager.getInstance(project).commitAllDocuments();
                    LOG.assertTrue(selectedValue.isValid());
                    doImport(selectedValue);
                });
            }
            return AddImportAction.getExcludesStep(PsiUtil.getMemberQualifiedName(selectedValue), project);
        }

        @Override
        public boolean hasSubstep(T selectedValue) {
            return true;
        }

        @NotNull
        @Override
        public String getTextFor(T value) {
            return getElementPresentableName(value);
        }

        @Override
        public Icon getIconFor(T aValue) {
            return aValue.getIcon(0);
        }
    };
    final ListPopupImpl popup = new ListPopupImpl(step) {

        final PopupListElementRenderer rightArrow = new PopupListElementRenderer(this);

        @Override
        protected ListCellRenderer getListElementRenderer() {
            return new PsiElementListCellRenderer<T>() {

                public String getElementText(T element) {
                    return getElementPresentableName(element);
                }

                public String getContainerText(final T element, final String name) {
                    return PsiClassListCellRenderer.getContainerTextStatic(element);
                }

                public int getIconFlags() {
                    return 0;
                }

                @Nullable
                @Override
                protected TextAttributes getNavigationItemAttributes(Object value) {
                    TextAttributes attrs = super.getNavigationItemAttributes(value);
                    if (value instanceof PsiDocCommentOwner && !((PsiDocCommentOwner) value).isDeprecated()) {
                        PsiClass psiClass = ((T) value).getContainingClass();
                        if (psiClass != null && psiClass.isDeprecated()) {
                            return TextAttributes.merge(attrs, super.getNavigationItemAttributes(psiClass));
                        }
                    }
                    return attrs;
                }

                @Override
                protected DefaultListCellRenderer getRightCellRenderer(final Object value) {
                    final DefaultListCellRenderer moduleRenderer = super.getRightCellRenderer(value);
                    return new DefaultListCellRenderer() {

                        @Override
                        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                            JPanel panel = new JPanel(new BorderLayout());
                            if (moduleRenderer != null) {
                                Component moduleComponent = moduleRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                                if (!isSelected) {
                                    moduleComponent.setBackground(getBackgroundColor(value));
                                }
                                panel.add(moduleComponent, BorderLayout.CENTER);
                            }
                            rightArrow.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                            Component rightArrowComponent = rightArrow.getNextStepLabel();
                            panel.add(rightArrowComponent, BorderLayout.EAST);
                            return panel;
                        }
                    };
                }
            };
        }
    };
    popup.showInBestPositionFor(editor);
}
Also used : PopupListElementRenderer(com.intellij.ui.popup.list.PopupListElementRenderer) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer)

Example 3 with PopupListElementRenderer

use of com.intellij.ui.popup.list.PopupListElementRenderer in project intellij-plugins by JetBrains.

the class ChooseActiveBuildConfigurationAction method createPopup.

public static ListPopup createPopup(@NotNull Module module) {
    final DefaultActionGroup actionGroup = new DefaultActionGroup();
    final FlexBuildConfigurationManager manager = FlexBuildConfigurationManager.getInstance(module);
    final FlexBuildConfiguration activeBc = manager.getActiveConfiguration();
    final FlexBuildConfiguration[] bcs = manager.getBuildConfigurations();
    Arrays.sort(bcs, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
    for (final FlexBuildConfiguration bc : bcs) {
        actionGroup.add(new SelectBcAction(bc, manager));
    }
    actionGroup.addSeparator();
    actionGroup.add(new EditBcsAction(module));
    final DataContext dataContext = SimpleDataContext.getProjectContext(module.getProject());
    return new PopupFactoryImpl.ActionGroupPopup(FlexBundle.message("choose.build.configuration.popup.title", module.getName()), actionGroup, dataContext, false, false, false, true, null, -1, anAction -> anAction instanceof SelectBcAction && ((SelectBcAction) anAction).getBC() == activeBc, null) {

        @Override
        protected ListCellRenderer getListElementRenderer() {
            return new PopupListElementRenderer(this) {

                {
                //myRendererComponent.setBorder(new EmptyBorder(5, 0, 5, 0));
                }

                @Override
                protected JComponent createItemComponent() {
                    return new MyPanel();
                }

                @Override
                public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
                    MyPanel p = (MyPanel) myComponent;
                    p.clear();
                    PopupFactoryImpl.ActionItem actionItem = (PopupFactoryImpl.ActionItem) value;
                    AnAction anAction = actionItem.getAction();
                    SimpleColoredText text;
                    Icon icon;
                    boolean isActive;
                    if (anAction instanceof SelectBcAction) {
                        FlexBuildConfiguration bc = ((SelectBcAction) anAction).getBC();
                        isActive = bc == activeBc;
                        text = BCUtils.renderBuildConfiguration(bc, null, isActive);
                        icon = bc.getIcon();
                    } else {
                        text = new SimpleColoredText(anAction.getTemplatePresentation().getText(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
                        icon = anAction.getTemplatePresentation().getIcon();
                        isActive = false;
                    }
                    RowIcon rowIcon = new RowIcon(2);
                    rowIcon.setIcon(isActive ? (isSelected ? ICON_ACTIVE_SELECTED : ICON_ACTIVE) : ICON_EMPTY, 0);
                    rowIcon.setIcon(icon, 1);
                    p.setIcon(rowIcon);
                    if (isSelected) {
                        text = text.derive(SimpleTextAttributes.SELECTED_SIMPLE_CELL_ATTRIBUTES, true);
                        setSelected(p);
                    } else {
                        setDeselected(p);
                    }
                    p.setText(text);
                    mySeparatorComponent.setVisible(actionItem.isPrependWithSeparator());
                    return myRendererComponent;
                }
            };
        }
    };
}
Also used : PopupFactoryImpl(com.intellij.ui.popup.PopupFactoryImpl) SimpleColoredText(com.intellij.ui.SimpleColoredText) FlexBuildConfiguration(com.intellij.lang.javascript.flex.projectStructure.model.FlexBuildConfiguration) RowIcon(com.intellij.ui.RowIcon) PopupListElementRenderer(com.intellij.ui.popup.list.PopupListElementRenderer) SimpleDataContext(com.intellij.openapi.actionSystem.impl.SimpleDataContext) FlexBuildConfigurationManager(com.intellij.lang.javascript.flex.projectStructure.model.FlexBuildConfigurationManager) RowIcon(com.intellij.ui.RowIcon) EmptyIcon(com.intellij.util.ui.EmptyIcon)

Example 4 with PopupListElementRenderer

use of com.intellij.ui.popup.list.PopupListElementRenderer in project intellij-community by JetBrains.

the class NavigationUtil method getPsiElementPopup.

private static JBPopup getPsiElementPopup(final Object[] elements, final Map<PsiElement, GotoRelatedItem> itemsMap, final String title, final boolean showContainingModules, final Processor<Object> processor) {
    final Ref<Boolean> hasMnemonic = Ref.create(false);
    final DefaultPsiElementCellRenderer renderer = new DefaultPsiElementCellRenderer() {

        {
            setFocusBorderEnabled(false);
        }

        @Override
        public String getElementText(PsiElement element) {
            String customName = itemsMap.get(element).getCustomName();
            return (customName != null ? customName : super.getElementText(element));
        }

        @Override
        protected Icon getIcon(PsiElement element) {
            Icon customIcon = itemsMap.get(element).getCustomIcon();
            return customIcon != null ? customIcon : super.getIcon(element);
        }

        @Override
        public String getContainerText(PsiElement element, String name) {
            String customContainerName = itemsMap.get(element).getCustomContainerName();
            if (customContainerName != null) {
                return customContainerName;
            }
            PsiFile file = element.getContainingFile();
            return file != null && !getElementText(element).equals(file.getName()) ? "(" + file.getName() + ")" : null;
        }

        @Override
        protected DefaultListCellRenderer getRightCellRenderer(Object value) {
            return showContainingModules ? super.getRightCellRenderer(value) : null;
        }

        @Override
        protected boolean customizeNonPsiElementLeftRenderer(ColoredListCellRenderer renderer, JList list, Object value, int index, boolean selected, boolean hasFocus) {
            final GotoRelatedItem item = (GotoRelatedItem) value;
            Color color = list.getForeground();
            final SimpleTextAttributes nameAttributes = new SimpleTextAttributes(SimpleTextAttributes.STYLE_PLAIN, color);
            final String name = item.getCustomName();
            if (name == null)
                return false;
            renderer.append(name, nameAttributes);
            renderer.setIcon(item.getCustomIcon());
            return true;
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            final JPanel component = (JPanel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (!hasMnemonic.get())
                return component;
            final JPanel panelWithMnemonic = new JPanel(new BorderLayout());
            final int mnemonic = getMnemonic(value, itemsMap);
            final JLabel label = new JLabel("");
            if (mnemonic != -1) {
                label.setText(mnemonic + ".");
                label.setDisplayedMnemonicIndex(0);
            }
            label.setPreferredSize(new JLabel("8.").getPreferredSize());
            final JComponent leftRenderer = (JComponent) component.getComponents()[0];
            component.remove(leftRenderer);
            panelWithMnemonic.setBorder(BorderFactory.createEmptyBorder(0, 7, 0, 0));
            panelWithMnemonic.setBackground(leftRenderer.getBackground());
            label.setBackground(leftRenderer.getBackground());
            panelWithMnemonic.add(label, BorderLayout.WEST);
            panelWithMnemonic.add(leftRenderer, BorderLayout.CENTER);
            component.add(panelWithMnemonic);
            return component;
        }
    };
    final ListPopupImpl popup = new ListPopupImpl(new BaseListPopupStep<Object>(title, Arrays.asList(elements)) {

        @Override
        public boolean isSpeedSearchEnabled() {
            return true;
        }

        @Override
        public String getIndexedString(Object value) {
            if (value instanceof GotoRelatedItem) {
                //noinspection ConstantConditions
                return ((GotoRelatedItem) value).getCustomName();
            }
            PsiElement element = (PsiElement) value;
            if (!element.isValid())
                return "INVALID";
            return renderer.getElementText(element) + " " + renderer.getContainerText(element, null);
        }

        @Override
        public PopupStep onChosen(Object selectedValue, boolean finalChoice) {
            processor.process(selectedValue);
            return super.onChosen(selectedValue, finalChoice);
        }
    }) {
    };
    popup.getList().setCellRenderer(new PopupListElementRenderer(popup) {

        Map<Object, String> separators = new HashMap<>();

        {
            final ListModel model = popup.getList().getModel();
            String current = null;
            boolean hasTitle = false;
            for (int i = 0; i < model.getSize(); i++) {
                final Object element = model.getElementAt(i);
                final GotoRelatedItem item = itemsMap.get(element);
                if (item != null && !StringUtil.equals(current, item.getGroup())) {
                    current = item.getGroup();
                    separators.put(element, current);
                    if (!hasTitle && !StringUtil.isEmpty(current)) {
                        hasTitle = true;
                    }
                }
            }
            if (!hasTitle) {
                separators.remove(model.getElementAt(0));
            }
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            final Component component = renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            final String separator = separators.get(value);
            if (separator != null) {
                JPanel panel = new JPanel(new BorderLayout());
                panel.add(component, BorderLayout.CENTER);
                final SeparatorWithText sep = new SeparatorWithText() {

                    @Override
                    protected void paintComponent(Graphics g) {
                        g.setColor(new JBColor(Color.WHITE, UIUtil.getSeparatorColor()));
                        g.fillRect(0, 0, getWidth(), getHeight());
                        super.paintComponent(g);
                    }
                };
                sep.setCaption(separator);
                panel.add(sep, BorderLayout.NORTH);
                return panel;
            }
            return component;
        }
    });
    popup.setMinimumSize(new Dimension(200, -1));
    for (Object item : elements) {
        final int mnemonic = getMnemonic(item, itemsMap);
        if (mnemonic != -1) {
            final Action action = createNumberAction(mnemonic, popup, itemsMap, processor);
            popup.registerAction(mnemonic + "Action", KeyStroke.getKeyStroke(String.valueOf(mnemonic)), action);
            popup.registerAction(mnemonic + "Action", KeyStroke.getKeyStroke("NUMPAD" + String.valueOf(mnemonic)), action);
            hasMnemonic.set(true);
        }
    }
    return popup;
}
Also used : PopupListElementRenderer(com.intellij.ui.popup.list.PopupListElementRenderer) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) PopupStep(com.intellij.openapi.ui.popup.PopupStep) DefaultPsiElementCellRenderer(com.intellij.ide.util.DefaultPsiElementCellRenderer) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) PsiFile(com.intellij.psi.PsiFile) JBColor(com.intellij.ui.JBColor) PsiElement(com.intellij.psi.PsiElement) JBColor(com.intellij.ui.JBColor) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) SeparatorWithText(com.intellij.ui.SeparatorWithText) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem)

Aggregations

PopupListElementRenderer (com.intellij.ui.popup.list.PopupListElementRenderer)4 BaseListPopupStep (com.intellij.openapi.ui.popup.util.BaseListPopupStep)3 ListPopupImpl (com.intellij.ui.popup.list.ListPopupImpl)3 DefaultPsiElementCellRenderer (com.intellij.ide.util.DefaultPsiElementCellRenderer)2 PsiElementListCellRenderer (com.intellij.ide.util.PsiElementListCellRenderer)1 FlexBuildConfiguration (com.intellij.lang.javascript.flex.projectStructure.model.FlexBuildConfiguration)1 FlexBuildConfigurationManager (com.intellij.lang.javascript.flex.projectStructure.model.FlexBuildConfigurationManager)1 GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)1 SimpleDataContext (com.intellij.openapi.actionSystem.impl.SimpleDataContext)1 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 PopupStep (com.intellij.openapi.ui.popup.PopupStep)1 PsiClass (com.intellij.psi.PsiClass)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 ColoredListCellRenderer (com.intellij.ui.ColoredListCellRenderer)1 JBColor (com.intellij.ui.JBColor)1 RowIcon (com.intellij.ui.RowIcon)1 SeparatorWithText (com.intellij.ui.SeparatorWithText)1 SimpleColoredText (com.intellij.ui.SimpleColoredText)1 SimpleTextAttributes (com.intellij.ui.SimpleTextAttributes)1