package org.example.nestedGrid.client;

import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceDateTimeField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.types.DateDisplayFormat;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.DateTimeItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class NestedGrid implements EntryPoint {
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		VLayout panel = new VLayout (15);
		
		RootPanel.getBodyElement().removeChild(RootPanel.get("loadingWrapper").getElement());
		
		DataSource ds = new DataSource ();
		ds.setID("sample1");
		DataSourceDateTimeField dateTimeField = new DataSourceDateTimeField ("dateTimeField","Date Time");
		dateTimeField.setDisplayFormat(DateDisplayFormat.TOSERIALIZEABLEDATE);
		DataSourceIntegerField id = new DataSourceIntegerField ("id");
		id.setPrimaryKey(true);
		id.setHidden(true);
		ds.setFields(dateTimeField,id);
		ds.setClientOnly(true);
		
		ListGridRecord data = new ListGridRecord ();
		data.setAttribute("dateTimeField", new Date ());
		data.setAttribute("id", 1);
		
		ds.addData(data);
		
		final ListGrid table = new ListGrid ();
		table.setHeight(200);
		table.setWidth(200);
		table.setDataSource(ds);
		table.setAutoFetchData(true);
		
		ds.fetchData(null, new DSCallback () {

			@Override
			public void execute(DSResponse response, Object rawData,
					DSRequest request) {
				table.setData(response.getData());
			}
			
		});
		
		DynamicForm form = new DynamicForm ();
		DateTimeItem date = new DateTimeItem ();
		date.setDateFormatter(DateDisplayFormat.TOSERIALIZEABLEDATE);
		date.setValue(new Date ());
		form.setItems(date);
		
		panel.addMember(table);
		panel.addMember(form);

        RootPanel.get("gui").add(panel);

	}
}
