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();
}
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;
}
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;
}
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;
}
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;
}
}
Aggregations