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

org.robobinding.binder.ByBindingAttributeMappingsResolver Maven / Gradle / Ivy

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

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.robobinding.PendingAttributesForView;
import org.robobinding.PendingAttributesForView.AttributeGroupResolver;
import org.robobinding.PendingAttributesForView.AttributeResolver;
import org.robobinding.viewattribute.ViewAttributeBinder;
import org.robobinding.viewattribute.event.EventViewAttributeBinder;
import org.robobinding.viewattribute.event.EventViewAttributeBinderFactory;
import org.robobinding.viewattribute.grouped.GroupedViewAttributeBinder;
import org.robobinding.viewattribute.grouped.GroupedViewAttributeBinderFactory;
import org.robobinding.viewattribute.property.MultiTypePropertyViewAttributeBinderFactory;
import org.robobinding.viewattribute.property.PropertyViewAttributeBinderFactory;
import org.robobinding.viewattribute.property.MultiTypePropertyViewAttributeBinder;
import org.robobinding.viewattribute.property.PropertyViewAttributeBinder;
import org.robobinding.viewbinding.InitailizedBindingAttributeMappings;

import com.google.common.collect.Lists;

/**
 * 
 * @since 1.0
 * @version $Revision: 1.0 $
 * @author Cheng Wei
 */
public class ByBindingAttributeMappingsResolver {
	private final InitailizedBindingAttributeMappings bindingAttributeMappings;

	private final PropertyViewAttributeResolver propertyViewAttributeResolver;
	private final MultiTypePropertyViewAttributeResolver multiTypePropertyViewAttributeResolver;
	private final EventViewAttributeResolver commandViewAttributeResolver;
	private final GroupedViewAttributeResolver groupedViewAttributeResolver;

	private List resolvedViewAttributes;

	public ByBindingAttributeMappingsResolver(InitailizedBindingAttributeMappings bindingAttributeMappings) {
		this.bindingAttributeMappings = bindingAttributeMappings;

		this.propertyViewAttributeResolver = new PropertyViewAttributeResolver();
		this.multiTypePropertyViewAttributeResolver = new MultiTypePropertyViewAttributeResolver();
		this.commandViewAttributeResolver = new EventViewAttributeResolver();
		this.groupedViewAttributeResolver = new GroupedViewAttributeResolver();
	}

	public Collection resolve(PendingAttributesForView pendingAttributesForView) {
		resolvedViewAttributes = Lists.newArrayList();

		resolvePropertyViewAttributes(pendingAttributesForView);
		resolveMultiTypePropertyViewAttributes(pendingAttributesForView);
		resolveCommandViewAttributes(pendingAttributesForView);
		resolveGroupedViewAttributes(pendingAttributesForView);

		List temp = resolvedViewAttributes;
		resolvedViewAttributes = null;
		return temp;
	}

	private void resolvePropertyViewAttributes(PendingAttributesForView pendingAttributesForView) {
		for (String propertyAttribute : bindingAttributeMappings.getPropertyAttributes()) {
			pendingAttributesForView.resolveAttributeIfExists(propertyAttribute, propertyViewAttributeResolver);
		}
	}

	private void resolveMultiTypePropertyViewAttributes(PendingAttributesForView pendingAttributesForView) {
		for (String propertyAttribute : bindingAttributeMappings.getMultiTypePropertyAttributes()) {
			pendingAttributesForView.resolveAttributeIfExists(propertyAttribute, multiTypePropertyViewAttributeResolver);
		}
	}

	private void resolveCommandViewAttributes(PendingAttributesForView pendingAttributesForView) {
		for (String commandAttribute : bindingAttributeMappings.getEventAttributes()) {
			pendingAttributesForView.resolveAttributeIfExists(commandAttribute, commandViewAttributeResolver);
		}
	}

	private void resolveGroupedViewAttributes(PendingAttributesForView pendingAttributesForView) {
		for (String[] attributeGroup : bindingAttributeMappings.getAttributeGroups()) {
			pendingAttributesForView.resolveAttributeGroupIfExists(attributeGroup, groupedViewAttributeResolver);
		}
	}

	private class PropertyViewAttributeResolver implements AttributeResolver {
		@Override
		public void resolve(Object view, String attribute, String attributeValue) {
			PropertyViewAttributeBinderFactory factory = bindingAttributeMappings.getPropertyViewAttributeFactory(attribute);
			PropertyViewAttributeBinder viewAttributeBinder = factory.create(view, attribute, attributeValue);
			resolvedViewAttributes.add(viewAttributeBinder);
		}
	}

	private class MultiTypePropertyViewAttributeResolver implements AttributeResolver {
		@Override
		public void resolve(Object view, String attribute, String attributeValue) {
			MultiTypePropertyViewAttributeBinderFactory factory = bindingAttributeMappings.getMultiTypePropertyViewAttributeFactory(attribute);
			MultiTypePropertyViewAttributeBinder viewAttributeBinder = factory.create(view, attribute, attributeValue);
			resolvedViewAttributes.add(viewAttributeBinder);
		}
	}

	private class EventViewAttributeResolver implements AttributeResolver {
		@Override
		public void resolve(Object view, String attribute, String attributeValue) {
			EventViewAttributeBinderFactory factory = bindingAttributeMappings.getEventViewAttributeFactory(attribute);
			EventViewAttributeBinder viewAttributeBinder = factory.create(view, attribute, attributeValue);
			resolvedViewAttributes.add(viewAttributeBinder);
		}
	}

	private class GroupedViewAttributeResolver implements AttributeGroupResolver {
		@Override
		public void resolve(Object view, String[] attributeGroup, Map presentAttributeMappings) {
			GroupedViewAttributeBinderFactory factory = bindingAttributeMappings.getGroupedViewAttributeFactory(attributeGroup);
			GroupedViewAttributeBinder groupedViewAttribute = factory.create(view, presentAttributeMappings);
			resolvedViewAttributes.add(groupedViewAttribute);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy