package com.ssdi.test.client;

import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.util.Offline;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.FieldStateChangedEvent;
import com.smartgwt.client.widgets.grid.events.FieldStateChangedHandler;

public abstract class DBListgrid extends ListGrid {

	/**
	 * add list grid fields
	 */
	public abstract void addListGridField();

	/**
	 * unique id per list grid and used for storing and retrieving list grid
	 * column state
	 */
	public abstract String getListGridId();
	

	public DBListgrid() {
		setListGridProperties();
	}

	/**
	 * set basic properties
	 */
	private void setListGridProperties() {
		setID(getListGridId());		
		setShowEmptyMessage(true);
		setEmptyMessage("No ItemsToShow");
		addListGridField();
		applyFieldState();

	}


	/**
	 * apply list grid column state
	 */
	public void applyFieldState() {
		try {

			final String viewState = (String) Offline.get(getId());
			if (viewState != null) {
				addDrawHandler(new DrawHandler() {
					@Override
					public void onDraw(DrawEvent event) {
						// restore any previously saved view state for this grid
						setViewState(viewState);
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		FieldStateChangedHandler fieldStateChangedHandler = new FieldStateChangedHandler() {

			@Override
			public void onFieldStateChanged(FieldStateChangedEvent event) {
				Offline.put(getId(), getViewState());				
			}
		};
		addFieldStateChangedHandler(fieldStateChangedHandler);
	}

	private String getId() {
		return getListGridId();
	}

	
}
