package com.smartgwt.sample.client;

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.FormItemCriteriaFunction;
import com.smartgwt.client.widgets.form.fields.FormItemFunctionContext;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;

public class MyTab extends Tab {
	
	public MyTab() {
		setCanClose(true);
		setTitle("title");
		setPane(createPane());
	}

	private Canvas createPane() {
  
        final DynamicForm form = new DynamicForm();
		form.setHeight100();
		
		final SelectItem countryitem = new SelectItem("country", "Country1");
		countryitem.setOptionDataSource(CountryClientOnlyDS.getInstance());
		countryitem.setValueField("id");
		countryitem.setDisplayField("displayText");
    	SelectItem cityitem = new SelectItem("city", "City1");
    	cityitem.setPickListFilterCriteriaFunction(new FormItemCriteriaFunction(){
			@Override
			public Criteria getCriteria(FormItemFunctionContext itemContext) {
				Criteria criteria = new Criteria();
    			criteria.addCriteria("countryId", countryitem.getValueAsString());
                return criteria;
			}});
		cityitem.setOptionDataSource(DataSource.get("brancheFlagDS"));
		cityitem.setAlwaysFetchMissingValues(true);
		cityitem.setValueField("id");
		cityitem.setDisplayField("displayText");
		cityitem.setOptionOperationId("xxx");
		cityitem.setAutoFetchData(false);

		final SelectItem countryitem2 = new SelectItem("country", "Country2");
		countryitem2.setOptionDataSource(CountryClientOnlyDS.getInstance());
		countryitem2.setValueField("id");
		countryitem2.setDisplayField("displayText");
		final SelectItem cityitem2 = new SelectItem("city", "City2");
    	cityitem2.setPickListFilterCriteriaFunction(new FormItemCriteriaFunction(){
			@Override
			public Criteria getCriteria(FormItemFunctionContext itemContext) {
				Criteria criteria = new Criteria();
    			criteria.addCriteria("countryId", countryitem2.getValueAsString());
                return criteria;
			}});
		cityitem2.setOptionDataSource(DataSource.get("brancheFlagDS"));
		cityitem2.setAlwaysFetchMissingValues(true);
		cityitem2.setValueField("id");
		cityitem2.setDisplayField("displayText");
		cityitem2.setAutoFetchData(false);
		
		ButtonItem buttonItem = new ButtonItem();
		buttonItem.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				SC.say(cityitem2.getDisplayValue());
			}
		});
		form.setFields(countryitem, cityitem, countryitem2, cityitem2, buttonItem);
		VLayout layout = new VLayout();
		layout.addMember(form);  
  
        return layout;
	}

}
