package com.ssdi.test.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.util.Offline;
import com.smartgwt.client.widgets.Canvas;
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.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.FieldStateChangedEvent;
import com.smartgwt.client.widgets.grid.events.FieldStateChangedHandler;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;

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

	ListGrid changeLogGrid;
	public void onModuleLoad() {

		Canvas mainCanvas = new Canvas(); 
		mainCanvas.setHeight100();
		mainCanvas.setWidth100();
		mainCanvas.setMargin(25);
		mainCanvas.setAlign(Alignment.CENTER);
		
		mainCanvas.addChild(DisplayInfo());
		mainCanvas.show();

	}

	private Canvas DisplayInfo() {

		final Canvas listGridCanvas = new Canvas();

		
		// / Vertical Layout
		VLayout verticalLayout = new VLayout();
		verticalLayout.setHeight100();
		verticalLayout.setWidth100();
		verticalLayout.setMinWidth(540);
		verticalLayout.setMinHeight(250);
		verticalLayout.setMembersMargin(5);
		verticalLayout.setMargin(5);

		
		// / Main Section Stack
		SectionStack mainSectionStack = new SectionStack();
		mainSectionStack.setWidth100();
		mainSectionStack.setHeight100();
		mainSectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);
		mainSectionStack.setOverflow(Overflow.VISIBLE);

	
		// / Inner section stack
		SectionStackSection innerSectionStack = new SectionStackSection();
		innerSectionStack.setTitle("ChangeLogList");
		innerSectionStack.setExpanded(true);

		
		// / List Grid
		changeLogGrid = new ListGrid();

		ListGridField dateNTimeField = new ListGridField("Code");
		dateNTimeField.setWidth("25%");

		ListGridField modelNameField = new ListGridField("Country");
		modelNameField.setWidth("25%");

		ListGridField ipAddressField = new ListGridField("Capital");
		ipAddressField.setWidth("25%");

		ListGridField changeAvailField = new ListGridField("Government");
		changeAvailField.setWidth("25%");

		changeLogGrid.setFields(dateNTimeField, modelNameField, ipAddressField, changeAvailField);
		
		changeLogGrid.setCanAutoFitFields(false);
		changeLogGrid.setWidth("100%");
		changeLogGrid.setDrawAheadRatio(4);
		applyFieldState();
		
		changeLogGrid.addFieldStateChangedHandler(new FieldStateChangedHandler() {
			
			@Override
			public void onFieldStateChanged(FieldStateChangedEvent event) {
				Offline.put(getId(),changeLogGrid.getViewState());	
			}
		});
		innerSectionStack.addItem(changeLogGrid);
		mainSectionStack.setSections(innerSectionStack);
		
		ListGridRecord[] exRecords =new ListGridRecord[2];
		exRecords[0]=new ListGridRecord();
		exRecords[1]=new ListGridRecord();
			
		exRecords[0].setAttribute("Code", "91");
		exRecords[0].setAttribute("Country", "India");
		exRecords[0].setAttribute( "Capital", "Delhi");
		exRecords[0].setAttribute("Government", "ABC");
		
		exRecords[1].setAttribute("Code", "81");
		exRecords[1].setAttribute("Country","Japan");
		exRecords[1].setAttribute( "Capital", "Tokyo");
		exRecords[1].setAttribute("Government", "XYZ");
		
		changeLogGrid.setData(exRecords);

		verticalLayout.addMember(mainSectionStack);

		listGridCanvas.addChild(verticalLayout);
		listGridCanvas.show();
		
		return listGridCanvas;

	}
	
	private String getId() {
		return "ListGrid_ID1";
	}

	public void applyFieldState() {
		try {

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