package com.arplatia.example.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
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.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.VLayout;

public class SmartGWTTest extends VLayout implements EntryPoint {
	public void onModuleLoad() {
		ListGridField id = new ListGridField("id");
		ListGridField name = new ListGridField("name");
		ListGridField count = new ListGridField("count");

		final ListGrid listGrid = new ListGrid(new TestDS());
		listGrid.setShowFilterEditor(true);
		listGrid.setWidth(250);
		listGrid.setHeight(100);
		listGrid.setFields(id, name, count);

		final Button button = new Button("Add data");
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Record record1 = new Record();
				record1.setAttribute("id", 1);
				record1.setAttribute("name", "Foo");
				record1.setAttribute("count", 1000);

				Record record2 = new Record();
				record2.setAttribute("id", 2);
				record2.setAttribute("name", "Bar");
				record2.setAttribute("count", 2000);

				listGrid.addData(record1);
				listGrid.addData(record2);

				button.disable();
			}
		});

		addMember(listGrid);
		addMember(button);

		draw();
	}

	private class TestDS extends DataSource {
		public TestDS() {
			setClientOnly(true);

			DataSourceIntegerField id = new DataSourceIntegerField("id", "Id");
			id.setPrimaryKey(true);

			DataSourceTextField name = new DataSourceTextField("name", "Name");

			DataSourceIntegerField count = new DataSourceIntegerField("count", "Count");

			setFields(id, name, count);
		}
	}
}
