package com.tid.test.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.tid.test.client.ds.SimpleDataSource;

public class ValuesChangedCrash implements EntryPoint {
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		final ListGrid list = new ListGrid();
		list.setDataSource(SimpleDataSource.getInstance());
		ListGridField nameField = new ListGridField(SimpleDataSource.NAME, "Name");
		ListGridField codeField = new ListGridField(SimpleDataSource.CODE, "Code");
		list.setFields(nameField, codeField);
		list.setAutoFetchData(true);

		final DynamicForm form = new DynamicForm();
		form.setDataSource(SimpleDataSource.getInstance());

		TextItem nameItem = new TextItem(SimpleDataSource.NAME, "Name");
		TextItem codeItem = new TextItem(SimpleDataSource.CODE, "Code");
		ButtonItem buttonItem = new ButtonItem("Submit");
		buttonItem.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (form.valuesHaveChanged()) {
					form.saveData();
				} else {
					SC.logWarn("No values changed");
				}
			}
			
		});
		form.setFields(nameItem, codeItem, buttonItem);

		list.addCellClickHandler(new CellClickHandler () {
			public void onCellClick(CellClickEvent event) {
				form.editRecord(event.getRecord());	
				form.rememberValues();
			}
			
		});
		VLayout layout = new VLayout();
		layout.setWidth100();
		layout.setHeight100();
		list.setHeight("50%");
		form.setHeight("50%");
		layout.setMembers(list, form);
		layout.draw();
	}
	
}
