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.Criterion;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.data.fields.DataSourceBooleanField;
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.types.OperatorId;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Test implements EntryPoint {
	private RestDataSource mainDataSource;
	private RestDataSource optionDataSource;
	
	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get();
		
		TabSet tabSet = new TabSet();
		tabSet.setSize("400px", "251px");
		
		Tab tab1 = new Tab("Tab 1");
		{
			ListGrid listGrid = new ListGrid();
			listGrid.setDataFetchMode (FetchMode.LOCAL);

			ListGridField codeField = new ListGridField ("code");
			ListGridField nameField = new ListGridField ("name");
			ListGridField optionField = new ListGridField ("option");
			optionField.setOptionDataSource (getOptionDataSource());
			optionField.setValueField ("id");
			optionField.setDisplayField ("name");
			listGrid.setFields (codeField, nameField, optionField);
			
			listGrid.setDataSource(getMainDataSource());
			listGrid.setShowFilterEditor(true);
			listGrid.setAutoFetchData (true);
			
			final Criterion criterion = new Criterion ("tab1", OperatorId.EQUALS, Boolean.TRUE);
			listGrid.setInitialCriteria (criterion);
			
			tab1.setPane(listGrid);
			tabSet.addTab(tab1);
		}
		
		Tab tab2 = new Tab("Tab 2");
		{
			ListGrid listGrid = new ListGrid();
			listGrid.setDataFetchMode (FetchMode.LOCAL);

			ListGridField codeField = new ListGridField ("code");
			ListGridField nameField = new ListGridField ("name");
			ListGridField optionField = new ListGridField ("option");
			optionField.setOptionDataSource (getOptionDataSource());
			optionField.setValueField ("id");
			optionField.setDisplayField ("name");
			optionField.setOptionDataSource (getOptionDataSource());
			listGrid.setFields (codeField, nameField, optionField);
			
			listGrid.setDataSource(getMainDataSource());
			listGrid.setShowFilterEditor(true);
			listGrid.setAutoFetchData (true);
			
			final Criterion criterion = new Criterion ("tab2", OperatorId.EQUALS, Boolean.TRUE);
			listGrid.setInitialCriteria (criterion);
			
			tab2.setPane(listGrid);
			tabSet.addTab(tab2);
		}
		
		rootPanel.add(tabSet, 20, 23);
	}
	

	private RestDataSource getMainDataSource() {
		if (mainDataSource == null) {
			mainDataSource = new RestDataSource();
			mainDataSource.setDataFormat(DSDataFormat.JSON);
			mainDataSource.setFetchDataURL("data/mainds_fetch.js");
			
			DataSourceIntegerField pkField = new DataSourceIntegerField("id", "ID");
			pkField.setPrimaryKey (true);
			mainDataSource.addField(pkField);
			
			DataSourceTextField codeField = new DataSourceTextField("code", "CODE");
			mainDataSource.addField(codeField);
			
			DataSourceTextField nameField = new DataSourceTextField("name", "NAME");
			mainDataSource.addField(nameField);
			
			DataSourceBooleanField tab1Field = new DataSourceBooleanField("tab1");
			tab1Field.setHidden (true);
			mainDataSource.addField(tab1Field);
			
			DataSourceBooleanField tab2Field = new DataSourceBooleanField("tab2");
			tab2Field.setHidden (true);
			mainDataSource.addField(tab2Field);
			
			DataSourceIntegerField optionField = new DataSourceIntegerField("option", "OPTION");
			mainDataSource.addField(optionField);
		}
		return mainDataSource;
	}
	
	private RestDataSource getOptionDataSource() {
		if (optionDataSource == null) {
			optionDataSource = new RestDataSource();
			optionDataSource.setDataFormat(DSDataFormat.JSON);
			optionDataSource.setFetchDataURL("data/optionds_fetch.js");
			
			DataSourceIntegerField pkField = new DataSourceIntegerField("id", "ID");
			optionDataSource.addField(pkField);
			
			DataSourceTextField nameField = new DataSourceTextField("name", "NAME");
			optionDataSource.addField(nameField);
		}
		return optionDataSource;
	}	
}
