com.dragome.forms.bindings.builders.TemplateComponentBindingBuilder 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 java.util.List;
import com.dragome.forms.bindings.client.form.binding.FormBinder;
import com.dragome.forms.bindings.client.value.ValueSource;
import com.dragome.guia.components.VisualButtonImpl;
import com.dragome.guia.components.VisualCheckboxImpl;
import com.dragome.guia.components.VisualImageImpl;
import com.dragome.guia.components.VisualLabelImpl;
import com.dragome.guia.components.VisualLinkImpl;
import com.dragome.guia.components.VisualPanelImpl;
import com.dragome.guia.components.VisualRadioButton;
import com.dragome.guia.components.VisualRadioButtonImpl;
import com.dragome.guia.components.VisualTextFieldImpl;
import com.dragome.guia.components.interfaces.VisualButton;
import com.dragome.guia.components.interfaces.VisualCheckbox;
import com.dragome.guia.components.interfaces.VisualComponent;
import com.dragome.guia.components.interfaces.VisualImage;
import com.dragome.guia.components.interfaces.VisualLabel;
import com.dragome.guia.components.interfaces.VisualLink;
import com.dragome.guia.components.interfaces.VisualPanel;
import com.dragome.guia.components.interfaces.VisualTextField;
import com.dragome.model.interfaces.HasValue;
import com.dragome.services.ServiceLocator;
import com.dragome.templates.TemplateLayout;
import com.dragome.templates.interfaces.Template;
public class TemplateComponentBindingBuilder extends BaseBuilder>
{
private Class extends VisualComponent> componentType;
private VisualPanel panel;
private FormBinder binder= new FormBinder();
private Template template;
private boolean built= false;
public TemplateComponentBindingBuilder(Template template, VisualPanel panel, Class componentType, BaseBuilder extends VisualComponent, ?> parentBuilder)
{
setParentBuilder((BaseBuilder extends VisualComponent, TemplateComponentBindingBuilder>) parentBuilder);
this.panel= panel;
this.template= template;
if (componentType.equals(VisualPanel.class))
this.componentType= VisualPanelImpl.class;
else if (componentType.equals(VisualTextField.class))
this.componentType= VisualTextFieldImpl.class;
else if (componentType.equals(VisualButton.class))
this.componentType= VisualButtonImpl.class;
else if (componentType.equals(VisualCheckbox.class))
this.componentType= VisualCheckboxImpl.class;
else if (componentType.equals(VisualRadioButton.class))
this.componentType= VisualRadioButtonImpl.class;
else if (componentType.equals(VisualLink.class))
this.componentType= VisualLinkImpl.class;
else if (componentType.equals(VisualLabel.class))
this.componentType= VisualLabelImpl.class;
else if (componentType.equals(VisualImage.class))
this.componentType= VisualImageImpl.class;
component= (C) ServiceLocator.getInstance().getReflectionService().createClassInstance(this.componentType);
setupComponent();
}
private void setupComponent()
{
component.setName(template.getName());
if (component instanceof VisualPanel)
{
VisualPanel visualPanel= (VisualPanel) component;
if (!(visualPanel.getLayout() instanceof TemplateLayout) || ((TemplateLayout) visualPanel.getLayout()).getTemplate() == null)
visualPanel.initLayout(new TemplateLayout(template));
}
}
public TemplateComponentBindingBuilder(Template template, VisualPanel panel, C component, BaseBuilder extends VisualComponent, ?> parentBuilder)
{
setParentBuilder((BaseBuilder extends VisualComponent, TemplateComponentBindingBuilder>) parentBuilder);
this.template= template;
this.panel= panel;
this.component= component;
setupComponent();
}
// private ComponentBuilder childBuilder()
// {
// build();
// return childrenBuilder();
// }
public RepeaterBuilder toListProperty(final Supplier> getter)
{
final ValueModelDelegator> valueModelDelegator= new ValueModelDelegator>();
List list= getter.get();
addListenerIfObservable(valueModelDelegator, list);
NullMutableValueModel> valueSource= new NullMutableValueModel>()
{
public List getDelegatedValue()
{
return getter.get();
}
};
valueModelDelegator.setValueSource(valueSource);
BindingSync.addCondition(valueModelDelegator);
return new RepeaterBuilder(valueModelDelegator, template, panel, (TemplateComponentBindingBuilder) this);
}
private void addListenerIfObservable(final ValueModelDelegator> valueModelDelegator, List list)
{
if (list instanceof ObservableList)
{
ObservableList observableList= (ObservableList) list;
observableList.setListChangeListener(new ListChangedListener()
{
public void listChanged()
{
valueModelDelegator.fireValueChangeEvent();
}
});
}
}
public RepeaterBuilder toList(final List list)
{
final ValueModelDelegator> valueModelDelegator= new ValueModelDelegator>();
addListenerIfObservable(valueModelDelegator, list);
NullMutableValueModel> valueSource= new NullMutableValueModel>()
{
public List getDelegatedValue()
{
return list;
}
};
valueModelDelegator.setValueSource(valueSource);
BindingSync.addCondition(valueModelDelegator);
return new RepeaterBuilder(valueModelDelegator, template, panel, (TemplateComponentBindingBuilder) this);
}
public TemplateComponentBindingBuilder toProperty(final Supplier getter)
{
return toProperty(getter, (Consumer)null);
}
public TemplateComponentBindingBuilder toProperty(final Supplier getter, final Consumer setter)
{
ValueModelDelegator valueModelDelegator= new ValueModelDelegator(new NullMutableValueModel()
{
public S getDelegatedValue()
{
if (getter != null)
return getter.get();
else
return null;
}
public void setValue(S value)
{
if (setter != null)
setter.accept(value);
}
});
return with(valueModelDelegator);
}
private TemplateComponentBindingBuilder with(ValueModelDelegator valueModelDelegator)
{
binder.bind(valueModelDelegator).to((HasValue) component);
BindingSync.addCondition(valueModelDelegator);
return this;
}
public TemplateComponentBindingBuilder to(final ValueSource valueSource)
{
ValueModelDelegator valueModelDelegator= new ValueModelDelegator(new NullMutableValueModel()
{
public S getDelegatedValue()
{
return valueSource.getValue();
}
});
return with(valueModelDelegator);
}
public C build()
{
if (!built)
{
panel.addChild(component);
built= true;
}
return component;
}
public TemplateComponentBindingBuilder toProperty(final Object object, final String propertyName)
{
return toProperty(new Supplier()
{
public S get()
{
return (S) object;
}
}, propertyName);
}
public TemplateComponentBindingBuilder toProperty(final Supplier> supplier, final String propertyName)
{
return toProperty(new Supplier()
{
public S get()
{
S propertyValue= (S) ServiceLocator.getInstance().getReflectionService().getPropertyValue(supplier.get(), propertyName);
return propertyValue;
}
}, new Consumer()
{
public void accept(S t)
{
ServiceLocator.getInstance().getReflectionService().setPropertyValue(supplier.get(), propertyName, t);
}
});
}
}