package com.tempusnova.canvasitemtest.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CanvasItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.RowEditorExitEvent;
import com.smartgwt.client.widgets.grid.events.RowEditorExitHandler;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class CanvasItemStoreValueTest implements EntryPoint {
	
	public void onModuleLoad() {
		
		final ListGrid grid = new ListGrid();
		grid.setHeight(150);
		ListGridField nameField = new ListGridField("Name");
		ListGridField numberField = new ListGridField("Number");
		numberField.setType(ListGridFieldType.FLOAT);
		grid.setFields(nameField, numberField);
		
		grid.addRowEditorExitHandler(new RowEditorExitHandler() {
			@Override
			public void onRowEditorExit(RowEditorExitEvent event) 
			{
				ListGrid myGrid = (ListGrid)event.getSource();
				if(myGrid != null)
				{
					//ListGrid -> VLayout -> CanvasItem
					myGrid.getParentElement().getCanvasItem().storeValue(myGrid.getDataAsRecordList());
				}
			}
		});
		
		IButton addButton = new IButton("Add");
		addButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event)
			{
				grid.startEditingNew();
			}
		});
		
		VLayout canvasLayout = new VLayout();
		canvasLayout.setMembers(grid, addButton);
		
		CanvasItem canvasItem = new CanvasItem("canvasItem", "Canvas Item");
		canvasItem.setCanvas(canvasLayout);
		
		TextItem listener = new TextItem("listener", "Changed Listener");
		
		canvasItem.addChangedHandler(new ChangedHandler() {
			@Override
			public void onChanged(ChangedEvent event)
			{
				event.getItem().addChangedHandler(this);
				FormItem targetField = event.getForm().getItem("listener");
				if (targetField != null)
				{
					SC.logWarn("ChangedEvent fired");
					RecordList records = RecordList.getOrCreateRef((JavaScriptObject)event.getValue());
					targetField.setValue("Grid now contains " + records.getLength() + " records");
				}
			}
		});
		
		DynamicForm form = new DynamicForm();
		form.setWidth("30%");
		form.setFields(canvasItem, listener);
		
	    RootPanel.get().add(form);
	}
}
