Search in sources :

Example 11 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem 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)

Example 12 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project intellij-community by JetBrains.

the class GotoTestRelatedProvider method getItems.

@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull DataContext context) {
    final PsiFile file = CommonDataKeys.PSI_FILE.getData(context);
    if (file == null)
        return Collections.emptyList();
    Collection<PsiElement> result;
    final boolean isTest = TestFinderHelper.isTest(file);
    if (isTest) {
        result = TestFinderHelper.findClassesForTest(file);
    } else {
        result = TestFinderHelper.findTestsForClass(file);
    }
    if (!result.isEmpty()) {
        final List<GotoRelatedItem> items = new ArrayList<>();
        for (PsiElement element : result) {
            items.add(new GotoRelatedItem(element, isTest ? "Tests" : "Testee classes"));
        }
        return items;
    }
    return Collections.emptyList();
}
Also used : ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project intellij-community by JetBrains.

the class GotoPropertyDeclarationsProvider method getItems.

@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull DataContext context) {
    final FileEditor editor = PlatformDataKeys.FILE_EDITOR.getData(context);
    if (!(editor instanceof ResourceBundleEditor)) {
        return Collections.emptyList();
    }
    final ResourceBundleEditor resourceBundleEditor = (ResourceBundleEditor) editor;
    final Collection<ResourceBundleEditorViewElement> elements = resourceBundleEditor.getSelectedElements();
    if (elements.size() != 1) {
        return Collections.emptyList();
    }
    final IProperty[] properties = ContainerUtil.getFirstItem(elements).getProperties();
    if (properties == null || properties.length != 1 || !(properties[0] instanceof Property)) {
        return Collections.emptyList();
    }
    final IProperty property = properties[0];
    final String propertyKey = property.getKey();
    final PropertiesFile file = PropertiesImplUtil.getPropertiesFile(property.getPsiElement().getContainingFile());
    assert file != null;
    final ResourceBundle resourceBundle = file.getResourceBundle();
    return ContainerUtil.mapNotNull(resourceBundle.getPropertiesFiles(), (NullableFunction<PropertiesFile, GotoRelatedItem>) f -> {
        final IProperty foundProperty = f.findPropertyByKey(propertyKey);
        return foundProperty == null ? null : new GotoRelatedItem(foundProperty.getPsiElement(), "Property Declarations");
    });
}
Also used : Property(com.intellij.lang.properties.psi.Property) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) DataContext(com.intellij.openapi.actionSystem.DataContext) Collection(java.util.Collection) NullableFunction(com.intellij.util.NullableFunction) ContainerUtil(com.intellij.util.containers.ContainerUtil) FileEditor(com.intellij.openapi.fileEditor.FileEditor) GotoRelatedProvider(com.intellij.navigation.GotoRelatedProvider) List(java.util.List) IProperty(com.intellij.lang.properties.IProperty) PlatformDataKeys(com.intellij.openapi.actionSystem.PlatformDataKeys) PropertiesImplUtil(com.intellij.lang.properties.PropertiesImplUtil) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) ResourceBundle(com.intellij.lang.properties.ResourceBundle) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) FileEditor(com.intellij.openapi.fileEditor.FileEditor) IProperty(com.intellij.lang.properties.IProperty) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) ResourceBundle(com.intellij.lang.properties.ResourceBundle) Property(com.intellij.lang.properties.psi.Property) IProperty(com.intellij.lang.properties.IProperty) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project intellij-community by JetBrains.

the class GotoResourceBundleLocalizationsProvider method getItems.

@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull final DataContext context) {
    final FileEditor editor = PlatformDataKeys.FILE_EDITOR.getData(context);
    if (editor instanceof ResourceBundleEditor) {
        return Collections.emptyList();
    }
    final PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(context);
    if (psiFile == null || !(psiFile instanceof PropertiesFile)) {
        return Collections.emptyList();
    }
    final ResourceBundle resourceBundle = ((PropertiesFile) psiFile).getResourceBundle();
    final List<PropertiesFile> bundlePropertiesFiles = resourceBundle.getPropertiesFiles();
    assert bundlePropertiesFiles.size() != 0;
    if (bundlePropertiesFiles.size() != 1) {
        final ArrayList<PropertiesFile> propertiesFilesWithoutCurrent = ContainerUtil.newArrayList(bundlePropertiesFiles);
        propertiesFilesWithoutCurrent.remove(psiFile);
        return ContainerUtil.map(propertiesFilesWithoutCurrent, propertiesFile -> new GotoRelatedItem((PsiElement) propertiesFile, "Other Localizations"));
    } else {
        return Collections.emptyList();
    }
}
Also used : FileEditor(com.intellij.openapi.fileEditor.FileEditor) PsiFile(com.intellij.psi.PsiFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) ResourceBundle(com.intellij.lang.properties.ResourceBundle) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with GotoRelatedItem

use of com.intellij.navigation.GotoRelatedItem in project intellij-community by JetBrains.

the class JavaFxRelatedItemLineMarkerProvider method collectNavigationMarkers.

@Override
protected void collectNavigationMarkers(@NotNull PsiElement element, final Collection<? super RelatedItemLineMarkerInfo> result) {
    if (element instanceof PsiField) {
        final PsiField field = (PsiField) element;
        if (JavaFxPsiUtil.isVisibleInFxml(field) && !field.hasModifierProperty(PsiModifier.STATIC) && !field.hasModifierProperty(PsiModifier.FINAL)) {
            final PsiClass containingClass = field.getContainingClass();
            if (containingClass != null && containingClass.hasModifierProperty(PsiModifier.PUBLIC) && containingClass.getQualifiedName() != null) {
                final PsiMethod[] constructors = containingClass.getConstructors();
                boolean defaultConstructor = constructors.length == 0;
                for (PsiMethod constructor : constructors) {
                    if (constructor.getParameterList().getParametersCount() == 0) {
                        defaultConstructor = true;
                        break;
                    }
                }
                if (!defaultConstructor)
                    return;
                final ArrayList<GotoRelatedItem> targets = new ArrayList<>();
                collectTargets(field, targets, element1 -> new GotoRelatedItem(element1), true);
                if (targets.isEmpty())
                    return;
                result.add(new RelatedItemLineMarkerInfo<>(field, field.getNameIdentifier().getTextRange(), AllIcons.FileTypes.Xml, Pass.LINE_MARKERS, null, new JavaFXIdIconNavigationHandler(), GutterIconRenderer.Alignment.LEFT, targets));
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem)

Aggregations

GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)21 PsiElement (com.intellij.psi.PsiElement)13 NotNull (org.jetbrains.annotations.NotNull)11 PsiFile (com.intellij.psi.PsiFile)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 ArrayList (java.util.ArrayList)3 ResourceBundle (com.intellij.lang.properties.ResourceBundle)2 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)2 GotoRelatedProvider (com.intellij.navigation.GotoRelatedProvider)2 DataContext (com.intellij.openapi.actionSystem.DataContext)2 FileEditor (com.intellij.openapi.fileEditor.FileEditor)2 Project (com.intellij.openapi.project.Project)2 Computable (com.intellij.openapi.util.Computable)2 PsiClass (com.intellij.psi.PsiClass)2 LineMarkerProvider (com.intellij.codeInsight.daemon.LineMarkerProvider)1 RelatedItemLineMarkerInfo (com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo)1 RelatedItemLineMarkerProvider (com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider)1 DomGotoRelatedItem (com.intellij.codeInsight.navigation.DomGotoRelatedItem)1 Location (com.intellij.execution.Location)1 DefaultPsiElementCellRenderer (com.intellij.ide.util.DefaultPsiElementCellRenderer)1