package com.ca.donner.ui.client.components.toolbar;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.ResultSet;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.util.JSOHelper;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class picklistDemo extends Canvas
{

	private static class DemoDataSource extends DataSource
	{
		private ListGridRecord buildRecord(String rowId, String category, String description, String shortname){
			ListGridRecord rval = new ListGridRecord(JSOHelper.createObject());
			rval.setAttribute("unique_id", rowId);
			rval.setAttribute("category",category);
			rval.setAttribute("description",description);
			rval.setAttribute("name", shortname);
			return rval;
		}
		
		public DemoDataSource()
		{
			ListGridRecord demoData[] = new ListGridRecord[5];
			demoData[0] = buildRecord("0", "Demo One", "First record", "first");
			demoData[1] = buildRecord("1", "Demo One", "Second record", "second");
			demoData[2] = buildRecord("2", "Demo One", "Another record", "third");
			demoData[3] = buildRecord("A1", "Demo One", "Record with a different id format", "first ID diff");
			demoData[4] = buildRecord("A2", "Demo Two", "record in a different category", "category diff");
			
			DataSourceTextField uid = new DataSourceTextField("unique_id", "ID");
			uid.setPrimaryKey(true);
			DataSourceTextField category = new DataSourceTextField("category","Category");
			DataSourceTextField desc = new DataSourceTextField("description","Description");
			DataSourceTextField name = new DataSourceTextField("name","Name");
			setFields(uid,category,desc,name);
			
			setClientOnly(true);
			setCacheData(demoData);
		}
	}

	public picklistDemo()
	{
		
		ListGrid pickListProperties = getPickListProperties();				
		ComboBoxItem combobox = new ComboBoxItem("name");
		combobox.setPickListProperties(pickListProperties);
		combobox.setOptionDataSource(new DemoDataSource());
		ListGridField idField = new ListGridField("unique_id");
		idField.setHidden(true);
		idField.setCanHide(false);
		ListGridField categoryField = new ListGridField("category","Category");
		categoryField.setWidth(60);
		ListGridField descriptionField = new ListGridField("description","Description");
		ListGridField nameField = new ListGridField("name","Name");
		nameField.setWidth(70);
		combobox.setPickListFields(idField,categoryField,descriptionField,nameField);
		combobox.setPickListWidth(300);
		combobox.setValueField("unique_id");
		combobox.setDisplayField("name");
		DynamicForm form = new DynamicForm();
		form.setItems(combobox);
		addChild(form);
	}
	
	private ListGrid getPickListProperties()
	{
		ListGrid pickListProperties = new ListGrid();
		pickListProperties.setShowHeaderContextMenu(true);
		pickListProperties.setShowHeader(true);
		pickListProperties.setModalEditing(true);
		pickListProperties.setCanDrag(false);
		pickListProperties.setAlternateRecordStyles(false);
		pickListProperties.setDrawAllMaxCells(800);
		pickListProperties.setDataPageSize(100);
		pickListProperties.setDrawAheadRatio(3);
		ResultSet dataProperties = new ResultSet();
		dataProperties.setUseClientFiltering(true);
		pickListProperties.setDataProperties(dataProperties);
		return pickListProperties;
	}
	
}
