/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.yournamehere.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

/**
 * Main entry point.
 *
 * @author parmstrong
 */
public class MainEntryPoint implements EntryPoint
{
	ListGrid countryGrid;
	
	/** 
	 * Creates a new instance of MainEntryPoint
	 */
	public MainEntryPoint()
	{
	}

	/** 
	 * The entry point method, called automatically by loading a module
	 * that declares an implementing class as an entry-point
	 */
	public void onModuleLoad()
	{		
		final Button button = new Button("Click me!");

		button.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				countryGrid.groupBy("capital", "continent");
				countryGrid.refreshFields();				
			}
		});

		RootPanel.get().add(button);
		//RootPanel.get().add(createChart());
		RootPanel.get().add(createGrid());
	}
	
	
	private ListGrid createGrid()
	{
	    countryGrid = new ListGrid();  
        countryGrid.setWidth(500);  
        countryGrid.setHeight(224);  
        countryGrid.setShowAllRecords(true); 
		countryGrid.setShowGroupTitleColumn(false);
		countryGrid.setShowGroupSummaryInHeader(true);
		countryGrid.setShowGroupSummary(true);
  
        ListGridField nameField = new ListGridField("countryName", "Country");		
        ListGridField capitalField = new ListGridField("capital", "Capital");  
        ListGridField continentField = new ListGridField("continent", "Continent");  
		ListGridField number = new ListGridField("number", "number");
		number.setType(ListGridFieldType.INTEGER);
		
		
		
        countryGrid.setFields(nameField, capitalField, continentField, number);  
        countryGrid.setCanResizeFields(true);  
		
		ListGridRecord[] records = new ListGridRecord[5];
		
		records[0] = getGridRow("USA", "Washington", "North America");
		records[1] = getGridRow("USA", "Washington", "North America");
		records[2] = getGridRow("USA", "Lamo", "North America");
		records[3] = getGridRow("USA", "Washinon", "North America");
		records[4] = getGridRow("USA", "Washington", "North Amera");		
		
		countryGrid.setData(records);	
		countryGrid.setGroupByField(nameField.getName());
		
		return countryGrid;
	}
	
	private ListGridRecord getGridRow(String countryName, String capital, String continent)
	{
		ListGridRecord listGridRecord = new ListGridRecord();
		listGridRecord.setAttribute("countryName", countryName);
		listGridRecord.setAttribute("capital", capital);
		listGridRecord.setAttribute("continent", continent);
		listGridRecord.setAttribute("number", 5);
		return listGridRecord;
	}
}
