package com.sbs.jpm.client;

import java.util.LinkedHashMap;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
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.fields.SelectItem;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;

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();

		RootPanel.get().add(panel);
	}

	public class Panel extends VLayout {

		private SelectItem responsibleSelectItem1, responsibleSelectItem2;

		public Panel() {

			responsibleSelectItem1 = new SelectItem();
			responsibleSelectItem1.setTitle("Responsible 1");
			responsibleSelectItem1.setName(TestDataSource.NAME_FIELD);
			responsibleSelectItem1.setOptionDataSource(dataSource);
			responsibleSelectItem1
					.setValueField(TestDataSource.UNIQUE_ID_FIELD);
			responsibleSelectItem1.setDisplayField(TestDataSource.NAME_FIELD);
			responsibleSelectItem1.setFilterLocally(true);
			responsibleSelectItem1.setFetchMissingValues(false);

			responsibleSelectItem2 = new SelectItem();
			responsibleSelectItem2.setTitle("Responsible 2");
			responsibleSelectItem2.setName(TestDataSource.NAME_FIELD);
			responsibleSelectItem2.setOptionDataSource(dataSource);
			responsibleSelectItem2
					.setValueField(TestDataSource.UNIQUE_ID_FIELD);
			responsibleSelectItem2.setDisplayField(TestDataSource.NAME_FIELD);
			responsibleSelectItem2.setFilterLocally(true);
			responsibleSelectItem2.setFetchMissingValues(false);
			LinkedHashMap<Long, String> hashMap = new LinkedHashMap<Long, String>();
			hashMap.put(-1l, "None");
			responsibleSelectItem2.setSpecialValues(hashMap);

			DynamicForm taskDetailsForm = new DynamicForm();
			taskDetailsForm.setDataSource(dataSource);
			taskDetailsForm.setFields(responsibleSelectItem1);

			DynamicForm taskDetailsForm2 = new DynamicForm();
			taskDetailsForm2.setDataSource(dataSource);
			taskDetailsForm2.setFields(responsibleSelectItem2);
			
			this.setMembers(taskDetailsForm, taskDetailsForm2);
		}
	}

	public class TestDataSource extends DataSource {

		private static final String DS_UNIQUE_ID = "_TaskDataSource";

		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);

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

			ListGridRecord node2 = new ListGridRecord();
			node2.setAttribute(UNIQUE_ID_FIELD, 3l);
			node2.setAttribute(NAME_FIELD, "Rogine Leger");

			ListGridRecord node3 = new ListGridRecord();
			node3.setAttribute(UNIQUE_ID_FIELD, 4l);
			node3.setAttribute(NAME_FIELD, "Gene Porter");

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