package com.smartgwt.sample.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.types.TopOperatorAppearance;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.FilterBuilder;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.VStack;

public class SQLIncludeFrom implements EntryPoint {
    final ListGrid listGrid = new ListGrid();
    FilterBuilder filterBuilder;
    DataSource cityDS = null;
    public void onModuleLoad() {
    	cityDS = DataSource.get("cityIncludeFromSQL");
	    listGrid.setDataSource(cityDS);  
	    listGrid.setWidth(700);  
	    listGrid.setHeight(224);  
	    listGrid.setShowFilterEditor(true);  
	    listGrid.setAlternateRecordStyles(true);  
	    listGrid.setAutoFetchData(true);  
	    listGrid.setDataPageSize(50);  
	    listGrid.setCanEdit(true);  
	    listGrid.setEditEvent(ListGridEditEvent.CLICK);  
	    ListGridField countryField = new ListGridField("countryId");
	    FormItem countryFieldFilterProperties = new FormItem(){{
	    	setOptionDataSource(DataSource.get("countryIncludeFromSQL"));
	    	setDisplayField("countryName");
	    }};
	    countryField.setFilterEditorProperties(countryFieldFilterProperties);
	    ListGridField continentField = new ListGridField("continent");
	    FormItem continentFieldFilterProperties = new SelectItem(){{
	    	setOptionDataSource(DataSource.get("continentIncludeFromSQL"));
	    	setValueField("continentName");
	    	setDisplayField("continentName");
	    }};
	    continentField.setFilterEditorProperties(continentFieldFilterProperties);
	    listGrid.setFields(new ListGridField("cityName"),  
	    		new ListGridField("countryId"),
	    		new ListGridField("tstamp"),
	    		continentField
	    );  
//	    VLayout layout = new VLayout(15);  
//	    this.setMembersMargin(15);
//        addMember(setupFilterBuilder());
//	    this.addMember(listGrid);  
//	
//	    this.draw();  
	    
	    VLayout layout = new VLayout(15);  
        layout.addMember(listGrid);  
        layout.addMember(setupFilterBuilder());
        layout.draw(); 
    }

    private HStack setupFilterBuilder() {
		filterBuilder = new FilterBuilder();
		filterBuilder.setTopOperatorAppearance(TopOperatorAppearance.RADIO);
		filterBuilder.setShowSubClauseButton(false);
		filterBuilder.setAllowEmpty(true);
//		filterBuilder.setRetainValuesAcrossFields(false);

		//set canFilter to false for all fields...
		for (DataSourceField f : cityDS.getFields()) {
			f.setCanFilter(false);
			if ("countryId".equals(f.getName())) {
				FormItem theEditorType = new ComboBoxItem();
				theEditorType.setOptionDataSource(DataSource.get("countryIncludeFromSQL"));
				theEditorType.setValueField("countryId");
				theEditorType.setDisplayField("countryName");
				f.setEditorType(theEditorType);
			}
		}
		//...and then set canFilter true for the ones we actually want (ie: the ones in the listgrid)
		for (ListGridField f : listGrid.getFields()) {
			cityDS.getField(f.getName()).setCanFilter(true);
		}

//		Common.setTypeOperators(cityDS);
		
		filterBuilder.setDataSource(cityDS);
		
		IButton filterButton = new IButton("Apply Filter");
		filterButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				listGrid.filterData(filterBuilder.getCriteria());
			}
		});

		IButton clearFilterButton = new IButton("Clear Filter");
		clearFilterButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				listGrid.clearCriteria();
			}
		});

		VStack buttonStack = new VStack();
		buttonStack.setAutoHeight();
		buttonStack.setMembersMargin(3);
		buttonStack.addMember(filterButton);
		buttonStack.addMember(clearFilterButton);
		
		HStack filterStack = new HStack();
		filterStack.setAutoHeight();
		filterStack.setMembersMargin(10);
		filterStack.addMember(buttonStack);
		filterStack.addMember(filterBuilder);

		return filterStack;
	}


}
