Search in sources :

Example 1 with JavaFxPropertyTagDescriptor

use of org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor in project intellij-community by JetBrains.

the class JavaFxPsiUtil method isClassAcceptable.

public static boolean isClassAcceptable(@Nullable XmlTag targetTag, @Nullable final PsiClass fromClass, @NotNull BiConsumer<String, Validator.ValidationHost.ErrorType> messageConsumer) {
    if (targetTag == null || fromClass == null || !fromClass.isValid()) {
        return true;
    }
    final XmlElementDescriptor tagDescriptor = targetTag.getDescriptor();
    if (tagDescriptor instanceof JavaFxPropertyTagDescriptor) {
        final PsiClass containingClass = ((JavaFxPropertyTagDescriptor) tagDescriptor).getPsiClass();
        final PsiType targetType = getWritablePropertyType(containingClass, tagDescriptor.getDeclaration());
        return canCoerce(targetType, fromClass, targetTag, messageConsumer);
    } else if (tagDescriptor instanceof JavaFxClassTagDescriptorBase) {
        final PsiElement tagDeclaration = tagDescriptor.getDeclaration();
        if (tagDeclaration instanceof PsiClass) {
            PsiClass defaultPropertyOwnerClass = (PsiClass) tagDeclaration;
            final XmlAttribute factoryAttr = targetTag.getAttribute(FxmlConstants.FX_FACTORY);
            if (factoryAttr != null) {
                defaultPropertyOwnerClass = getFactoryProducedClass((PsiClass) tagDeclaration, factoryAttr.getValue());
            }
            final PsiType targetType = getDefaultPropertyExpectedType(defaultPropertyOwnerClass);
            if (targetType != null) {
                return canCoerce(targetType, fromClass, targetTag, messageConsumer);
            }
            if (!isObservableCollection(defaultPropertyOwnerClass)) {
                return noDefaultPropertyError(messageConsumer);
            }
        }
    }
    return true;
}
Also used : JavaFxClassTagDescriptorBase(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxClassTagDescriptorBase) JavaFxPropertyTagDescriptor(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor)

Example 2 with JavaFxPropertyTagDescriptor

use of org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor in project intellij-community by JetBrains.

the class JavaFxColorRgbInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
    if (!JavaFxFileTypeFactory.isFxml(session.getFile()))
        return PsiElementVisitor.EMPTY_VISITOR;
    return new XmlElementVisitor() {

        @Override
        public void visitXmlAttribute(XmlAttribute attribute) {
            super.visitXmlAttribute(attribute);
            final String attributeValue = attribute.getValue();
            if (attributeValue == null)
                return;
            final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
            if (descriptor instanceof JavaFxPropertyAttributeDescriptor) {
                final PsiClass psiClass = ((JavaFxPropertyAttributeDescriptor) descriptor).getPsiClass();
                if (psiClass != null && JavaFxCommonNames.JAVAFX_SCENE_COLOR.equals(psiClass.getQualifiedName())) {
                    final XmlAttributeValue valueElement = attribute.getValueElement();
                    final PsiElement location = valueElement != null ? valueElement : attribute;
                    validateColorComponent(psiClass, attribute.getName(), attributeValue, location);
                }
            }
        }

        @Override
        public void visitXmlTag(XmlTag tag) {
            super.visitXmlTag(tag);
            if (tag.getSubTags().length != 0)
                return;
            final XmlElementDescriptor descriptor = tag.getDescriptor();
            if (descriptor instanceof JavaFxPropertyTagDescriptor) {
                final PsiClass psiClass = ((JavaFxPropertyTagDescriptor) descriptor).getPsiClass();
                if (psiClass != null && JavaFxCommonNames.JAVAFX_SCENE_COLOR.equals(psiClass.getQualifiedName())) {
                    final XmlTagValue valueElement = tag.getValue();
                    final XmlText[] textElements = valueElement.getTextElements();
                    final PsiElement location = textElements.length == 1 ? textElements[0] : tag;
                    validateColorComponent(psiClass, tag.getName(), valueElement.getTrimmedText(), location);
                }
            }
        }

        private void validateColorComponent(@NotNull PsiClass psiClass, @NotNull String propertyName, @NotNull String propertyValue, @NotNull PsiElement location) {
            final PsiMember declaration = JavaFxPsiUtil.getWritableProperties(psiClass).get(propertyName);
            final String boxedQName = JavaFxPsiUtil.getBoxedPropertyType(psiClass, declaration);
            if (CommonClassNames.JAVA_LANG_FLOAT.equals(boxedQName) || CommonClassNames.JAVA_LANG_DOUBLE.equals(boxedQName)) {
                try {
                    double value = Double.parseDouble(propertyValue);
                    if (value < 0.0 || value > 1.0) {
                        holder.registerProblem(location, "Color component has to be a number between 0.0 and 1.0, inclusively");
                    }
                } catch (NumberFormatException ignored) {
                }
            }
        }
    };
}
Also used : NotNull(org.jetbrains.annotations.NotNull) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) JavaFxPropertyTagDescriptor(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) JavaFxPropertyAttributeDescriptor(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyAttributeDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with JavaFxPropertyTagDescriptor

use of org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor in project intellij-community by JetBrains.

the class JavaFxDefaultTagInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
    if (!JavaFxFileTypeFactory.isFxml(session.getFile()))
        return PsiElementVisitor.EMPTY_VISITOR;
    return new XmlElementVisitor() {

        @Override
        public void visitXmlTag(XmlTag tag) {
            super.visitXmlTag(tag);
            final XmlElementDescriptor descriptor = tag.getDescriptor();
            if (descriptor instanceof JavaFxPropertyTagDescriptor) {
                final PsiClass parentTagClass = JavaFxPsiUtil.getTagClass(tag.getParentTag());
                if (parentTagClass != null) {
                    final String propertyName = JavaFxPsiUtil.getDefaultPropertyName(parentTagClass);
                    final String tagName = tag.getName();
                    if (Comparing.strEqual(tagName, propertyName) && !isCollectionAssignment(parentTagClass, propertyName, tag)) {
                        final TextRange startTagRange = XmlTagUtil.getStartTagRange(tag);
                        final TextRange rangeInElement = startTagRange != null ? startTagRange.shiftRight(-tag.getTextOffset()) : null;
                        holder.registerProblem(tag, rangeInElement, "Default property tag could be removed", new UnwrapTagFix(tagName));
                    }
                }
            }
        }
    };
}
Also used : JavaFxPropertyTagDescriptor(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor) TextRange(com.intellij.openapi.util.TextRange) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with JavaFxPropertyTagDescriptor

