package com.ssdi.test.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.Offline;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
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.layout.SectionStack;
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
		Canvas mainCanvas = new Canvas();
		mainCanvas.setHeight100();
		mainCanvas.setWidth100();
		mainCanvas.setMargin(25);
		//mainCanvas.setAlign(Alignment.CENTER);

		//
		// Display Button
		Button btnShowButton = new Button();
		btnShowButton.setTitle("SHOW");

		btnShowButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {



				final Canvas mainCanvas = new Canvas();

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

				//
				// / Main Section Stack
				SectionStack mainSectionStack = new SectionStack();
				mainSectionStack.setWidth100();
				mainSectionStack.setHeight100();

				//
				/// List Grid
				changeLogGrid = new ListGrid();
				changeLogGrid.setCanAutoFitFields(false);
				changeLogGrid.setWidth("100%");
				changeLogGrid.setCanExpandRecords(true);

				// Add list grid fields
				ListGridField dateNTimeField = new ListGridField("ChangeConfirmTime");
				dateNTimeField.setWidth("25%");

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

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

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

				changeLogGrid.setFields(dateNTimeField, modelNameField, ipAddressField,
						changeAvailField);


				mainSectionStack.addChild(changeLogGrid);

				// Close Button
				Button btnCloseButton = new Button();
				btnCloseButton.setTitle("CLOSE");

				btnCloseButton.addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						String viewState = changeLogGrid.getViewState();
						Offline.put("exampleState", viewState);
						mainCanvas.hide();
					}

				});
				
				
				final String previouslySavedState = (String) Offline
						.get("exampleState");
				if (previouslySavedState != null) {
					changeLogGrid.addDrawHandler(new DrawHandler() {
						@Override
						public void onDraw(DrawEvent event) {
							// restore any previously saved view state for this grid
							changeLogGrid.setViewState(previouslySavedState);
						}
					});
				}
				
				//
				// Add main section stack to Vlayout
				verticalLayout.addMember(mainSectionStack);
				verticalLayout.addMember(btnCloseButton);

				//
				//  Add vlayout to canvas and show
				mainCanvas.addChild(verticalLayout);
				mainCanvas.show();			

			}
		});

		//
		// Display Canvas
		mainCanvas.addChild(btnShowButton);
		mainCanvas.show();

	}


}
