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

com.dragome.forms.bindings.builders.RepeaterBuilder 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 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 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 comparable= (Comparable) orderByGetter.get(o1);
						Comparable another= orderByGetter.get(o2);
						if (comparable != null && another != null)
						{
							int compareTo= comparable.compareTo(another);
							return compareTo * (order.get() == Order.DESC ? -1 : 1);
						}
					}

					return 0;
				}
			});
	}
	

	private List collectItems(List value)
	{
		List collectedList= new ArrayList();
		for (final T t : value)
		{
			if (filter == null || filter.get().test(t))
			{
				if (valueModelDelegatorForFilter == null && filter != null)
					valueModelDelegatorForFilter= createWatcher(new Runnable()
					{
						public void run()
						{
							filter.get().test(t);
						}
					});

				collectedList.add(t);
			}
		}

		// Java 8 Stream Implementation
		// List collectedList= valueModelDelegator.getValue().stream().filter(filter.get(model)).collect(Collectors.toList());
		return collectedList;
	}

	private  ValueModelDelegator createWatcher(final Runnable supplier)
	{
		ValueModelDelegator delegator;
		delegator= BindingSync.createCondition(new Supplier()
		{
			public S get()
			{
				supplier.run();
				return null;
			}
		});

		delegator.addValueChangeHandler(new ValueChangeHandler()
		{
			public void onValueChange(ValueChangeEvent event)
			{
				valueModelDelegator.fireValueChangeEvent();
			}
		});

		delegator.getValue();
		return delegator;
	}

	public RepeaterBuilder filter(Supplier> aFilter)
	{
		this.filter= aFilter;
		return this;
	}

	public RepeaterBuilder orderBy(Getter> getter, Supplier order)
	{
		this.orderByGetter= getter;
		this.order= order;
		return this;
	}

	public TemplateComponentBindingBuilder builder()
	{
		return templateComponentBindingBuilder;
	}

}