use of org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor in project intellij-community by JetBrains.

the class JavaFxRedundantPropertyValueInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
    if (!JavaFxFileTypeFactory.isFxml(session.getFile()))
        return PsiElementVisitor.EMPTY_VISITOR;
    return new XmlElementVisitor() {

        @Override
        public void visitXmlAttribute(XmlAttribute attribute) {
            super.visitXmlAttribute(attribute);
            final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
            if (!(descriptor instanceof JavaFxPropertyAttributeDescriptor))
                return;
            final String attributeName = attribute.getName();
            final String attributeValue = attribute.getValue();
            if (attributeValue == null || attributeValue.startsWith("$") || attributeValue.startsWith("#") || attributeValue.startsWith("%") || FxmlConstants.FX_ID.equals(attributeName) || FxmlConstants.FX_VALUE.equals(attributeName) || FxmlConstants.FX_CONSTANT.equals(attributeName) || FxmlConstants.FX_CONTROLLER.equals(attributeName)) {
                return;
            }
            final PsiClass tagClass = JavaFxPsiUtil.getTagClass(attribute.getParent());
            final String defaultValue = getDefaultValue(attributeName, tagClass);
            if (defaultValue == null)
                return;
            if (isEqualValue(tagClass, attributeValue, defaultValue, descriptor.getDeclaration())) {
                holder.registerProblem(attribute, "Attribute is redundant because it contains default value", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new RemoveAttributeIntentionFix(attributeName, attribute));
            }
        }

        @Override
        public void visitXmlTag(XmlTag tag) {
            super.visitXmlTag(tag);
            final XmlElementDescriptor descriptor = tag.getDescriptor();
            if (!(descriptor instanceof JavaFxPropertyTagDescriptor)) {
                return;
            }
            if (tag.getSubTags().length != 0)
                return;
            final String tagText = tag.getValue().getTrimmedText();
            if (tagText.startsWith("$") || tagText.startsWith("#") || tagText.startsWith("%")) {
                return;
            }
            final XmlTag parentTag = tag.getParentTag();
            if (parentTag == null)
                return;
            final PsiClass tagClass = JavaFxPsiUtil.getTagClass(parentTag);
            final String defaultValue = getDefaultValue(tag.getName(), tagClass);
            if (defaultValue == null)
                return;
            if (isEqualValue(tagClass, tagText, defaultValue, descriptor.getDeclaration())) {
                holder.registerProblem(tag, "Tag is redundant because it contains default value", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new RemoveTagIntentionFix(tag.getName(), tag));
            }
        }
    };
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) JavaFxPropertyTagDescriptor(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor) RemoveAttributeIntentionFix(com.intellij.codeInsight.daemon.impl.analysis.RemoveAttributeIntentionFix) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) JavaFxPropertyAttributeDescriptor(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyAttributeDescriptor) RemoveTagIntentionFix(com.intellij.codeInsight.daemon.impl.analysis.RemoveTagIntentionFix) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with JavaFxPropertyTagDescriptor

use of org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor in project intellij-community by JetBrains.

the class JavaFxCollapseSubTagToAttributeIntention method isAvailable.

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
    if (element instanceof XmlToken && ((XmlToken) element).getTokenType() == XmlTokenType.XML_NAME && element.getParent() instanceof XmlTag) {
        final XmlTag tag = (XmlTag) element.getParent();
        for (XmlTag xmlTag : tag.getSubTags()) {
            if (xmlTag.getAttribute(FxmlConstants.FX_VALUE) == null)
                return false;
        }
        final XmlTag parentTag = tag.getParentTag();
        if (parentTag != null && tag.getDescriptor() instanceof JavaFxPropertyTagDescriptor && parentTag.getDescriptor() instanceof JavaFxClassTagDescriptorBase) {
            setText("Collapse tag '" + tag.getName() + "' to attribute");
            return true;
        }
    }
    return false;
}
Also used : JavaFxClassTagDescriptorBase(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxClassTagDescriptorBase) JavaFxPropertyTagDescriptor(org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor) XmlToken(com.intellij.psi.xml.XmlToken) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

JavaFxPropertyTagDescriptor (org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyTagDescriptor)5 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)4 XmlTag (com.intellij.psi.xml.XmlTag)3 NotNull (org.jetbrains.annotations.NotNull)3 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)2 JavaFxClassTagDescriptorBase (org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxClassTagDescriptorBase)2 JavaFxPropertyAttributeDescriptor (org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyAttributeDescriptor)2 RemoveAttributeIntentionFix (com.intellij.codeInsight.daemon.impl.analysis.RemoveAttributeIntentionFix)1 RemoveTagIntentionFix (com.intellij.codeInsight.daemon.impl.analysis.RemoveTagIntentionFix)1 TextRange (com.intellij.openapi.util.TextRange)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlToken (com.intellij.psi.xml.XmlToken)1