package com.nolmecolindor.sgweet.test.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.core.Function;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.TextMatchStyle;
import com.smartgwt.client.util.SC;
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 SurplussFetches extends VLayout implements EntryPoint {

	final ListGrid grid = new ListGrid();

	private final IButton consoleButton = new IButton("show console") {{ addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) {
		SC.showConsole();
	}}); }};
	
	private final IButton step1Button = new IButton("execute test step 1") {{ addClickHandler(new ClickHandler() { 
		{ setAutoFit(true); }
		@Override public void onClick(ClickEvent event) {
			grid.scrollToRow(40);
		}
	}); }};
	
	private final IButton step2Button = new IButton("execute test step 2") {{ addClickHandler(new ClickHandler() {
		{ setAutoFit(true); }
		@Override public void onClick(ClickEvent event) {
			final DSRequest req = new DSRequest();
			req.setTextMatchStyle(TextMatchStyle.SUBSTRING);
			grid.fetchData(new Criteria("item2_name", "23"), null, req);
		}
	}); }};
	
	@Override public void onModuleLoad() {
		grid.setShowFilterEditor(true);
		grid.setWidth(500);
		grid.setHeight(300);
		grid.setDrawAllMaxCells(0);
		DataSource.load("fetchtest", new Function() { 
			@Override public void execute() {
				grid.setDataSource(DataSource.get("fetchtest"));
				grid.fetchData();
			}
		}, false);

		addMember(consoleButton);
		addMember(step1Button);
		addMember(step2Button);
		addMember(grid);

		draw();
	}
	

}
