package web.gwt.client;

/* 
 * Smart GWT (GWT for SmartClient) 
 * Copyright 2008 and beyond, Isomorphic Software, Inc. 
 * 
 * Smart GWT is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License version 3 
 * as published by the Free Software Foundation.  Smart GWT is also 
 * available under typical commercial license terms - see 
 * http://smartclient.com/license 
 * 
 * This software is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * Lesser General Public License for more details. 
 */  
  
import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.OperationBinding;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.types.ListGridFieldType;
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.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.VStack;
  
public class Main11 implements EntryPoint {
	
	public static class RestDataSource extends com.smartgwt.client.data.RestDataSource {
		public RestDataSource(final String fetchUrl, final String restUrl) {
			this.setAttribute("disableQueuing", true, true);
			setShowPrompt(false);
			setDataFormat(DSDataFormat.JSON);
			setDataProtocol(DSProtocol.POSTXML);
			OperationBinding[] bindings = new OperationBinding[4];
			if(fetchUrl != null){
				bindings[0] = new OperationBinding(){{
						setOperationType(DSOperationType.FETCH);
						setDataURL(fetchUrl);
					}};
			}
			if(restUrl != null){
				bindings[1] = new OperationBinding(){{
					setOperationType(DSOperationType.ADD);
					setDataURL(restUrl);
				}};
				bindings[2] = new OperationBinding(){{
					setOperationType(DSOperationType.UPDATE);
					setDataURL(restUrl);
				}};
				bindings[3] = new OperationBinding(){{
					setOperationType(DSOperationType.REMOVE);
					setDataURL(restUrl);
				}};
			}
			setOperationBindings(bindings);
		}
	}
  
    @Override
	public void onModuleLoad() {  
  
    	RestDataSource dataSource = new RestDataSource("test", null) {{  		
    		DataSourceTextField countryNameField = new DataSourceTextField("countryName", "Country");
    		DataSourceTextField continentField = new DataSourceTextField("continent", "Continent");
    		continentField.setValueMap("Europe", "Asia", "North America", "Australia/Oceania", "South America", "Africa");
    		continentField.setMultiple(true);
    		DataSourceIntegerField populationField = new DataSourceIntegerField("population", "Population");
    		setFields(countryNameField, continentField, populationField);
    	}};

        final FilterBuilder filterBuilder = new FilterBuilder();
        filterBuilder.setAllowExternalFilters(true);
        filterBuilder.setDataSource(dataSource);

        final ListGrid countryGrid = new ListGrid();
        countryGrid.setAllowFilterExpressions(true);
        countryGrid.setWidth(550);
        countryGrid.setHeight(224);
		countryGrid.setDataSource(dataSource);
        countryGrid.setAutoFetchData(true);
        countryGrid.setShowFilterEditor(true);
        countryGrid.setFilterOnKeypress(true);

        ListGridField nameField = new ListGridField("countryName", "Country");
        ListGridField continentField = new ListGridField("continent", "Continent");
        ListGridField populationField = new ListGridField("population", "Population");
        populationField.setType(ListGridFieldType.INTEGER);
        countryGrid.setFields(nameField,continentField, populationField);

        IButton filterButton = new IButton("Filter");
        filterButton.addClickHandler(new ClickHandler() {
            @Override
			public void onClick(ClickEvent event) {
            	countryGrid.fetchData(filterBuilder.getCriteria().asAdvancedCriteria());
            	countryGrid.setCriteria(filterBuilder.getCriteria().asAdvancedCriteria());
            }
        });

        VStack vStack = new VStack(10);
        vStack.addMember(filterBuilder);
        vStack.addMember(filterButton);
        vStack.addMember(countryGrid);
        vStack.draw();
    }
}  