// Description: 
// Isomorphic thread: 
// Resolution: 

package com.hp.isomorphic.client.issues;

import java.util.LinkedHashMap;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.hp.isomorphic.client.sandbox.controls.PMImageFormItem;
import com.smartgwt.client.data.AdvancedCriteria;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.OperatorId;
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.DataArrivedEvent;
import com.smartgwt.client.widgets.grid.events.DataArrivedHandler;
import com.smartgwt.client.widgets.layout.VLayout;

public class GridSelectionWithFilterUpdate extends VLayout {
	private ListGrid grid;
	
	public GridSelectionWithFilterUpdate() {
		grid = new ListGrid();
		
		ListGridField gridIdField = new ListGridField("idField", "ID Column", 32);
		gridIdField.setHeaderTitle("ID");
		gridIdField.setCanDragResize(false);
		gridIdField.setCanReorder(false);
		gridIdField.setType(ListGridFieldType.TEXT);
		
		ListGridField gridIconField = new ListGridField("iconField", "Icon Column", 32);
		gridIconField.setHeaderTitle("");
		gridIconField.setCanDragResize(false);
		gridIconField.setCanReorder(false);
		gridIconField.setType(ListGridFieldType.IMAGE);

		ListGridField textField = new ListGridField("textField", "Text Column", 100);

		grid.setFields(gridIdField, gridIconField, textField);
		
		final DataSource ds = new DataSource();

		DataSourceField dsIdField = new DataSourceField("idField", FieldType.TEXT, "ID");
		dsIdField.setPrimaryKey(true);
		
		DataSourceField dsIconField = new DataSourceField("iconField", FieldType.IMAGE, "Icon");
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("ERROR", "[SKINIMG]Product/status/ERROR_16.png");
		map.put("WARNING", "[SKINIMG]Product/status/WARNING_16.png");
		map.put("UNKNOWN", "[SKINIMG]Product/status/UNKNOWN_16.png");
		dsIconField.setValueMap(map);
		dsIconField.setValidOperators(OperatorId.EQUALS,
				OperatorId.NOT_EQUAL,
				OperatorId.IS_NULL,
				OperatorId.NOT_NULL);
		dsIconField.setEditorProperties(new PMImageFormItem("iconField", map));

		DataSourceField dsTextField = new DataSourceField("textField", FieldType.TEXT, "Text");
		ds.setFields(dsIdField, dsIconField, dsTextField);
		
		ListGridRecord[] records = new ListGridRecord[3];
		records[0] = new ListGridRecord();
		records[0].setAttribute("idField", "0");
		records[0].setAttribute("iconField", "ERROR");
		records[0].setAttribute("textField", "Content row 0");
		records[1] = new ListGridRecord();
		records[1].setAttribute("idField", "1");
		records[1].setAttribute("iconField", "WARNING");
		records[1].setAttribute("textField", "Content row 1");
		records[2] = new ListGridRecord();
		records[2].setAttribute("idField", "2");
		records[2].setAttribute("iconField", "UNKNOWN");
		records[2].setAttribute("textField", "Content row 2");
		ds.setCacheData(records);
		ds.setClientOnly(true);
		
		grid.setShowAllRecords(true);
		grid.setAutoFetchData(true);
		grid.setDataSource(ds);
		
		grid.addDataArrivedHandler(new DataArrivedHandler() {
			@Override
			public void onDataArrived(DataArrivedEvent event) {
				grid.selectRecord(0);
			}
		});
		
		AdvancedCriteria criteria = new AdvancedCriteria("textField", OperatorId.NOT_CONTAINS, "update");
		grid.setCriteria(criteria);

		addMember(grid);
		
		Button button = new Button("Update selected record");
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ListGridRecord record = grid.getSelectedRecord();
				if (record != null) {
					record.setAttribute("textField", record.getAttributeAsString("textField") + " - update");
					grid.updateData(record);
				}
			}
		});
		
		addMember(button);
	}
}
