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

com.mantledillusion.vaadin.cotton.model.ModelContainer Maven / Gradle / Ivy

Go to download

Cotton is a Vaadin extension destined for the ultimate of developer convenience.

There is a newer version: 2.3.4
Show newest version
package com.mantledillusion.vaadin.cotton.model;

import java.lang.annotation.ElementType;
import java.util.ArrayList;
import java.util.List;

import com.mantledillusion.data.epiphy.Property;
import com.mantledillusion.data.epiphy.context.Context;
import com.mantledillusion.data.epiphy.context.function.*;
import com.mantledillusion.data.epiphy.context.reference.ReferencedValue;
import org.apache.commons.lang3.ObjectUtils;

import com.mantledillusion.vaadin.cotton.model.ModelBinder.UpdateType;

/**
 * {@link ModelHandler} implementation that contains the model instance.
 *
 * @param 
 *            The root type of the data model the {@link ModelContainer} is able
 *            to contain.
 */
public final class ModelContainer implements ModelHandler {

	public static final String SID_CONTAINER = "_modelContainer";
	public static final String SID_PROPERTYCONTEXT = "_propertyContext";

	private final List> children = new ArrayList<>();
	private ModelType model;

	final void register(ModelAccessor childAccessor) {
		this.children.add(childAccessor);
		childAccessor.updateAll(UpdateType.EXCHANGE);
	}

	final void unregister(ModelAccessor childAccessor) {
		this.children.remove(childAccessor);
	}

	private void updateAll(UpdateType type) {
		this.children.forEach(child -> child.updateAll(type));
	}

	private void update(Property property, Context context, UpdateType type) {
		this.children.forEach(child -> child.update(property, context, type));
	}

	// ######################################################################################################################################
	// ########################################################### MODEL CONTROL ############################################################
	// ######################################################################################################################################

	@Override
	public ModelType getModel() {
		return model;
	}

	@Override
	public void setModel(ModelType model) {
		this.model = model;
		updateAll(UpdateType.EXCHANGE);
	}

	@Override
	public  boolean exists(Property property) {
		return exists(property, Context.EMPTY);
	}

	@Override
	public  boolean exists(Property property, Context context) {
		context = ObjectUtils.defaultIfNull(context, Context.EMPTY);
		return property.exists(this.model, context);
	}

	// ######################################################################################################################################
	// ###################################################### PROPERTIED MODEL ACCESS #######################################################
	// ######################################################################################################################################

	@Override
	public  PropertyValueType get(Property property) {
		return get(property, Context.EMPTY);
	}

	@Override
	public  PropertyValueType get(Property property, Context context) {
		context = ObjectUtils.defaultIfNull(context, Context.EMPTY);
		return property.get(this.model, context, true);
	}

	@Override
	public  void set(Property property,
			PropertyValueType value) {
		set(property, value, Context.EMPTY);
	}

	@Override
	public  void set(Property property, PropertyValueType value, Context context) {
		context = ObjectUtils.defaultIfNull(context, Context.EMPTY);
		property.set(this.model, value, context);
		update(property, context, UpdateType.EXCHANGE);
	}

	@Override
	public  ReferenceType include(IncludableProperty property, PropertyElementType element) {
		return include(property, element, Context.EMPTY);
	}

	@Override
	public  ReferenceType include(IncludableProperty property, PropertyElementType element, Context context) {
		context = ObjectUtils.defaultIfNull(context, Context.EMPTY);
		ReferenceType reference = property.include(this.model, element, context);
		update(property, context, UpdateType.ADD);
		return reference;
	}

	@Override
	public  void insert(InsertableProperty property, PropertyElementType element, ReferenceType reference) {
		this.insert(property, element, reference, Context.EMPTY);
	}

	@Override
	public  void insert(InsertableProperty property, PropertyElementType element, ReferenceType reference, Context context) {
		context = ObjectUtils.defaultIfNull(context, Context.EMPTY);
		property.insert(this.model, element, reference, context);
		update(property, context, UpdateType.ADD);
	}

	@Override
	public  ReferencedValue strip(StripableProperty property) {
		return strip(property, Context.EMPTY);
	}

	@Override
	public  ReferencedValue strip(StripableProperty property, Context context) {
		context = ObjectUtils.defaultIfNull(context, Context.EMPTY);
		ReferencedValue referencedValue = property.strip(this.model, context);
		update(property, context, UpdateType.REMOVE);
		return referencedValue;
	}

	@Override
	public  ReferenceType drop(DropableProperty property, PropertyElementType element) {
		return drop(property, element, Context.EMPTY);
	}

	@Override
	public  ReferenceType drop(DropableProperty property, PropertyElementType element, Context context) {
		context = ObjectUtils.defaultIfNull(context, Context.EMPTY);
		ReferenceType reference = property.drop(this.model, element, context);
		update(property, context, UpdateType.REMOVE);
		return reference;
	}

	@Override
	public  PropertyElementType extract(ExtractableProperty property, ReferenceType reference) {
		return extract(property, reference, Context.EMPTY);
	}

	@Override
	public  PropertyElementType extract(ExtractableProperty property, ReferenceType reference, Context context) {
		context = ObjectUtils.defaultIfNull(context, Context.EMPTY);
		PropertyElementType element = property.extract(this.model, reference, context);
		update(property, context, UpdateType.REMOVE);
		return element;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy