All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.robobinding.widget.edittext.TwoWayTextAttributeGroup Maven / Gradle / Ivy

There is a newer version: 0.8.14
Show newest version
package org.robobinding.widget.edittext;

import static org.robobinding.attribute.ChildAttributeResolvers.enumChildAttributeResolver;
import static org.robobinding.attribute.ChildAttributeResolvers.valueModelAttributeResolver;

import org.robobinding.BindingContext;
import org.robobinding.attribute.ChildAttributeResolverMappings;
import org.robobinding.attribute.EnumAttribute;
import org.robobinding.attribute.MalformedAttributeException;
import org.robobinding.attribute.ResolvedGroupAttributes;
import org.robobinding.attribute.ValueModelAttribute;
import org.robobinding.viewattribute.grouped.ChildViewAttributesBuilder;
import org.robobinding.viewattribute.grouped.GroupedViewAttribute;

import android.widget.EditText;

/**
 * 
 * @since 1.0
 * @version $Revision: 1.0 $
 * @author Robert Taylor
 */
public class TwoWayTextAttributeGroup implements GroupedViewAttribute {
	public static final String TEXT = "text";
	public static final String VALUE_COMMIT_MODE = "valueCommitMode";

	@Override
	public String[] getCompulsoryAttributes() {
		return new String[] { TEXT };
	}

	@Override
	public void mapChildAttributeResolvers(ChildAttributeResolverMappings resolverMappings) {
		resolverMappings.map(valueModelAttributeResolver(), TEXT);
		resolverMappings.map(enumChildAttributeResolver(ValueCommitMode.class), VALUE_COMMIT_MODE);
	}

	@Override
	public void validateResolvedChildAttributes(ResolvedGroupAttributes resolvedGroupAttributes) {
		if (valueCommitModeSpecified(resolvedGroupAttributes) && isTextAttributeNotTwoWayBinding(resolvedGroupAttributes))
			throw new MalformedAttributeException(VALUE_COMMIT_MODE,
					"The valueCommitMode attribute can only be used when a two-way binding text attribute is specified");
	}

	private boolean valueCommitModeSpecified(ResolvedGroupAttributes resolvedGroupAttributes) {
		return resolvedGroupAttributes.hasAttribute(VALUE_COMMIT_MODE);
	}

	private boolean isTextAttributeNotTwoWayBinding(ResolvedGroupAttributes resolvedGroupAttributes) {
		ValueModelAttribute textAttribute = resolvedGroupAttributes.valueModelAttributeFor(TEXT);
		return !textAttribute.isTwoWayBinding();
	}

	@Override
	public void setupChildViewAttributes(EditText view, ChildViewAttributesBuilder childViewAttributesBuilder, BindingContext bindingContext) {
		TwoWayTextAttribute textAttribute = new TwoWayTextAttribute();
		childViewAttributesBuilder.add(TEXT, textAttribute);
		textAttribute.setValueCommitMode(determineValueCommitMode(childViewAttributesBuilder));
	}

	private ValueCommitMode determineValueCommitMode(ChildViewAttributesBuilder childViewAttributesBuilder) {
		if (valueCommitModeSpecified(childViewAttributesBuilder)) {
			EnumAttribute enumAttribute = childViewAttributesBuilder.enumAttributeFor(VALUE_COMMIT_MODE);
			return enumAttribute.getValue();
		}

		return ValueCommitMode.ON_CHANGE;
	}

	private boolean valueCommitModeSpecified(ChildViewAttributesBuilder childViewAttributesBuilder) {
		return childViewAttributesBuilder.hasAttribute(VALUE_COMMIT_MODE);
	}

	@Override
	public void postBind(EditText view, BindingContext bindingContext) {
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy