package gridtest.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceBooleanField;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceFloatField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.TopOperatorAppearance;
import com.smartgwt.client.widgets.Button;
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;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GridTest implements EntryPoint {
	
	DataSource worldDS = null;
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		String state = "({    selected:\"[\\r]\",     field:\"[\\r    {\\r        name:\"countryName\", \\r        width:null\\r    }, \\r    {\\r        name:\"member_g8\", \\r        width:null\\r    }, \\r    {\\r        name:\"population\", \\r        width:null\\r    }, \\r    {\\r        name:\"continent\", \\r        visible:false, \\r        width:null\\r    }, \\r    {\\r        name:\"independence\", \\r        width:80\r    }\\r]\",    sort:\"({\\r    fieldName:null, \\r    sortDir:true\\r})\"})";

		WorldXmlDS("test");
		  
        final FilterBuilder filterBuilder = new FilterBuilder();           
        filterBuilder.setDataSource(worldDS);   
        filterBuilder.setTopOperatorAppearance(TopOperatorAppearance.RADIO);   
  
        final ListGrid countryGrid = new ListGrid();   
        
        countryGrid.setAutoFetchData(true); 

        countryGrid.setWidth(550);   
        countryGrid.setHeight(224);   
        countryGrid.setAlternateRecordStyles(true);   
        countryGrid.setDataSource(worldDS);   
          
  
        ListGridField nameField2 = new ListGridField("countryName", "Country");   
        ListGridField continentField = new ListGridField("continent", "Continent");   
        ListGridField memberG8Field = new ListGridField("member_g8", "Member G8");   
        memberG8Field.setCanEdit(false);   
  
        ListGridField populationField = new ListGridField("population", "Population");   
        populationField.setType(ListGridFieldType.INTEGER);   
         
        ListGridField independenceField = new ListGridField("independence", "Independence");   
        countryGrid.setFields(nameField2,continentField, memberG8Field, populationField, independenceField);   

        System.out.println(state);

        countryGrid.setViewState(state);
        
        RootPanel.get("grid").add(filterBuilder);
        RootPanel.get("grid").add(countryGrid);
        
        Button button = new Button();
        button.setTitle("filter");
        
        button.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				System.out.println(countryGrid.getViewState());
			}
        });
        
        RootPanel.get("grid").add(button);
        countryGrid.setViewState(state);
        System.out.println(countryGrid.getViewState());
	}
	
	public void WorldXmlDS(String id) {   
		  
		worldDS = new DataSource();
		
		worldDS.setID(id);   
        DataSourceIntegerField pkField = new DataSourceIntegerField("pk");   
        pkField.setHidden(true);   
        pkField.setPrimaryKey(true);   
  
        DataSourceTextField countryCodeField = new DataSourceTextField("countryCode", "Code");   
        countryCodeField.setRequired(true);   
  
        DataSourceTextField countryNameField = new DataSourceTextField("countryName", "Country");   
        countryNameField.setRequired(true);   
  
        DataSourceTextField capitalField = new DataSourceTextField("capital", "Capital");   
        DataSourceTextField governmentField = new DataSourceTextField("government", "Government", 500);   
  
        DataSourceBooleanField memberG8Field = new DataSourceBooleanField("member_g8", "G8");   
  
        DataSourceTextField continentField = new DataSourceTextField("continent", "Continent");   
        continentField.setValueMap("Europe", "Asia", "North America", "Australia/Oceania", "South America", "Africa");   
  
        DataSourceDateField independenceField = new DataSourceDateField("independence", "Nationhood");   
        DataSourceFloatField areaField = new DataSourceFloatField("area", "Area (km&sup2;)");   
        DataSourceIntegerField populationField = new DataSourceIntegerField("population", "Population");   
        DataSourceFloatField gdpField = new DataSourceFloatField("gdp", "GDP ($M)");   
           
        worldDS.setFields(pkField, countryCodeField, countryNameField, capitalField, governmentField,   
                memberG8Field, continentField, independenceField, areaField, populationField,   
                gdpField);   
   
        worldDS.setClientOnly(true);   
    }   
}
