package com.smartgwt.sample.client;

import com.smartgwt.client.util.EventHandler;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.FormItemIcon;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.IconClickEvent;
import com.smartgwt.client.widgets.form.fields.events.IconClickHandler;

public class CustomTextItem extends TextItem
{
	private CustomCanvas customCanvas;

	public CustomTextItem()
	{
		FormItemIcon formItemIcon = new FormItemIcon();
		setIcons( formItemIcon );
		customCanvas = new CustomCanvas();

		addIconClickHandler( new IconClickHandler()
		{
			@Override
			public void onIconClick(IconClickEvent event)
			{
				System.out.println( "getValue() is " + event.getItem().getValue() );

				FormItem formItem = event.getItem();
				customCanvas.setValue( (String) formItem.getValue() );

				customCanvas.setTop( EventHandler.getY() );
				customCanvas.setLeft( EventHandler.getX() );
				customCanvas.show();
				customCanvas.bringToFront();
			}
		} );

		customCanvas.registerCancelHandler( new ClickCallback()
		{
			@Override
			public void onClick()
			{
				customCanvas.hide();
			}
		} );

		customCanvas.registerSaveHandler( new CustomTextItemCallback( CustomTextItem.this ) );

	}

	public class CustomTextItemCallback implements ObjectCallback
	{
		CustomTextItem myCustomTextItem;

		public CustomTextItemCallback(CustomTextItem customTextItem)
		{
			myCustomTextItem = customTextItem;
		}

		@Override
		public void execute(Object value)
		{
			customCanvas.hide();
			System.out.println( "CustomTextItem, value = " + value );
			myCustomTextItem.setValue( (String) value );
			System.out.println( "myCustomTextItem.getValue() = " + myCustomTextItem.getValue() );
		}
	}

}
