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

com.dragome.forms.bindings.builders.DrangularTemplateRepeater Maven / Gradle / Ivy

The 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.HashMap;
import java.util.List;
import java.util.Map;

import com.dragome.guia.GuiaServiceLocator;
import com.dragome.render.interfaces.TemplateHandler;
import com.dragome.templates.interfaces.SimpleItemProcessor;
import com.dragome.templates.interfaces.Template;

public class DrangularTemplateRepeater
{
	private List items;
	private List lastItems;
	private Template mainTemplate;
	private String subTemplateName;
	private SimpleItemProcessor simpleItemProcessor;
	private boolean updating;
	private Map clonedChildren= new HashMap();
	private Template child;
	private Template lastInserted;
	private Template parent;

	public DrangularTemplateRepeater()
	{
	}

	public DrangularTemplateRepeater(List items, Template mainTemplate, String subTemplateName, final SimpleItemProcessor simpleItemProcessor, boolean updating)
	{
		this.items= items;
		this.mainTemplate= mainTemplate;
		this.subTemplateName= subTemplateName;
		this.simpleItemProcessor= simpleItemProcessor;
		this.updating= updating;
	}

	public void repeatItems()
	{
		if (!items.equals(lastItems))
		{
			TemplateHandler templateHandler= GuiaServiceLocator.getInstance().getTemplateHandler();
			if (child == null)
			{
				child= mainTemplate.getChild(subTemplateName);
				parent= child.getParent();
			}

			if (lastInserted != null)
				parent.insertAfter(child, lastInserted);

			for (Template template : clonedChildren.values())
				if (template.getParent() != null)
					template.getParent().remove(template);

			if (items.isEmpty())
				lastInserted= null;
			
			for (T item : items)
			{
				Template clonedChild= templateHandler.clone(child);
				clonedChild.setName(clonedChild.getName() + System.currentTimeMillis());
				clonedChildren.put(item, clonedChild);
				simpleItemProcessor.fillTemplate(item, clonedChild);

				parent.insertBefore(clonedChild, child);
				lastInserted= clonedChild;
				templateHandler.makeVisible(clonedChild);
			}

			parent.remove(child);

			lastItems= new ArrayList(items);
		}
	}

	public void clearAndRepeatItems()
	{
		repeatItems();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy