package com.smartgwt.sample.client;

import com.google.gwt.event.shared.HandlerRegistration;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;


public class CustomCanvas extends Canvas
{
	private TextItem item;

	IButton saveButton;

	final IButton cancelButton;

	public CustomCanvas()
	{
		super( );
		this.setBackgroundColor( "#FFFFFF" );
		this.setBorder( "2px solid" );
		this.setWidth( 255 );
		this.setHeight( 100 );

		VLayout vlayout = new VLayout( 10 );
		vlayout.setPadding( 10 );

		DynamicForm itemForm = new DynamicForm();
		itemForm.setWidth( "100%" );

		item = new TextItem();
		item.setTitle( "Item" );
		item.setWidth( 200 );
		itemForm.setItems( item );
		
		vlayout.addMember( itemForm );
		
		HLayout hlayout = new HLayout();
		hlayout.setWidth( 255 );
		hlayout.setAlign( Alignment.CENTER );	

		saveButton = new IButton( "Save" );

		cancelButton = new IButton( "Cancel" );

		DynamicForm spacer1 = new DynamicForm();
		spacer1.setWidth( 10 );

		hlayout.addMember( saveButton );
		hlayout.addMember( spacer1 );
		hlayout.addMember( cancelButton );

		vlayout.addMember( hlayout );
		addChild( vlayout );
	}

	public HandlerRegistration registerSaveHandler(final ObjectCallback callback)
	{
		return saveButton.addClickHandler( new ClickHandler()
		{
			public void onClick(com.smartgwt.client.widgets.events.ClickEvent event)
			{
				String value = ((String)item.getValue()) + " plus a modification.";
				callback.execute( value );
			}
		} );
	}

	public HandlerRegistration registerCancelHandler(final ClickCallback callback)
	{
		return cancelButton.addClickHandler( new ClickHandler()
		{
			public void onClick(com.smartgwt.client.widgets.events.ClickEvent event)
			{
				callback.onClick();
			}
		} );
	}

	/**
	 * @param value
	 */
	public void setValue(String value)
	{
		item.setValue( value );		
	}

}
