Search in sources :

Example 1 with DomElementProblemDescriptor

use of com.intellij.util.xml.highlighting.DomElementProblemDescriptor in project intellij-community by JetBrains.

the class EditorTextFieldControl method updateComponent.

@Override
protected void updateComponent() {
    final DomElement domElement = getDomElement();
    if (domElement == null || !domElement.isValid())
        return;
    final EditorTextField textField = getEditorTextField(getComponent());
    final Project project = getProject();
    ApplicationManager.getApplication().invokeLater(() -> {
        if (!project.isOpen())
            return;
        if (!getDomWrapper().isValid())
            return;
        final DomElement domElement1 = getDomElement();
        if (domElement1 == null || !domElement1.isValid())
            return;
        final DomElementAnnotationsManager manager = DomElementAnnotationsManager.getInstance(project);
        final DomElementsProblemsHolder holder = manager.getCachedProblemHolder(domElement1);
        final List<DomElementProblemDescriptor> errorProblems = holder.getProblems(domElement1);
        final List<DomElementProblemDescriptor> warningProblems = new ArrayList<>(holder.getProblems(domElement1, true, HighlightSeverity.WARNING));
        warningProblems.removeAll(errorProblems);
        Color background = getDefaultBackground();
        if (errorProblems.size() > 0 && textField.getText().trim().length() == 0) {
            background = getErrorBackground();
        } else if (warningProblems.size() > 0) {
            background = getWarningBackground();
        }
        final Editor editor = textField.getEditor();
        if (editor != null) {
            final MarkupModel markupModel = editor.getMarkupModel();
            markupModel.removeAllHighlighters();
            if (!errorProblems.isEmpty() && editor.getDocument().getLineCount() > 0) {
                final TextAttributes attributes = SimpleTextAttributes.ERROR_ATTRIBUTES.toTextAttributes();
                attributes.setEffectType(EffectType.WAVE_UNDERSCORE);
                attributes.setEffectColor(attributes.getForegroundColor());
                markupModel.addLineHighlighter(0, 0, attributes);
                editor.getContentComponent().setToolTipText(errorProblems.get(0).getDescriptionTemplate());
            }
        }
        textField.setBackground(background);
    });
}
Also used : Project(com.intellij.openapi.project.Project) DomElementsProblemsHolder(com.intellij.util.xml.highlighting.DomElementsProblemsHolder) DomElement(com.intellij.util.xml.DomElement) EditorTextField(com.intellij.ui.EditorTextField) DomElementProblemDescriptor(com.intellij.util.xml.highlighting.DomElementProblemDescriptor) ArrayList(java.util.ArrayList) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Editor(com.intellij.openapi.editor.Editor) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) DomElementAnnotationsManager(com.intellij.util.xml.highlighting.DomElementAnnotationsManager)

Example 2 with DomElementProblemDescriptor

use of com.intellij.util.xml.highlighting.DomElementProblemDescriptor in project intellij-community by JetBrains.

the class DomCollectionControl method validate.

private void validate() {
    DomElement domElement = getDomElement();
    final List<DomElementProblemDescriptor> list = DomElementAnnotationsManager.getInstance(getProject()).getCachedProblemHolder(domElement).getProblems(domElement);
    final List<String> messages = new ArrayList<>();
    for (final DomElementProblemDescriptor descriptor : list) {
        if (descriptor instanceof DomCollectionProblemDescriptor && myChildDescription.equals(((DomCollectionProblemDescriptor) descriptor).getChildDescription())) {
            messages.add(descriptor.getDescriptionTemplate());
        }
    }
    myCollectionPanel.setErrorMessages(ArrayUtil.toStringArray(messages));
    myCollectionPanel.repaint();
}
Also used : DomElement(com.intellij.util.xml.DomElement) DomCollectionProblemDescriptor(com.intellij.util.xml.highlighting.DomCollectionProblemDescriptor) DomElementProblemDescriptor(com.intellij.util.xml.highlighting.DomElementProblemDescriptor) ArrayList(java.util.ArrayList)

Example 3 with DomElementProblemDescriptor

use of com.intellij.util.xml.highlighting.DomElementProblemDescriptor in project intellij-community by JetBrains.

the class ErrorableTableCellRenderer method getTableCellRendererComponent.

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    final Component component = myRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    if (!myRoot.isValid()) {
        return component;
    }
    final DomElementAnnotationsManager annotationsManager = DomElementAnnotationsManager.getInstance(myRowDomElement.getManager().getProject());
    final DomElementsProblemsHolder holder = annotationsManager.getCachedProblemHolder(myRoot);
    final List<DomElementProblemDescriptor> errorProblems = holder.getProblems(myCellValueDomElement);
    final List<DomElementProblemDescriptor> warningProblems = new ArrayList<>(holder.getProblems(myCellValueDomElement, true, HighlightSeverity.WARNING));
    warningProblems.removeAll(errorProblems);
    final boolean hasErrors = errorProblems.size() > 0;
    if (hasErrors) {
        component.setForeground(JBColor.RED);
        if (component instanceof JComponent) {
            ((JComponent) component).setToolTipText(TooltipUtils.getTooltipText(errorProblems));
        }
    } else {
        component.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
        if (component instanceof JComponent) {
            ((JComponent) component).setToolTipText(null);
        }
    }
    // highlight empty cell with errors
    if (hasErrors && (value == null || value.toString().trim().length() == 0)) {
        component.setBackground(BaseControl.ERROR_BACKGROUND);
    } else if (warningProblems.size() > 0) {
        component.setBackground(BaseControl.WARNING_BACKGROUND);
        if (isSelected)
            component.setForeground(JBColor.foreground());
    }
    final List<DomElementProblemDescriptor> errorDescriptors = annotationsManager.getCachedProblemHolder(myRowDomElement).getProblems(myRowDomElement, true, true);
    if (table.getModel().getColumnCount() - 1 == column) {
        if (errorDescriptors.size() > 0) {
            final JPanel wrapper = new JPanel(new BorderLayout());
            wrapper.add(component, BorderLayout.CENTER);
            wrapper.setBackground(component.getBackground());
            final JLabel errorLabel = new JLabel(getErrorIcon());
            wrapper.setToolTipText(TooltipUtils.getTooltipText(errorDescriptors));
            wrapper.add(errorLabel, BorderLayout.EAST);
            if (component instanceof JComponent) {
                final JComponent jComponent = (JComponent) component;
                wrapper.setBorder(jComponent.getBorder());
                jComponent.setBorder(BorderFactory.createEmptyBorder());
                jComponent.setToolTipText(TooltipUtils.getTooltipText(errorDescriptors));
            }
            return wrapper;
        } else {
            if (component instanceof JComponent) {
                ((JComponent) component).setToolTipText(null);
            }
        }
    }
    return component;
}
Also used : DomElementsProblemsHolder(com.intellij.util.xml.highlighting.DomElementsProblemsHolder) DomElementProblemDescriptor(com.intellij.util.xml.highlighting.DomElementProblemDescriptor) ArrayList(java.util.ArrayList) DomElementAnnotationsManager(com.intellij.util.xml.highlighting.DomElementAnnotationsManager)

Example 4 with DomElementProblemDescriptor

use of com.intellij.util.xml.highlighting.DomElementProblemDescriptor in project intellij-community by JetBrains.

the class ExtendsClassChecker method checkExtendClass.

@NotNull
public static List<DomElementProblemDescriptor> checkExtendClass(final GenericDomValue element, final PsiClass value, final String name, final boolean instantiatable, final boolean canBeDecorator, final boolean allowInterface, final boolean allowNonPublic, final boolean allowAbstract, final boolean allowEnum, final DomElementAnnotationHolder holder) {
    final Project project = element.getManager().getProject();
    PsiClass extendClass = JavaPsiFacade.getInstance(project).findClass(name, GlobalSearchScope.allScope(project));
    final SmartList<DomElementProblemDescriptor> list = new SmartList<>();
    if (extendClass != null) {
        if (!name.equals(value.getQualifiedName()) && !value.isInheritor(extendClass, true)) {
            String message = DomBundle.message("class.is.not.a.subclass", value.getQualifiedName(), extendClass.getQualifiedName());
            list.add(holder.createProblem(element, message));
        }
    }
    if (instantiatable) {
        if (value.hasModifierProperty(PsiModifier.ABSTRACT)) {
            list.add(holder.createProblem(element, DomBundle.message("class.is.not.concrete", value.getQualifiedName())));
        } else if (!allowNonPublic && !value.hasModifierProperty(PsiModifier.PUBLIC)) {
            list.add(holder.createProblem(element, DomBundle.message("class.is.not.public", value.getQualifiedName())));
        } else if (!PsiUtil.hasDefaultConstructor(value, true)) {
            if (canBeDecorator) {
                boolean hasConstructor = false;
                for (PsiMethod method : value.getConstructors()) {
                    final PsiParameterList psiParameterList = method.getParameterList();
                    if (psiParameterList.getParametersCount() != 1)
                        continue;
                    PsiTypeElement typeElement = psiParameterList.getParameters()[0].getTypeElement();
                    if (typeElement != null) {
                        final PsiType psiType = typeElement.getType();
                        if (psiType instanceof PsiClassType) {
                            final PsiClass psiClass = ((PsiClassType) psiType).resolve();
                            if (psiClass != null && InheritanceUtil.isInheritorOrSelf(psiClass, extendClass, true)) {
                                hasConstructor = true;
                                break;
                            }
                        }
                    }
                }
                if (!hasConstructor) {
                    list.add(holder.createProblem(element, DomBundle.message("class.decorator.or.has.default.constructor", value.getQualifiedName())));
                }
            } else {
                list.add(holder.createProblem(element, DomBundle.message("class.has.no.default.constructor", value.getQualifiedName())));
            }
        }
    }
    if (!allowInterface && value.isInterface()) {
        list.add(holder.createProblem(element, DomBundle.message("interface.not.allowed", value.getQualifiedName())));
    }
    if (!allowEnum && value.isEnum()) {
        list.add(holder.createProblem(element, DomBundle.message("enum.not.allowed", value.getQualifiedName())));
    }
    if (!allowAbstract && value.hasModifierProperty(PsiModifier.ABSTRACT) && !value.isInterface()) {
        list.add(holder.createProblem(element, DomBundle.message("abstract.class.not.allowed", value.getQualifiedName())));
    }
    return list;
}
Also used : Project(com.intellij.openapi.project.Project) DomElementProblemDescriptor(com.intellij.util.xml.highlighting.DomElementProblemDescriptor) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with DomElementProblemDescriptor

