package de.smartgwttest.client;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.ValuesManager;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
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.SortNormalizer;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;
import com.smartgwt.client.widgets.tree.TreeGrid;

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

	int count = 1;


	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		DataSource ds = new DataSource();
		ds.setID("myDS");
		DataSourceIntegerField pkField = new DataSourceIntegerField("pk");
		pkField.setPrimaryKey(true);
		DataSourceTextField textField = new DataSourceTextField("text");
		DataSourceTextField groupField = new DataSourceTextField("group");
		DataSourceIntegerField parentField = new DataSourceIntegerField("parent");
		parentField.setForeignKey("myDS.pk");
		ds.setFields(pkField, textField, groupField, parentField);
		ds.setClientOnly(true);

		ds.setTestData(new Record[] { createRecord(count++, "test1", "group", null), createRecord(count++, "test2", "group", 1),
				createRecord(count++, "test3", "group", 1) });


		ListGrid listGrid = new ListGrid();
		listGrid.setDataSource(ds);
		listGrid.setAutoFetchData(true);
		listGrid.setWidth100();
		listGrid.setHeight100();
		listGrid.setFields(new ListGridField("pk"), new ListGridField("text"), new ListGridField("group"));
		listGrid.setGroupByField("group");

		final TreeGrid treeGrid = new TreeGrid();
		treeGrid.setDataSource(ds);
		treeGrid.setAutoFetchData(true);
		treeGrid.setWidth100();
		treeGrid.setHeight100();
		
		treeGrid.setFields(new ListGridField("pk"), new ListGridField("text"));
		IButton addButton = new IButton();  
        addButton.setWidth(150);  
        addButton.setTitle("AddRecord");  
        addButton.addClickHandler(new ClickHandler() {  
            @Override  
            public void onClick(ClickEvent event) {
            	
            	treeGrid.addData(createRecord(count++, "test1", "group", 1));
              }  
        });
		VLayout layout = new VLayout(10);
		layout.setWidth100();
		layout.setHeight100();

		layout.addMember(listGrid);
		layout.addMember(treeGrid);
		layout.addMember(addButton);
		layout.draw();

	}
	
	private ListGridRecord createRecord(Integer pk, String value, String group, Integer parent) {
		ListGridRecord record = new ListGridRecord();
		record.setAttribute("pk", pk);
		record.setAttribute("text", value);
		record.setAttribute("group", group);
		record.setAttribute("parent", parent);
		return record;
	}
}
