package com.test.client;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.types.RowEndEditAction;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GridTest implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		final ArrayList<Map<String, Integer>> mArrayList = new ArrayList<Map<String, Integer>>();
		Map<String, Integer> nMap = new LinkedHashMap<String, Integer>();
		nMap.put("a", 1);
		mArrayList.add(nMap);
		nMap.put("1a", 1);
		mArrayList.add(nMap);
		nMap.put("2a", 1);
		mArrayList.add(nMap);
		nMap.put("3a", 1);
		mArrayList.add(nMap);
		nMap.put("4a", 1);
		mArrayList.add(nMap);
		nMap.put("5a", 1);
		mArrayList.add(nMap);
		nMap.put("6a", 1);
		mArrayList.add(nMap);
		nMap.put("7a", 1);
		mArrayList.add(nMap);
		nMap.put("8a", 1);
		mArrayList.add(nMap);
		nMap.put("9a", 1);
		mArrayList.add(nMap);
		nMap.put("0a", 1);
		mArrayList.add(nMap);

		BaseDataSource gridDS = new BaseDataSource() {
			@Override
			protected void executeFetch(String requestId, DSRequest dsRequest,
					DSResponse response) {
				ArrayList<ListGridRecord> recordList = new ArrayList<ListGridRecord>();
				for (Map<String, Integer> aclMap : mArrayList) {
					System.out.println(aclMap);
					// if (aclMap.get("Name") != null) {
					ListGridRecord record = new ListGridRecord();
					record.setAttribute("Name", "dsada");
					record.setAttribute("Role", "sadsd");
					record.setAttribute("all", "asdasd");
					record.setAttribute("temp", "asdasd");
					record.setAttribute("temp2", "asdasd");
					record.setAttribute("temp1", "asdasd");
					recordList.add(record);
					// }
				}

				ListGridRecord[] records = new ListGridRecord[recordList.size()];
				recordList.toArray(records);
				response.setData(records);
				response.setTotalRows(records.length);
				processResponse(requestId, response);
			}
		};

		DataSourceField nameField = new DataSourceField("Name", FieldType.TEXT,
				"Name");
		nameField.setAttribute("width", "*");
		DataSourceField roleField = new DataSourceField("Role", FieldType.TEXT,
				"Role");
		roleField.setCanEdit(true);
		DataSourceField allField = new DataSourceField("all", FieldType.TEXT,
				"ALL");
		allField.setCanEdit(true);
		DataSourceField tempField = new DataSourceField("temp", FieldType.TEXT,
				"temp");
		tempField.setCanEdit(true);
		tempField.setAttribute("width", "*");
		DataSourceField tempField1 = new DataSourceField("temp1",
				FieldType.TEXT, "temp1");
		tempField1.setCanEdit(true);
		tempField1.setAttribute("width", "*");
		DataSourceField tempField2 = new DataSourceField("temp2",
				FieldType.TEXT, "temp2");
		tempField2.setCanEdit(true);
		tempField2.setAttribute("width", "*");

		gridDS.setFields(nameField, roleField, allField, tempField, tempField1,
				tempField2);

		ListGrid lg = new ListGrid();
		lg.setDataSource(gridDS);
		lg.setAutoFetchData(true);
		lg.setShowAllRecords(true);
		lg.setAlternateRecordStyles(true);
		lg.setMargin(5);
		lg.setWidth100();
		lg.setHeight(100);
		lg.setCanGroupBy(false);
		lg.setCanEdit(true);
		lg.setEditByCell(true);
		lg.setEditEvent(ListGridEditEvent.CLICK);
		lg.setShowRollOverCanvas(false);
		lg.setRowEndEditAction(RowEndEditAction.NEXT);
		lg.setListEndEditAction(RowEndEditAction.DONE);
		lg.setUseCellRollOvers(true);

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("nameFieldContainer").add(lg);
	}
}
