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

com.dragome.forms.bindings.client.bean.PropertyModelRegistry Maven / Gradle / Ivy

There is a newer version: 0.96-beta4
Show newest version
package com.dragome.forms.bindings.client.bean;

import java.util.HashMap;
import java.util.LinkedHashMap;

import com.dragome.forms.bindings.client.condition.OrFunction;
import com.dragome.forms.bindings.client.value.ReducingValueModel;
import com.dragome.forms.bindings.client.value.ValueModel;

/**
 * Created by IntelliJ IDEA.
 * User: andrew
 * Date: Mar 26, 2010
 * Time: 11:31:43 AM
 * To change this template use File | Settings | File Templates.
 */
public class PropertyModelRegistry
{
	private LinkedHashMap allModels= new LinkedHashMap();

	private HashMap> valueModels= new HashMap>();
	private HashMap> listModels= new HashMap>();

	private ReducingValueModel dirtyModel= new ReducingValueModel(new OrFunction());

	public ValueModel getDirtyModel()
	{
		return dirtyModel;
	}

	/**
	 * Visits each model in the order it was registered.  This method also holds prevents
	 * any changes to the dirty state until after the visitor has finished.  This is mainly
	 * to prevent the dirty model from recomputing needlessly if the visitor is updating the
	 * dirty state of each model.
	 *
	 * @param visitor the visitor.
	 */
	public void withEachModel(final PropertyModelVisitor visitor)
	{
		// we only update the dirty model after we've finished updating all
		// the models, otherwise we'll get a lot of recomputing for nothing.
		dirtyModel.recomputeAfterRunning(new Runnable()
		{
			public void run()
			{
				// performance could be improved here if the dirty model went deaf for a bit.
				for (BeanPropertyModelBase model : allModels.values())
				{
					visitor.visit(model);
				}
			}
		});
	}

	public BeanPropertyValueModel getValueModel(String fullPath)
	{
		return valueModels.get(fullPath);
	}

	public BeanPropertyListModel getListModel(String fullPath)
	{
		return listModels.get(fullPath);
	}

	public  void add(String fullPath, BeanPropertyListModel listModel)
	{
		doAddCommonBits(fullPath, listModel);
		listModels.put(fullPath, listModel);
	}

	public  void add(String fullPath, BeanPropertyValueModel valueModel)
	{
		doAddCommonBits(fullPath, valueModel);
		valueModels.put(fullPath, valueModel);
	}

	private void doAddCommonBits(String key, BeanPropertyModelBase valueModel)
	{
		allModels.put(key, valueModel);
		dirtyModel.addSourceModel(valueModel.dirty());
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy