package com.mstar.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.OperationBinding;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.util.SC;
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.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Rds_test implements EntryPoint {
	
	public void onModuleLoad() {
		VLayout panel = new VLayout();
		panel.setSize("100%", "100%");

		// set up datasource
		final RestDataSource ds = new RestDataSource();
		ds.setFetchDataURL("fetch.xml");
		OperationBinding op_fetch = new OperationBinding();
		op_fetch.setOperationType(DSOperationType.FETCH);
		op_fetch.setDataProtocol(DSProtocol.POSTMESSAGE);
		OperationBinding op_add = new OperationBinding();
		op_add.setOperationType(DSOperationType.ADD);
		op_add.setDataProtocol(DSProtocol.POSTMESSAGE);
		OperationBinding op_update = new OperationBinding();
		op_update.setOperationType(DSOperationType.UPDATE);
		op_update.setDataProtocol(DSProtocol.POSTMESSAGE);
		OperationBinding op_remove = new OperationBinding();
		op_remove.setOperationType(DSOperationType.REMOVE);
		op_remove.setDataProtocol(DSProtocol.POSTMESSAGE);
		ds.setOperationBindings(new OperationBinding[] {op_fetch, op_add, op_update, op_remove});
		
		DataSourceField f1 = new DataSourceField("pk", FieldType.INTEGER, "Primary key");
		f1.setPrimaryKey(true);
		f1.setCanEdit(false);
		DataSourceField f2 = new DataSourceField("field", FieldType.TEXT, "Field");
		ds.setFields(f1, f2);

		//set up grid
		final ListGrid grid = new ListGrid();
		grid.setUseAllDataSourceFields(true);
		grid.setAutoSaveEdits(false);
		grid.setCanEdit(true);
		grid.setSelectionType(SelectionStyle.SINGLE);
		grid.setDataSource(ds);
		grid.addSelectionChangedHandler(new SelectionChangedHandler() {
			public void onSelectionChanged(SelectionEvent event) {
				SC.say("select record pk=" + event.getRecord().getAttribute("pk"));
			}
		});
		final DSCallback cb = new DSCallback() {
			public void execute(DSResponse response, Object rawData,
					DSRequest request) {
				grid.selectSingleRecord(0);
			}
		};
		grid.fetchData(null, cb);
		
		panel.addMember(grid);
		
		Button b0 = new Button("refresh");
		b0.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				grid.getResultSet().invalidateCache();
				grid.fetchData(null, cb);
			}
		});
		panel.addMember(b0);

		Button b3 = new Button("select 2 row");
		b3.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				grid.selectSingleRecord(1);
			}
		});
		panel.addMember(b3);
		
		panel.draw();	}
}