use of com.intellij.util.xml.highlighting.DomElementProblemDescriptor in project intellij-community by JetBrains.

the class ExtendsClassChecker method checkExtendsClassInReferences.

public static List<DomElementProblemDescriptor> checkExtendsClassInReferences(final GenericDomValue element, final DomElementAnnotationHolder holder) {
    if (!isPsiClassType(element)) {
        return Collections.emptyList();
    }
    final Object valueObject = element.getValue();
    if (!(valueObject instanceof PsiClass))
        return Collections.emptyList();
    final XmlElement valueElement = DomUtil.getValueElement(element);
    if (valueElement == null)
        return Collections.emptyList();
    final PsiReference[] references = ourProvider.getReferencesByElement(valueElement, new ProcessingContext());
    for (PsiReference reference : references) {
        if (reference instanceof JavaClassReference) {
            final PsiReferenceProvider psiReferenceProvider = ((JavaClassReference) reference).getProvider();
            final String[] value = psiReferenceProvider instanceof JavaClassReferenceProvider ? JavaClassReferenceProvider.EXTEND_CLASS_NAMES.getValue(((JavaClassReferenceProvider) psiReferenceProvider).getOptions()) : null;
            if (value != null && value.length != 0) {
                for (String className : value) {
                    final List<DomElementProblemDescriptor> problemDescriptors = checkExtendClass(element, ((PsiClass) valueObject), className, false, false, true, false, true, true, holder);
                    if (!problemDescriptors.isEmpty()) {
                        return problemDescriptors;
                    }
                }
            }
        }
    }
    return Collections.emptyList();
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) JavaClassReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference) JavaClassReferenceProvider(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceProvider) DomElementProblemDescriptor(com.intellij.util.xml.highlighting.DomElementProblemDescriptor) XmlElement(com.intellij.psi.xml.XmlElement)

Aggregations

DomElementProblemDescriptor (com.intellij.util.xml.highlighting.DomElementProblemDescriptor)9 DomElementsProblemsHolder (com.intellij.util.xml.highlighting.DomElementsProblemsHolder)6 DomElementAnnotationsManager (com.intellij.util.xml.highlighting.DomElementAnnotationsManager)5 Project (com.intellij.openapi.project.Project)4 DomElement (com.intellij.util.xml.DomElement)4 ArrayList (java.util.ArrayList)3 Editor (com.intellij.openapi.editor.Editor)1 MarkupModel (com.intellij.openapi.editor.markup.MarkupModel)1 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 Pair (com.intellij.openapi.util.Pair)1 JavaClassReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference)1 JavaClassReferenceProvider (com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceProvider)1 XmlElement (com.intellij.psi.xml.XmlElement)1 XmlTag (com.intellij.psi.xml.XmlTag)1 EditorTextField (com.intellij.ui.EditorTextField)1 JBColor (com.intellij.ui.JBColor)1 SimpleTextAttributes (com.intellij.ui.SimpleTextAttributes)1 ProcessingContext (com.intellij.util.ProcessingContext)1 SmartList (com.intellij.util.SmartList)1 DomCollectionProblemDescriptor (com.intellij.util.xml.highlighting.DomCollectionProblemDescriptor)1