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

com.dragome.forms.bindings.builders.helpers.BinderHelper Maven / Gradle / Ivy

The newest version!
package com.dragome.forms.bindings.builders.helpers;

import java.util.List;

import com.dragome.forms.bindings.builders.ActionExecutor;
import com.dragome.forms.bindings.builders.ChildrenBuilder;
import com.dragome.forms.bindings.builders.ComponentBuilder;
import com.dragome.forms.bindings.builders.Consumer;
import com.dragome.forms.bindings.builders.Supplier;
import com.dragome.forms.bindings.builders.TemplateBindingBuilder;
import com.dragome.forms.bindings.builders.TemplateComponentBindingBuilder;
import com.dragome.forms.bindings.client.value.ValueSource;
import com.dragome.guia.components.interfaces.VisualButton;
import com.dragome.guia.components.interfaces.VisualCheckbox;
import com.dragome.guia.components.interfaces.VisualComponent;
import com.dragome.guia.components.interfaces.VisualLabel;
import com.dragome.guia.components.interfaces.VisualLink;
import com.dragome.guia.components.interfaces.VisualPanel;
import com.dragome.guia.components.interfaces.VisualTextField;
import com.dragome.guia.events.listeners.interfaces.BlurListener;
import com.dragome.guia.events.listeners.interfaces.DoubleClickListener;
import com.dragome.guia.events.listeners.interfaces.KeyUpListener;

public class BinderHelper
{
	public static ComponentBuilder componentBuilder;
	private static TemplateBindingBuilder templateBindingBuilder;
	private String templateName;
	static TemplateComponentBindingBuilder templateComponentBindingBuilder;
	private static VisualComponent component;
	public static Class PANEL= VisualPanel.class;
	public static Class BUTTON= VisualButton.class;
	public static Class LABEL= VisualLabel.class;
	public static Class CHECKBOX= VisualCheckbox.class;
	public static Class TEXTFIELD= VisualTextField.class;
	public static Class LINK= VisualLink.class;

	public BinderHelper(String aChildTemplateName)
	{
		this.templateName= aChildTemplateName;
	}

	public static BinderHelper bind(String aChildTemplateName)
	{
		return new BinderHelper(aChildTemplateName);
	}

	public  C as(Class componentType, final ActionExecutor actionExecutor)
	{
		TemplateBindingBuilder lastTemplateBindingBuilder= templateBindingBuilder;
		templateBindingBuilder= componentBuilder.bindTemplate(templateName);
		templateComponentBindingBuilder= templateBindingBuilder.as(componentType);
		component= templateComponentBindingBuilder.component();
		templateComponentBindingBuilder.buildChildren(new ChildrenBuilder()
		{
			public void build(ComponentBuilder builder)
			{
				ComponentBuilder lastComponentBuilder= componentBuilder;
				componentBuilder= builder;
				actionExecutor.execute();
				componentBuilder= lastComponentBuilder;
			}
		});
		templateComponentBindingBuilder.build();
		templateBindingBuilder= lastTemplateBindingBuilder;
		return (C) component;
	}

	public static  TemplateComponentBindingBuilder toProperty(final Supplier getter, final Consumer setter)
	{
		return (TemplateComponentBindingBuilder) templateComponentBindingBuilder.toProperty(getter, setter);
	}

	public static  RepeaterBuilderHelper toListProperty(final Supplier> getter)
	{
		return new RepeaterBuilderHelper(templateComponentBindingBuilder.toListProperty(getter));
	}

	public static  TemplateComponentBindingBuilder to(final ValueSource valueSource)
	{
		return (TemplateComponentBindingBuilder) templateComponentBindingBuilder.to(valueSource);
	}

	public static void showWhen(Supplier supplier)
	{
		componentBuilder.showWhen(supplier);
	}

	public static void onClick(final ActionExecutor actionExecutor)
	{
		templateComponentBindingBuilder.onClick(actionExecutor);
	}

	public static void onDoubleClick(final ActionExecutor actionExecutor)
	{
		templateComponentBindingBuilder.onDoubleClick(new DoubleClickListener()
		{
			public void doubleClickPerformed(VisualComponent aVisualComponent)
			{
				actionExecutor.execute();
			}
		});
	}

	public static void onKeyUp(final KeyUpListener keyUpListener, int... codes)
	{
		templateComponentBindingBuilder.onKeyUp(keyUpListener, codes);
	}

	public static void onBlur(BlurListener blurListener)
	{
		templateComponentBindingBuilder.onBlur(blurListener);
	}

	public static ComponentBuilder styleWith(String className)
	{
		return componentBuilder.styleWith(className);
	}

	public static ComponentBuilder style()
	{
		return componentBuilder.style(component);
	}

	public static void start(VisualPanel mainPanel)
	{
		componentBuilder= new ComponentBuilder(mainPanel);
	}
}