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.types.MultipleAppearance;
import com.smartgwt.client.widgets.Label;
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.form.fields.SubmitItem;
import com.smartgwt.client.widgets.layout.VLayout;

public class MultiSelectItem implements EntryPoint {

	@Override
	public void onModuleLoad() {
		
		
		DataSource ds=new DataSource();
		
		DataSourceEnumField dsField=new DataSourceEnumField("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(dsField);
	
		VLayout vLayout=new VLayout();
		
		
		final DynamicForm dForm=new DynamicForm();
		dForm.setDataSource(ds);
		SelectItem selItem=new SelectItem("ITEMS", "Select Items");
		selItem.setMultiple(true);
		selItem.setMultipleAppearance(MultipleAppearance.PICKLIST);
		
		final Label textItem=new Label();
		textItem.setTitle("Selected values");
		
		SubmitItem submitItem=new SubmitItem();
		
		
		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.setHeight(100);
		vLayout.setWidth(100);
		vLayout.draw();
	}

}
