Search in sources :

Example 26 with AttributeDefinition

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

the class BrowsePanel method showResourceChooser.

public static ChooseResourceDialog showResourceChooser(@NotNull NlProperty property) {
    Module module = property.getModel().getModule();
    AttributeDefinition definition = property.getDefinition();
    EnumSet<ResourceType> types = getResourceTypes(property.getName(), definition);
    //return new ChooseResourceDialog(module, types, property.getValue(), property.getTag());
    return ChooseResourceDialog.builder().setModule(module).setTypes(types).setCurrentValue(property.getValue()).setTag(property.getTag()).build();
}
Also used : AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) ResourceType(com.android.resources.ResourceType) Module(com.intellij.openapi.module.Module)

Example 27 with AttributeDefinition

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

the class AttributeDefinitionEnumSupport method getAllValues.

@Override
@NotNull
public List<ValueWithDisplayString> getAllValues() {
    AttributeDefinition definition = myProperty.getDefinition();
    String[] stringValues = definition != null ? definition.getValues() : ArrayUtil.EMPTY_STRING_ARRAY;
    List<ValueWithDisplayString> values = new ArrayList<>(stringValues.length);
    for (String stringValue : stringValues) {
        values.add(new ValueWithDisplayString(stringValue, stringValue));
    }
    return values;
}
Also used : ArrayList(java.util.ArrayList) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with AttributeDefinition

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

the class NlReferenceEditor method configureSlider.

private boolean configureSlider() {
    if (myProperty == null) {
        return false;
    }
    AttributeDefinition definition = myProperty.getDefinition();
    if (definition == null || Collections.disjoint(definition.getFormats(), ImmutableList.of(AttributeFormat.Dimension, AttributeFormat.Float))) {
        return false;
    }
    int maximum;
    int value;
    switch(myProperty.getName()) {
        case SdkConstants.ATTR_ELEVATION:
        case SdkConstants.ATTR_CARD_ELEVATION:
            // Range: [0, 24] integer (dp)
            maximum = 24;
            value = getValueInDp(0);
            break;
        case SdkConstants.ATTR_MIN_HEIGHT:
            // Range: [0, 250] integer (dp)
            maximum = 250;
            value = getValueInDp(180);
            break;
        case SdkConstants.ATTR_MOCKUP_OPACITY:
        case SdkConstants.ATTR_COLLAPSE_PARALLAX_MULTIPLIER:
            // Range: [0.0, 1.0] float (no unit)
            maximum = 10;
            value = (int) (getValueAsFloat(1.0) * 10 + 0.5);
            break;
        default:
            return false;
    }
    mySlider.setMinimum(0);
    mySlider.setMaximum(maximum);
    mySlider.setValue(value);
    return true;
}
Also used : AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition)

Example 29 with AttributeDefinition

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

the class NlProperties method getPropertiesWithReadLock.

@NotNull
private Table<String, String, NlPropertyItem> getPropertiesWithReadLock(@NotNull AndroidFacet facet, @NotNull List<NlComponent> components, @NotNull GradleDependencyManager dependencyManager) {
    ResourceManager localResourceManager = facet.getLocalResourceManager();
    ResourceManager systemResourceManager = facet.getSystemResourceManager();
    if (systemResourceManager == null) {
        Logger.getInstance(NlProperties.class).error("No system resource manager for module: " + facet.getModule().getName());
        return ImmutableTable.of();
    }
    AttributeDefinitions localAttrDefs = localResourceManager.getAttributeDefinitions();
    AttributeDefinitions systemAttrDefs = systemResourceManager.getAttributeDefinitions();
    Table<String, String, NlPropertyItem> combinedProperties = null;
    for (NlComponent component : components) {
        XmlTag tag = component.getTag();
        if (!tag.isValid()) {
            return ImmutableTable.of();
        }
        XmlElementDescriptor elementDescriptor = myDescriptorProvider.getDescriptor(tag);
        if (elementDescriptor == null) {
            return ImmutableTable.of();
        }
        XmlAttributeDescriptor[] descriptors = elementDescriptor.getAttributesDescriptors(tag);
        Table<String, String, NlPropertyItem> properties = HashBasedTable.create(3, descriptors.length);
        for (XmlAttributeDescriptor desc : descriptors) {
            String namespace = getNamespace(desc, tag);
            AttributeDefinitions attrDefs = NS_RESOURCES.equals(namespace) ? systemAttrDefs : localAttrDefs;
            AttributeDefinition attrDef = attrDefs == null ? null : attrDefs.getAttrDefByName(desc.getName());
            NlPropertyItem property = NlPropertyItem.create(components, desc, namespace, attrDef);
            properties.put(StringUtil.notNullize(namespace), property.getName(), property);
        }
        // Exceptions:
        switch(tag.getName()) {
            case AUTO_COMPLETE_TEXT_VIEW:
                // An AutoCompleteTextView has a popup that is created at runtime.
                // Properties for this popup can be added to the AutoCompleteTextView tag.
                properties.put(ANDROID_URI, ATTR_POPUP_BACKGROUND, NlPropertyItem.create(components, new AndroidAnyAttributeDescriptor(ATTR_POPUP_BACKGROUND), ANDROID_URI, systemAttrDefs != null ? systemAttrDefs.getAttrDefByName(ATTR_POPUP_BACKGROUND) : null));
                break;
        }
        combinedProperties = combine(properties, combinedProperties);
    }
    // The following properties are deprecated in the support library and can be ignored by tools:
    assert combinedProperties != null;
    combinedProperties.remove(AUTO_URI, ATTR_PADDING_START);
    combinedProperties.remove(AUTO_URI, ATTR_PADDING_END);
    combinedProperties.remove(AUTO_URI, ATTR_THEME);
    setUpDesignProperties(combinedProperties);
    setUpSrcCompat(combinedProperties, facet, components, dependencyManager);
    initStarState(combinedProperties);
    //noinspection ConstantConditions
    return combinedProperties;
}
Also used : AndroidAnyAttributeDescriptor(org.jetbrains.android.dom.AndroidAnyAttributeDescriptor) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) NamespaceAwareXmlAttributeDescriptor(com.intellij.xml.NamespaceAwareXmlAttributeDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with AttributeDefinition

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

the class LayoutParamsManager method setAttribute.

/**
   * Sets the given attribute in the passed layoutParams instance. This method tries to infer the type to use from the attribute name and
   * the field type.
   * <p>
   * @return whether the method was able to set the attribute or not.
   */
public static boolean setAttribute(@NotNull Object layoutParams, @NotNull String attributeName, @Nullable String value, @NotNull NlModel model) {
    // Try to find the attribute definition and retrieve the defined formats
    AttributeDefinition attributeDefinition = ResolutionUtils.getAttributeDefinition(model.getModule(), model.getConfiguration(), ATTR_LAYOUT_RESOURCE_PREFIX + attributeName);
    EnumSet<AttributeFormat> inferredTypes = attributeDefinition != null ? EnumSet.copyOf(attributeDefinition.getFormats()) : EnumSet.noneOf(AttributeFormat.class);
    if (value != null && (value.startsWith(SdkConstants.PREFIX_RESOURCE_REF) || value.startsWith(SdkConstants.PREFIX_THEME_REF)) && model.getConfiguration().getResourceResolver() != null) {
        // This is a reference so we resolve the actual value and we try to infer the type from the given reference type
        ResourceValue resourceValue = model.getConfiguration().getResourceResolver().findResValue(value, false);
        if (resourceValue != null) {
            value = resourceValue.getValue();
            //noinspection EnumSwitchStatementWhichMissesCases
            switch(resourceValue.getResourceType()) {
                case INTEGER:
                case ID:
                case DIMEN:
                    inferredTypes.add(AttributeFormat.Integer);
                    break;
                case FRACTION:
                    inferredTypes.add(AttributeFormat.Float);
                    break;
            }
            if (resourceValue.getResourceType() == ID) {
                Integer resolvedId = AppResourceRepository.getAppResources(model.getFacet(), true).getResourceId(ID, resourceValue.getName());
                // TODO: Remove this wrapping/unwrapping
                value = resolvedId.toString();
            }
        }
    }
    // Now we have a value and an attributeName. We now try to map the given attributeName to the field in the LayoutParams that
    // stores its value.
    MappedField mappedField = mapField(layoutParams, attributeName);
    if (inferredTypes.isEmpty()) {
        // If we don't know the type yet, use the field type.
        inferredTypes.addAll(mappedField.type);
    }
    // 3. Lastly, if we do not have a better option, we try to infer the value from the default value in the class
    if (inferredTypes.isEmpty()) {
        inferredTypes.addAll(inferTypeFromValue(value));
    }
    if (inferredTypes.isEmpty()) {
        inferredTypes.addAll(inferTypeFromField(layoutParams, mappedField));
    }
    Object defaultValue = null;
    try {
        defaultValue = getDefaultValue(layoutParams, mappedField);
    } catch (NoSuchElementException ignore) {
    }
    if (defaultValue != null && inferredTypes.isEmpty()) {
        inferredTypes.addAll(attributeFormatFromType(defaultValue.getClass()));
    }
    if (value == null) {
        return setField(layoutParams, mappedField, defaultValue);
    } else {
        boolean fieldSet = false;
        for (AttributeFormat type : inferredTypes) {
            switch(type) {
                case Dimension:
                    fieldSet = setField(layoutParams, mappedField, getDimensionValue(value, model.getConfiguration()));
                    break;
                case Integer:
                    try {
                        fieldSet = setField(layoutParams, mappedField, Integer.parseInt(value));
                    } catch (NumberFormatException e) {
                        fieldSet = false;
                    }
                    break;
                case String:
                    fieldSet = setField(layoutParams, mappedField, value);
                    break;
                case Boolean:
                    fieldSet = setField(layoutParams, mappedField, Boolean.parseBoolean(value));
                    break;
                case Float:
                    try {
                        fieldSet = setField(layoutParams, mappedField, Float.parseFloat(value));
                    } catch (NumberFormatException e) {
                        fieldSet = false;
                    }
                    break;
                case Enum:
                    {
                        Integer intValue = attributeDefinition != null ? attributeDefinition.getValueMapping(value) : null;
                        if (intValue != null) {
                            fieldSet = setField(layoutParams, mappedField, intValue);
                        }
                    }
                    break;
                case Flag:
                    {
                        OptionalInt flagValue = Splitter.on('|').splitToList(value).stream().map(StringUtil::trim).mapToInt(flagName -> {
                            Integer intValue = attributeDefinition != null ? attributeDefinition.getValueMapping(flagName) : null;
                            return intValue != null ? intValue : 0;
                        }).reduce((a, b) -> a + b);
                        if (flagValue.isPresent()) {
                            fieldSet = setField(layoutParams, mappedField, flagValue.getAsInt());
                        }
                    }
                    break;
                default:
            }
            if (fieldSet) {
                return true;
            }
        }
        return false;
    }
}
Also used : AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) java.lang.reflect(java.lang.reflect) LinearLayout(android.widget.LinearLayout) ResourceHelper(com.android.tools.idea.res.ResourceHelper) java.util(java.util) SdkConstants(com.android.SdkConstants) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) StringUtil(com.intellij.openapi.util.text.StringUtil) Function(java.util.function.Function) ViewGroup(android.view.ViewGroup) TimeUnit(java.util.concurrent.TimeUnit) Nullable(org.jetbrains.annotations.Nullable) ID(com.android.resources.ResourceType.ID) AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CacheBuilder(com.google.common.cache.CacheBuilder) ATTR_LAYOUT_RESOURCE_PREFIX(com.android.SdkConstants.ATTR_LAYOUT_RESOURCE_PREFIX) Cache(com.google.common.cache.Cache) NotNull(org.jetbrains.annotations.NotNull) Splitter(com.google.common.base.Splitter) Arrays.stream(java.util.Arrays.stream) ResolutionUtils(com.android.tools.idea.editors.theme.ResolutionUtils) Configuration(com.android.tools.idea.configurations.Configuration) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition)

