Search in sources :

Example 1 with AttributeFormat

use of org.jetbrains.android.dom.attrs.AttributeFormat in project android by JetBrains.

the class AndroidDomExtender method registerExtensions.

@Override
public void registerExtensions(@NotNull AndroidDomElement element, @NotNull final DomExtensionsRegistrar registrar) {
    final AndroidFacet facet = AndroidFacet.getInstance(element);
    if (facet == null) {
        return;
    }
    AttributeProcessingUtil.processAttributes(element, facet, true, (xmlName, attrDef, parentStyleableName) -> {
        Set<AttributeFormat> formats = attrDef.getFormats();
        Class valueClass = formats.size() == 1 ? getValueClass(formats.iterator().next()) : String.class;
        registrar.registerAttributeChildExtension(xmlName, GenericAttributeValue.class);
        return registrar.registerGenericAttributeValueChildExtension(xmlName, valueClass);
    });
    SubtagsProcessingUtil.processSubtags(facet, element, registrar::registerCollectionChildrenExtension);
}
Also used : AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 2 with AttributeFormat

use of org.jetbrains.android.dom.attrs.AttributeFormat in project android by JetBrains.

the class AndroidDomUtil method getResourceReferenceConverter.

@Nullable
public static ResourceReferenceConverter getResourceReferenceConverter(@NotNull AttributeDefinition attr) {
    boolean containsReference = false;
    boolean containsNotReference = false;
    Set<ResourceType> resourceTypes = EnumSet.noneOf(ResourceType.class);
    Set<AttributeFormat> formats = attr.getFormats();
    for (AttributeFormat format : formats) {
        if (format == AttributeFormat.Reference) {
            containsReference = true;
        } else {
            containsNotReference = true;
        }
        ResourceType type = getResourceType(format);
        if (type != null) {
            resourceTypes.add(type);
        }
    }
    ResourceType specialResourceType = getSpecialResourceType(attr.getName());
    if (specialResourceType != null) {
        resourceTypes.add(specialResourceType);
    }
    if (containsReference) {
        if (resourceTypes.contains(ResourceType.COLOR)) {
            resourceTypes.add(ResourceType.DRAWABLE);
        }
        if (resourceTypes.contains(ResourceType.DRAWABLE)) {
            resourceTypes.add(ResourceType.MIPMAP);
        }
        if (resourceTypes.size() == 0) {
            resourceTypes.addAll(AndroidResourceUtil.REFERRABLE_RESOURCE_TYPES);
        }
    }
    if (resourceTypes.size() > 0) {
        final ResourceReferenceConverter converter = new ResourceReferenceConverter(resourceTypes, attr);
        converter.setAllowLiterals(containsNotReference);
        return converter;
    }
    return null;
}
Also used : AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) ResourceType(com.android.resources.ResourceType) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with AttributeFormat

use of org.jetbrains.android.dom.attrs.AttributeFormat in project android by JetBrains.

the class RenderErrorContributor method reportTagResourceFormat.

private void reportTagResourceFormat(@NotNull RenderResult result, @NotNull RenderProblem message) {
    Object clientData = message.getClientData();
    if (!(clientData instanceof String[])) {
        return;
    }
    String[] strings = (String[]) clientData;
    if (strings.length != 2) {
        return;
    }
    RenderTask renderTask = result.getRenderTask();
    if (renderTask == null) {
        return;
    }
    IAndroidTarget target = renderTask.getConfiguration().getRealTarget();
    if (target == null) {
        return;
    }
    AndroidPlatform platform = renderTask.getPlatform();
    if (platform == null) {
        return;
    }
    AndroidTargetData targetData = platform.getSdkData().getTargetData(target);
    AttributeDefinitions definitionLookup = targetData.getPublicAttrDefs(result.getFile().getProject());
    final String attributeName = strings[0];
    final String currentValue = strings[1];
    if (definitionLookup == null) {
        return;
    }
    AttributeDefinition definition = definitionLookup.getAttrDefByName(attributeName);
    if (definition == null) {
        return;
    }
    Set<AttributeFormat> formats = definition.getFormats();
    if (formats.contains(AttributeFormat.Flag) || formats.contains(AttributeFormat.Enum)) {
        String[] values = definition.getValues();
        if (values.length > 0) {
            HtmlBuilder builder = new HtmlBuilder();
            builder.add("Change ").add(currentValue).add(" to: ");
            boolean first = true;
            for (String value : values) {
                if (first) {
                    first = false;
                } else {
                    builder.add(", ");
                }
                builder.addLink(value, myLinkManager.createReplaceAttributeValueUrl(attributeName, currentValue, value));
            }
            addRefreshAction(builder);
            addIssue().setSummary("Incorrect resource value format").setHtmlContent(builder).build();
        }
    }
}
Also used : HtmlBuilder(com.android.utils.HtmlBuilder) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) IAndroidTarget(com.android.sdklib.IAndroidTarget) AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) AndroidTargetData(org.jetbrains.android.sdk.AndroidTargetData)

