package com.smartgwt.sample.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.widgets.Button;
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.fields.CheckboxItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.VLayout;

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

	public void onModuleLoad() {
		CheckboxItem nameCheckboxItem = new CheckboxItem("Name");
		CheckboxItem emailCheckboxItem = new CheckboxItem("Email");
		CheckboxItem salaryCheckboxItem = new CheckboxItem("Salary");
		
		Button  refreshGridButton = new Button("Refresh Grid");
		
		final DynamicForm dynamicForm = new DynamicForm();
		dynamicForm.setIsGroup(Boolean.TRUE);
		dynamicForm.setGroupTitle("Select Checkbox to add Column in Group.");
		dynamicForm.setWidth100();
		dynamicForm.setFields(nameCheckboxItem, emailCheckboxItem, salaryCheckboxItem);
		
		final ListGrid listGrid = new ListGrid();
		listGrid.setDataSource(DataSource.get("employees"));
		listGrid.setAutoFetchData(Boolean.TRUE);
		listGrid.setCanEdit(Boolean.TRUE);
		listGrid.setShowFilterEditor(Boolean.TRUE);

		VLayout  layout = new VLayout();
		layout.setWidth100();
		layout.setHeight100();
		layout.setMembersMargin(10);
		layout.setLayoutMargin(10);
		layout.addMember(dynamicForm);
		layout.addMember(refreshGridButton);
		layout.addMember(listGrid);
		layout.draw();
		
		refreshGridButton.addClickHandler(new ClickHandler() {
			@Override public void onClick(ClickEvent event) {
				Criteria criteria = dynamicForm.getValuesAsCriteria();
				listGrid.fetchData(criteria);
			}
		});
		
		
	}
}
