com.dragome.forms.bindings.builders.RepeaterBuilder 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.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.dragome.guia.components.VisualPanelImpl;
import com.dragome.guia.components.interfaces.VisualComponent;
import com.dragome.guia.components.interfaces.VisualPanel;
import com.dragome.model.interfaces.ValueChangeEvent;
import com.dragome.model.interfaces.ValueChangeHandler;
import com.dragome.templates.TemplateRepeater;
import com.dragome.templates.interfaces.SimpleItemProcessor;
import com.dragome.templates.interfaces.Template;
public class RepeaterBuilder
{
private ValueModelDelegator> valueModelDelegator;
private Template mainTemplate;
private Template template;
private Supplier> filter;
private VisualPanel panel;
private TemplateComponentBindingBuilder extends VisualComponent> templateComponentBindingBuilder;
private Getter> orderByGetter;
private Supplier order;
private ValueModelDelegator valueModelDelegatorForFilter;
private ValueModelDelegator> valueModelDelegatorForOrder;
public RepeaterBuilder(ValueModelDelegator> valueModelDelegator, Template template, VisualPanel panel, TemplateComponentBindingBuilder templateComponentBindingBuilder)
{
this.valueModelDelegator= valueModelDelegator;
this.template= template;
this.panel= panel;
this.templateComponentBindingBuilder= templateComponentBindingBuilder;
}
public RepeaterBuilder repeat(final ItemRepeater itemRepeater)
{
final List list= new ArrayList(collectItems(valueModelDelegator.getValue()));
sortList(list);
final TemplateRepeater templateRepeater= new TemplateRepeater(list, template.getParent(), template.getName(), new SimpleItemProcessor()
{
public void fillTemplate(T item, Template aTemplate)
{
VisualPanelImpl itemPanel= new VisualPanelImpl(aTemplate);
panel.addChild(itemPanel);
ComponentBuilder componentBuilder= new ComponentBuilder(itemPanel, templateComponentBindingBuilder);
itemRepeater.process(item, componentBuilder);
componentBuilder.build();
}
}, true);
templateRepeater.repeatItems();
valueModelDelegator.addValueChangeHandler(new ValueChangeHandler>()
{
public void onValueChange(ValueChangeEvent> event)
{
List value= valueModelDelegator.getValue();
List collectedList= collectItems(value);
list.clear();
list.addAll(collectedList);
sortList(list);
templateRepeater.clearAndRepeatItems();
}
});
return this;
}
private void sortList(final List list)
{
if (orderByGetter != null)
Collections.sort(list, new Comparator()
{
public int compare(final T o1, T o2)
{
if (o1 != null && o2 != null)
{
if (valueModelDelegatorForOrder == null)
valueModelDelegatorForOrder= createWatcher(new Runnable()
{
public void run()
{
order.get();
orderByGetter.get(o1);
}
});
Comparable