package com.mycompany.project.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.FetchMode;
import com.smartgwt.client.widgets.form.FilterBuilder;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Test implements EntryPoint {
	private RestDataSource mainDataSource;

	@Override
	public void onModuleLoad () {
		final RootPanel rootPanel = RootPanel.get ();
		final VLayout container = new VLayout ();
		container.setWidth100 ();
		container.setHeight100 ();
		
		final FilterBuilder filterBuilder = new FilterBuilder ();
		filterBuilder.setDataSource (getMainDataSource ());
		
		final ListGrid listGrid = new ListGrid ();
		listGrid.setWidth (600);
		listGrid.setDataFetchMode (FetchMode.LOCAL);
		listGrid.setAutoFetchData (true);
		listGrid.setDataSource (getMainDataSource ());
		listGrid.setShowFilterEditor (true);
		listGrid.setHeight (200);

		final ListGridField idField = new ListGridField ("id");
		final ListGridField nameField = new ListGridField ("name");
		final ListGridField parentField = new ListGridField ("parent_id");
		parentField.setOptionDataSource (getMainDataSource ());
		parentField.setValueField ("id");
		parentField.setDisplayField ("name");	//NEEDED to show the display value on the list grid
		
		/*
		 * the following commented block would sort the drop down values for parent field filter editor, but seems irrelevant
		 */
		
//		final SelectItem parentItem = new SelectItem ("parent_id");
//		parentItem.setOptionDataSource (getMainDataSource ());
//		parentItem.setValueField ("id");
//		parentItem.setDisplayField ("name");
//		
//		parentItem.setSortField ("name");
//		ListGrid pickListProperties = new ListGrid ();
//		pickListProperties.setDataFetchMode (FetchMode.LOCAL);
//		pickListProperties.setAutoFetchData (true);
//		pickListProperties.setDataSource (getMainDataSource ());
//		pickListProperties.setInitialSort (new SortSpecifier[]{new SortSpecifier ("name", SortDirection.ASCENDING)});
////		pickListProperties.setSort (new SortSpecifier[]{new SortSpecifier ("name", SortDirection.ASCENDING)});
////		pickListProperties.setSortField ("name");
//		parentItem.setPickListProperties (pickListProperties);
//		parentField.setEditorType (parentItem);
		
		
		listGrid.setFields (idField, nameField, parentField);

		rootPanel.setHeight ("600px");
		
		container.addMember (filterBuilder);
		container.addMember (listGrid);

		rootPanel.add (container);
	}

	private synchronized RestDataSource getMainDataSource () {
		if (mainDataSource == null) {
			mainDataSource = new RestDataSource ();
			mainDataSource.setID ("mainDataSource");
			mainDataSource.setDataFormat (DSDataFormat.JSON);
			mainDataSource.setFetchDataURL ("data/mainds_fetch.js");

			final DataSourceIntegerField pkField = new DataSourceIntegerField ("id", "ID");
			pkField.setPrimaryKey (true);
			mainDataSource.addField (pkField);

			final DataSourceTextField nameField = new DataSourceTextField ("name", "NAME");
			mainDataSource.addField (nameField);

			final DataSourceIntegerField parentIdField = new DataSourceIntegerField ("parent_id", "PARENT");
			parentIdField.setForeignKey ("mainDataSource.id");

			final SelectItem optionsItem = new SelectItem ();
			optionsItem.setName (parentIdField.getName ());

			mainDataSource.addField (parentIdField);
			
			optionsItem.setOptionDataSource (mainDataSource);
			optionsItem.setValueField ("id");
			optionsItem.setDisplayField ("name");
			optionsItem.setSortField ("name");	//sort the drop down values
			
			parentIdField.setEditorType (optionsItem);	//NEEDED to let the filter builder show a sorted drop-down list
		}
		return mainDataSource;
	}

}
