package com.nolmecolindor.sgweet.test.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.core.Function;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.events.DataChangedEvent;
import com.smartgwt.client.data.events.DataChangedHandler;
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 DataChangerTest 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.getResultSet().addDataChangedHandler(new DataChangedHandler() {
				@Override public void onDataChanged(DataChangedEvent event) { SC.logWarn("Data changed."); }
			});
		}
	}); }};

	private final IButton step2Button = new IButton("execute test step 2") {{ addClickHandler(new ClickHandler() {
		{ setAutoFit(true); }
		@Override public void onClick(ClickEvent event) {
			grid.scrollToRow(500);
		}
	}); }};
	
	@Override public void onModuleLoad() {
		DataSource.load("fetchtest", new Function() { 
			@Override public void execute() { init(); }
		}, false);
	}
	
	private void init() {
		grid.setWidth(500);
		grid.setHeight(300);
		grid.setDataSource(DataSource.get("fetchtest"));
		grid.fetchData();
		addMember(grid);
		addMember(consoleButton);
		addMember(step1Button);
		addMember(step2Button);
		draw();
	}

}
