com.dragome.forms.bindings.builders.ModelBinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dragome-form-bindings Show documentation
Show all versions of dragome-form-bindings Show documentation
Dragome SDK module: form bindings
/*
* Copyright (c) 2011-2014 Fernando Petrola
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.guia.components.interfaces.VisualComponent;
import com.dragome.guia.components.interfaces.VisualPanel;
import com.dragome.model.interfaces.HasValue;
import com.dragome.model.interfaces.HasVisible;
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 extends H> type, Format formatter)
{
FormattedFieldModel extends H> 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 extends H> 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 extends H> 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 extends H> type)
{
FieldModel result= bindMemberToValueHolder(propertyName, aHasValue, type);
panel.addChild(aHasValue);
return result;
}
}