package com.dsoft.testcase.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.util.JSON;
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.grid.ListGrid;
import com.smartgwt.client.widgets.layout.VLayout;

public class Main implements EntryPoint {

    private Criteria filter;
    private String sFilter;

    public void onModuleLoad() {

	final ListGrid countryGrid = new ListGrid();
	countryGrid.setWidth100();
	countryGrid.setHeight100();
	countryGrid.setDataSource(DataSource.get("worldDS"));
	countryGrid.setAutoFetchData(true);
	countryGrid.setShowFilterEditor(true);
	countryGrid.setFilterOnKeypress(true);

	final IButton save = new IButton("save");
	save.addClickHandler(new ClickHandler() {
	    @Override
	    public void onClick(ClickEvent event) {
		filter = countryGrid.getCriteria();
		sFilter = JSON.encode(countryGrid.getCriteria().getJsObj());
	    }
	});

	final IButton load = new IButton("load");
	load.addClickHandler(new ClickHandler() {
	    @Override
	    public void onClick(ClickEvent event) {
		countryGrid.setCriteria(filter);
	    }
	});

	final IButton loadJSON = new IButton("loadJSON");
	loadJSON.addClickHandler(new ClickHandler() {
	    @Override
	    public void onClick(ClickEvent event) {
		countryGrid.setCriteria(new Criteria(JSON.decode(sFilter)));
	    }
	});

	VLayout testLayout = new VLayout(10);
	testLayout.setWidth100();
	testLayout.setHeight100();

	testLayout.setMembers(save, load, loadJSON, countryGrid);

	testLayout.draw();

    }

}