package de.usu.issue.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class SelectItemDSIssue implements EntryPoint {  

	public void onModuleLoad() {  

		final DynamicForm form = new DynamicForm();  
		form.setWidth(250);  

		// SelectItem with ValueMap
		SelectItem cbItem = new SelectItem();  
		cbItem.setTitle("ValueMap");
		cbItem.setValueMap("test1", "test2", "test3");  
		cbItem.setValue("aaaa");
		
		// SelectItem with DataSource
		SelectItem selectItemDS = new SelectItem();
		selectItemDS.setTitle("DataSource");
		selectItemDS.setDisplayField("displayValue");
		selectItemDS.setValueField("value");
		
		ListGridRecord[] tmpData = new ListGridRecord[]  {
				new ListGridRecord() {
					{
						setAttribute("value", "test1");
						setAttribute("displayValue", "test1");
					}
				},
				new ListGridRecord() {
					{
						setAttribute("value", "test2");
						setAttribute("displayValue", "test2");
					}
				},
				new ListGridRecord() {
					{
						setAttribute("value", "test3");
						setAttribute("displayValue", "test3");
					}
				}
		};
		
		DataSource tmpDS = createValuesetDS();
		tmpDS.setCacheData(tmpData);
		selectItemDS.setOptionDataSource(tmpDS);
		selectItemDS.setValue("aaaa");
		
		
		form.setFields(cbItem, selectItemDS);  
		form.draw();  
	}
	
	/**
	 * Creates data source for value set FormItem.
	 * 	-	two fields ("value", "displayValue") 
	 */
	private static DataSource createValuesetDS() {
		DataSourceField[] tmpFields = new DataSourceField[2];
		tmpFields[0] = new DataSourceField();
		tmpFields[0].setType(com.smartgwt.client.types.FieldType.TEXT);
		tmpFields[0].setPrimaryKey(true);
		tmpFields[0].setName("value");

		tmpFields[1] = new DataSourceField();
		tmpFields[1].setType(com.smartgwt.client.types.FieldType.TEXT);
		tmpFields[1].setName("displayValue");

		DataSource tmpDS = new DataSource();
		tmpDS.setFields(tmpFields);
		tmpDS.setClientOnly(true);

		return tmpDS;
	}
	
}  

