package com.pci.sample.smartgwtbugreport.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;

public class AppEntryPoint implements EntryPoint
{
	private static final ListGridRecord[] RECORDS = { makeRecord(1, "test"), makeRecord(0, "test2"), makeRecord(2, "test2"), makeRecord(4, "test4") };

	@Override
	public void onModuleLoad()
	{
		VLayout layout = new VLayout();
		RootPanel.get().add(layout);

		DataSource dataSource = new DataSource();
		dataSource.setClientOnly(true);
		dataSource.setTestData(RECORDS);
		DataSourceField sequenceField = new DataSourceTextField("id", "ID");
		// XXX 	sequenceField.setRequired(true);
		DataSourceTextField textField = new DataSourceTextField("textField", "Text Field");
		dataSource.setFields(sequenceField, textField);
		ListGrid listGrid = new ListGrid(dataSource);
		listGrid.setSize("500", "200");
		listGrid.setCanEdit(true);
		listGrid.setAutoFetchData(true);
		listGrid.setUseCopyPasteShortcuts(true);
		listGrid.setCanDragSelect(true);
		listGrid.setCanSelectCells(true);
		layout.addMember(listGrid);

		Button button = new Button("add");
		button.addClickHandler(h -> listGrid.startEditingNew());
		layout.addMember(button);
	}

	private static final ListGridRecord makeRecord(int id, String textField)
	{
		ListGridRecord r = new ListGridRecord();
		r.setAttribute("id", id);
		r.setAttribute("textField", textField);
		return r;
	}
}