Aggregations

AttributeDefinition (org.jetbrains.android.dom.attrs.AttributeDefinition)35 NotNull (org.jetbrains.annotations.NotNull)10 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)8 NlProperty (com.android.tools.idea.uibuilder.property.NlProperty)7 Nullable (org.jetbrains.annotations.Nullable)7 NlPropertyItem (com.android.tools.idea.uibuilder.property.NlPropertyItem)6 AttributeDefinitions (org.jetbrains.android.dom.attrs.AttributeDefinitions)6 ModelBuilder (com.android.tools.idea.uibuilder.fixtures.ModelBuilder)5 NlModel (com.android.tools.idea.uibuilder.model.NlModel)5 AttributeFormat (org.jetbrains.android.dom.attrs.AttributeFormat)5 ResourceType (com.android.resources.ResourceType)4 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)3 XmlTag (com.intellij.psi.xml.XmlTag)3 ResourceManager (org.jetbrains.android.resourceManagers.ResourceManager)3 HtmlBuilder (com.android.utils.HtmlBuilder)2 NamespaceAwareXmlAttributeDescriptor (com.intellij.xml.NamespaceAwareXmlAttributeDescriptor)2 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)2 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)2 ViewGroup (android.view.ViewGroup)1 LinearLayout (android.widget.LinearLayout)1