package com.sbs.jpm.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.events.SubmitValuesEvent;
import com.smartgwt.client.widgets.form.events.SubmitValuesHandler;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;

public class TestCase implements EntryPoint {

	private Panel panel;
	private TestDataSource dataSource;

	public void onModuleLoad() {

		SC.showConsole();

		dataSource = new TestDataSource();
		
		panel = new Panel();
		panel.setWidth100();
		panel.setHeight100();

		panel.draw();
	}

	public class Panel extends HLayout {

		public Panel() {	
			final TextItem text = new TextItem();
			DynamicForm form = new DynamicForm() {
				public void saveData() {
					SC.logWarn("save Data");
					//do some custom code...
					Record newRecord = new Record();
					newRecord.setAttribute(TestDataSource.NAME_FIELD, text.getValueAsString());
					newRecord.setAttribute(TestDataSource.UNIQUE_ID_FIELD, Long.MIN_VALUE);
					dataSource.addData(newRecord);
				}
			};
			form.setDataSource(dataSource);
			form.addSubmitValuesHandler(new SubmitValuesHandler() {
				@Override
				public void onSubmitValues(SubmitValuesEvent event) {
					SC.logWarn("submit values");
					//do some custom code...
					Record newRecord = new Record();
					newRecord.setAttribute(TestDataSource.NAME_FIELD, text.getValueAsString());
					newRecord.setAttribute(TestDataSource.UNIQUE_ID_FIELD, Long.MIN_VALUE);
					dataSource.addData(newRecord);
				}
			});
			text.setSaveOnEnter(true);
			text.setImplicitSaveOnBlur(true);
			text.setShowHintInField(true);
			text.setHint("List name");
			text.setShowTitle(false);
			
			form.setFields(text);
			form.setSaveOnEnter(true);
			form.setImplicitSaveOnBlur(true);
			
			
			this.setMembers(form);
		}
	}
	
	public class TestDataSource extends DataSource {

		private static final String DS_UNIQUE_ID = "_TestDataSource";

		public static final String UNIQUE_ID_FIELD = "uniqueIdField" + DS_UNIQUE_ID;
		public static final String NAME_FIELD = "nameField" + DS_UNIQUE_ID;

		private TestDataSource() {

			DataSourceField uniqueId = new DataSourceField();
			uniqueId.setName(UNIQUE_ID_FIELD);
			uniqueId.setPrimaryKey(true);
			uniqueId.setHidden(true);
			uniqueId.setType(FieldType.INTEGER);

			DataSourceField name = new DataSourceField();
			name.setName(NAME_FIELD);

			ListGridRecord node1 = new ListGridRecord();
			node1.setAttribute(UNIQUE_ID_FIELD, 1l);
			node1.setAttribute(NAME_FIELD, "Charles Madigen");

			this.setID(DS_UNIQUE_ID);
			this.setClientOnly(true);
			this.setFields(uniqueId, name);
			this.setCacheData(new ListGridRecord[] { node1 });
		}
	}
}
