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

com.dragome.forms.bindings.builders.ModelBinder Maven / Gradle / Ivy

There is a newer version: 0.96-beta4
Show newest version
/*******************************************************************************
 * Copyright (c) 2011-2014 Fernando Petrola
 * 
 *  This file is part of Dragome SDK.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 ******************************************************************************/
package com.dragome.forms.bindings.builders;

import com.dragome.forms.bindings.client.bean.AbstractBeanModelProvider;
import com.dragome.forms.bindings.client.form.FieldModel;
import com.dragome.forms.bindings.client.form.FormModel;
import com.dragome.forms.bindings.client.form.FormattedFieldModel;
import com.dragome.forms.bindings.client.form.ListFieldModel;
import com.dragome.forms.bindings.client.form.binding.FormBinder;
import com.dragome.forms.bindings.client.format.Format;
import com.dragome.forms.bindings.client.style.StyleBinder;
import com.dragome.forms.bindings.client.value.ValueHolder;
import com.dragome.forms.bindings.client.value.ValueModel;
import com.dragome.forms.bindings.reflect.ReflectionBeanModelProvider;
import com.dragome.model.interfaces.HasValue;
import com.dragome.model.interfaces.HasVisible;
import com.dragome.model.interfaces.VisualComponent;
import com.dragome.model.interfaces.VisualPanel;
import com.dragome.model.interfaces.list.HasListModel;

public class ModelBinder
{
	protected AbstractBeanModelProvider modelProvider;
	protected FormModel formModel= new FormModel();
	protected FormBinder binder= new FormBinder();
	private VisualPanel panel;

	public ModelBinder(T model)
	{
		modelProvider= new ReflectionBeanModelProvider((Class) model.getClass());
		modelProvider.setBeanSource(new ValueHolder(model));
		modelProvider.setAutoCommit(true);
	}

	public ModelBinder(T model, VisualPanel panel)
	{
		this(model);
		this.panel= panel;
	}

	public > ModelBinder bind(String key, V aHasValue)
	{
		bindMemberToValueHolder(key, aHasValue, String.class);
		//		System.out.println(fieldModel.getValue());
		return this;
	}

	public , H> void bindFormattedMemberToValueHolder(String propertyName, V aHasValue, Class type, Format formatter)
	{
		FormattedFieldModel formattedFieldModel= formModel.formattedFieldOfType(type).using(formatter).boundTo(modelProvider, propertyName);
		aHasValue.setValue(formattedFieldModel.getValue());
		binder.bind(formattedFieldModel).to((HasValue) aHasValue);
	}

	public , H> FieldModel bindMemberToValueHolder(String propertyName, V aHasValue, Class type)
	{
		FieldModel fieldModel= (FieldModel) formModel.fieldOfType(type).boundTo(modelProvider, propertyName);
		aHasValue.setValue(fieldModel.getValue());
		binder.bind(fieldModel).to(aHasValue);
		return fieldModel;
	}

	public > ModelBinder bindToPanel(String key, V aHasValue)
	{
		panel.addChild(aHasValue);
		return bind(key, aHasValue);
	}

	public > ModelBinder bindToPanel(V component)
	{
		return bindToPanel(component.getName(), component);
	}

	public > ModelBinder bindToPanel(String key, V aHasValue)
	{
		return bindToPanel(key, aHasValue);
	}

	public  void bindListMemberToHasListModel(String propertyName, HasListModel hasListModel, Class type)
	{
		ListFieldModel listFieldModel= (ListFieldModel) new FormModel().listOfType(type).boundTo(modelProvider, propertyName);
		binder.bind(listFieldModel).to(hasListModel.getListModel());
	}

	public ModelBinder bindVisible(HasVisible hasVisible, ValueModel valueModel)
	{
		binder.show(hasVisible).when(valueModel);
		return this;
	}

	public ModelBinder bindStyle(VisualComponent visualComponent, ValueModel valueModel)
	{
		StyleBinder style= new StyleBinder();
		style.style(visualComponent).with("disabled").when(valueModel);
		return this;
	}

	public , H> FieldModel bindMemberToValueHolderAdding(String propertyName, V aHasValue, Class type)
	{
		FieldModel result= bindMemberToValueHolder(propertyName, aHasValue, type);
		panel.addChild(aHasValue);
		return result;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy