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
*
* 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 java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.dragome.model.VisualPanelImpl;
import com.dragome.model.interfaces.ValueChangeEvent;
import com.dragome.model.interfaces.ValueChangeHandler;
import com.dragome.model.interfaces.VisualComponent;
import com.dragome.model.interfaces.VisualPanel;
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