package com.test.myproject.client;

/*
 * Smart GWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * Smart GWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.  Smart GWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.AutoFitWidthApproach;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.util.KeyCallback;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
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.grid.HeaderSpan;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class ListGridExample implements EntryPoint {

	private ListGrid countryGrid1;

	@SuppressWarnings("deprecation")
	public void onModuleLoad() {

		if (!GWT.isScript()) {
		    KeyIdentifier debugKey = new KeyIdentifier();
		    debugKey.setCtrlKey(true);
		    debugKey.setKeyName("D");

		    Page.registerKey(debugKey, new KeyCallback() {
		        public void execute(String keyName) {
		            SC.showConsole();
		        }
		    });
		}
		
		Window.enableScrolling(false);
		Window.setMargin("0px");

		VLayout vlMainLayout = new VLayout();
		vlMainLayout.setWidth100();
		vlMainLayout.setHeight100();

		vlMainLayout.setMembersMargin(20);

		Button button1 = new Button("Show Records");
		button1.setWidth(250);
		button1.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				countryGrid1.setData(CountrySampleData.getLargeRecords());
			}
		});

		Button buttonClear = new Button("Clear");
		buttonClear.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				countryGrid1.setData(new Record[0]);

			}
		});

		HLayout hLayout = new HLayout(10);
		hLayout.setMembersMargin(10);
		hLayout.setPadding(10);
		hLayout.setAutoHeight();
		hLayout.addMember(button1);
		hLayout.addMember(buttonClear);
		vlMainLayout.addMember(hLayout);
		vlMainLayout.addMember(getGridWithBoldFontHeader());

		vlMainLayout.draw();      


	}

	private Canvas getGridWithBoldFontHeader(){

		VLayout vlGroup = new VLayout();
		vlGroup.setID("vlbold");
		vlGroup.setIsGroup(true);
		vlGroup.setPadding(20);
		vlGroup.setHeight100();
		vlGroup.setGroupTitle("Grid");

		countryGrid1 = new ListGrid();

		setListGrid(countryGrid1, "BoldHeaderGrid");

		ListGridField lgfMTDesc1502 = new ListGridField("countryName","Name");


		ListGridField lgfMTDesc1503 = new ListGridField("capital","Capital");

		ListGridField lgfMTDesc1501 = new ListGridField("countryCode","Code");

		ListGridField lgfMTDesc1504 = new ListGridField("area", "Area");

		ListGridField lgfMTDesc1505 = new ListGridField("population");

		ListGridField lgfMTcontinent = new ListGridField("continent");
		ListGridField lgfMTgdp = new ListGridField("gdp");
		ListGridField lgfMTindependence = new ListGridField("independence");
		ListGridField lgfMTgovernment = new ListGridField("government");
		ListGridField lgfMTgovernment_desc = new ListGridField("government_desc");
		ListGridField lgfMTmember_g8 = new ListGridField("member_g8");
		ListGridField lgfMTarticle = new ListGridField("article");
		ListGridField lgfMTbackground = new ListGridField("background");

		HeaderSpan hsMTRoute =   new HeaderSpan("Country -- Code -- Area", new String[]{"countryCode",
		"area"});

		countryGrid1.setHeaderSpans(hsMTRoute);
		countryGrid1.setFields(lgfMTDesc1503, lgfMTDesc1501,  lgfMTDesc1504,lgfMTDesc1502, lgfMTDesc1505,
				lgfMTcontinent, lgfMTgdp, lgfMTindependence,lgfMTgovernment,  lgfMTgovernment_desc,
				lgfMTmember_g8, lgfMTarticle, lgfMTbackground);
		countryGrid1.draw();



		vlGroup.addMember(countryGrid1);

		return vlGroup;

	}


	public static void setListGrid(ListGrid listGrid, String componentID){

		if(componentID != null){
			listGrid.setID(componentID);	
		}

		listGrid.setWidth100();  
		listGrid.setHeight100();		
		listGrid.setAlternateRecordStyles(true);
		listGrid.setBorder("1px solid #A7ABB4");
		listGrid.setShowHeaderContextMenu(false);
		listGrid.setShowEmptyMessage(false);
		listGrid.setEmptyMessage("");		
		listGrid.setBodyOverflow(Overflow.SCROLL);
		listGrid.setLeaveScrollbarGap(true);
		listGrid.setCanSort(false);
		listGrid.setCanResizeFields(true);	
		listGrid.setFastCellUpdates(true);
		// Setting this to false disables keyboard scrolling on all browsers with the exception of MSIE 
		//listGrid.setCanFocus(false);
		listGrid.setShowRollOver(false);
		listGrid.setAutoFitFieldsFillViewport(false);
		listGrid.setSelectionType(SelectionStyle.SINGLE);
		listGrid.setCellPadding(5);
		// this is required so that when a column is sized so small that its contents are clipped, the cell value will be displayed
		// when the user hovers over that cell
		listGrid.setCanHover(null);
		listGrid.setShowClippedValuesOnHover(true);
		//listGrid.setAutoSizeHeaderSpans(true);

		// the next 4 options can potentially cause a performance issue

		// if a developer needs to not auto size the grid, they should override these next 2 values
		listGrid.setAutoFitFieldWidths(true);
		listGrid.setAutoFitWidthApproach(AutoFitWidthApproach.BOTH);

		// This is added to avoid the lag that we see when scrolling through many records.
		// Automatically sets 
		// listGrid.setShowAllColumns(boolean);
		// listGrid.setShowAllRecords(boolean);
		listGrid.setDrawAheadRatio(2);
		listGrid.setShowAllRecords(false);
		listGrid.setShowAllColumns(false);

		//the next 3 values prevents the display of the context menu on header spans 
		listGrid.setShowHeaderSpanTitlesInSortEditor(false);
		listGrid.setCanFreezeFields(false);
		listGrid.setCanPickFields(false);
		listGrid.setCanPickOmittedFields(false);
	}
}
