package my.package_name.client;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceFloatField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.events.DataArrivedEvent;
import com.smartgwt.client.widgets.tree.events.DataArrivedHandler;


public class Test extends HLayout implements EntryPoint
{
	public static final String XML_FILE= "ds/employees.data.xml";
	
	public void onModuleLoad()
	{
		RootPanel.get().add(getViewPanel());
	}

	public Canvas getViewPanel()
	{
		final TreeGrid treeGrid= new TreeGrid();
		treeGrid.setLoadDataOnDemand(false);
		treeGrid.setWidth(500);
		treeGrid.setHeight(400);
		treeGrid.setDataSource(EmployeeXmlDS.getInstance());
		treeGrid.setAutoFetchData(true);

		TreeGridField nameField= new TreeGridField("Name", 150);
		TreeGridField jobField= new TreeGridField("Job", 150);
		TreeGridField salaryField= new TreeGridField("Salary");

		treeGrid.setFields(nameField, jobField, salaryField);

		treeGrid.addDataArrivedHandler(new DataArrivedHandler() {
			public void onDataArrived(DataArrivedEvent event)
			{
				treeGrid.getData().openAll();
			}
		});

		Menu menu= new Menu();
		menu.setShowShadow(true);
		menu.setShadowDepth(10);
		MenuItem theItem= new MenuItem("theItem");
		theItem.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(MenuItemClickEvent event)
			{
				System.out.println("clicked");
			}
		});
		menu.setItems(theItem);

		treeGrid.setContextMenu(menu);

		return treeGrid;
	}
}


class EmployeeXmlDS extends DataSource
{
	private static EmployeeXmlDS instance= new EmployeeXmlDS("employeesDS");;

	public static EmployeeXmlDS getInstance()
	{
		return instance;
	}

	public EmployeeXmlDS(String id)
	{
		setID(id);
		setTitleField("Name");
		setRecordXPath("/List/employee");
		DataSourceTextField nameField= new DataSourceTextField("Name", "Name", 128);

		DataSourceIntegerField employeeIdField= new DataSourceIntegerField("EmployeeId", "Employee ID");
		employeeIdField.setPrimaryKey(true);
		employeeIdField.setRequired(true);

		DataSourceIntegerField reportsToField= new DataSourceIntegerField("ReportsTo", "Manager");
		reportsToField.setRequired(true);
		reportsToField.setForeignKey(id + ".EmployeeId");
		reportsToField.setRootValue("1");

		DataSourceTextField jobField= new DataSourceTextField("Job", "Title", 128);
		DataSourceFloatField salaryField= new DataSourceFloatField("Salary", "Salary");
		
		setFields(nameField, employeeIdField, reportsToField, jobField, salaryField);
		setDataURL(Test.XML_FILE);
		setClientOnly(true);
	}
}