package com.example.client;

import java.util.LinkedHashMap;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceEnumField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.MultipleAppearance;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
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.SelectItem;
import com.smartgwt.client.widgets.layout.VLayout;

public class MultiSelectItem implements EntryPoint {

	@Override
	public void onModuleLoad() {
		
		
		DataSource ds=new DataSource();
		
		DataSourceTextField name=new DataSourceTextField("UNAME","User Name");
		DataSourceTextField pwd=new DataSourceTextField("Pwd","Password");
		DataSourceTextField email=new DataSourceTextField("Email","Email");
		
		DataSourceEnumField dsField=new DataSourceEnumField("ITEMS","Items");
		LinkedHashMap<String,String> map=new LinkedHashMap<String, String>();
		map.put("ITEM1", "ITEM1");
		map.put("ITEM2", "ITEM2");
		map.put("ITEM3", "ITEM3");
		map.put("ITEM4", "ITEM4");
		
		dsField.setMultiple(true);
		dsField.setValueMap(map);
		ds.setFields(name,pwd,email,dsField);
	
		VLayout vLayout=new VLayout();
		
		
		final DynamicForm dForm=new DynamicForm();
		dForm.setNumCols(4);
		dForm.setDataSource(ds);
		
		SelectItem selItem=new SelectItem();
		selItem.setMultiple(true);
		selItem.setMultipleAppearance(MultipleAppearance.PICKLIST);
		selItem.setStartRow(true);
		
		dForm.getField("ITEMS").setEditorType(selItem);
		//dForm.getField("ITEMS").setStartRow(true);
		
		final Label textItem=new Label();
		textItem.setTitle("Selected values");
		
		IButton submitItem=new IButton("Submit");
		
		submitItem.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				dForm.submit();
			}
		});
		
		/*dForm.setItems(selItem,submitItem);*/
		
		dForm.addSubmitValuesHandler(new SubmitValuesHandler() {
			
			@Override
			public void onSubmitValues(SubmitValuesEvent event) {
				boolean flag=dForm.validate();
				
				textItem.setContents(""+dForm.getValues());
				//SC.say("VAlues: "+dForm.getValues());
				
			}
		});
		
		
		vLayout.addMember(dForm);
		vLayout.addMember(textItem);
		vLayout.addMember(submitItem);
		
		vLayout.setHeight(100);
		vLayout.setWidth(100);
		vLayout.draw();
	}

}