Example 4 with AttributeFormat

use of org.jetbrains.android.dom.attrs.AttributeFormat in project android by JetBrains.

the class ResolutionUtils method getAttrType.

@Nullable
public static /*if we can't work out the type, e.g a 'reference' with a '@null' value or enum*/
ResourceType getAttrType(@NotNull ItemResourceValue item, @NotNull Configuration configuration) {
    ResourceResolver resolver = configuration.getResourceResolver();
    assert resolver != null;
    ResourceValue resolvedValue = resolver.resolveResValue(item);
    ResourceType attrType = resolvedValue.getResourceType();
    if (attrType != null) {
        return attrType;
    } else {
        AttributeDefinition def = getAttributeDefinition(configuration, item);
        if (def != null) {
            for (AttributeFormat attrFormat : def.getFormats()) {
                attrType = AndroidDomUtil.getResourceType(attrFormat);
                if (attrType != null) {
                    return attrType;
                }
            }
        }
    }
    // sometimes we won't find the type of the attr, this means it's either a reference that points to @null, or a enum
    return null;
}
Also used : AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) ResourceResolver(com.android.ide.common.resources.ResourceResolver) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) ResourceType(com.android.resources.ResourceType) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with AttributeFormat

use of org.jetbrains.android.dom.attrs.AttributeFormat in project android by JetBrains.

the class NlPropertyEditors method getEditorType.

@NotNull
private static EditorType getEditorType(@NotNull NlProperty property) {
    AttributeDefinition definition = property.getDefinition();
    Set<AttributeFormat> formats = definition != null ? definition.getFormats() : Collections.emptySet();
    Boolean isBoolean = null;
    for (AttributeFormat format : formats) {
        switch(format) {
            case Boolean:
                if (isBoolean == null) {
                    isBoolean = Boolean.TRUE;
                }
                break;
            case String:
            case Color:
            case Dimension:
            case Integer:
            case Float:
            case Fraction:
                if (isBoolean == null) {
                    isBoolean = Boolean.FALSE;
                }
                break;
            case Enum:
                return EditorType.COMBO;
            case Flag:
                return EditorType.FLAG;
            default:
                break;
        }
    }
    // Do not inline this method. Other classes should not know about EnumSupportFactory.
    if (isBoolean == Boolean.TRUE) {
        return EditorType.BOOLEAN;
    } else if (EnumSupportFactory.supportsProperty(property)) {
        if (property.getName().equals(ATTR_STYLE)) {
            return EditorType.COMBO_WITH_BROWSE;
        }
        return EditorType.COMBO;
    }
    return EditorType.DEFAULT;
}
Also used : AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AttributeFormat (org.jetbrains.android.dom.attrs.AttributeFormat)11 AttributeDefinition (org.jetbrains.android.dom.attrs.AttributeDefinition)6 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 ResourceType (com.android.resources.ResourceType)3 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)2 ViewGroup (android.view.ViewGroup)1 LinearLayout (android.widget.LinearLayout)1 SdkConstants (com.android.SdkConstants)1 ATTR_LAYOUT_RESOURCE_PREFIX (com.android.SdkConstants.ATTR_LAYOUT_RESOURCE_PREFIX)1 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)1 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)1 ResourceResolver (com.android.ide.common.resources.ResourceResolver)1 ID (com.android.resources.ResourceType.ID)1 IAndroidTarget (com.android.sdklib.IAndroidTarget)1 Configuration (com.android.tools.idea.configurations.Configuration)1 ResolutionUtils (com.android.tools.idea.editors.theme.ResolutionUtils)1 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1 ResourceHelper (com.android.tools.idea.res.ResourceHelper)1 HtmlBuilder (com.android.utils.HtmlBuilder)1