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
 *
 * 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 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;
	}

}