package be.uniway.rvk.test.datasource.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ExportDisplay;
import com.smartgwt.client.types.ExportFormat;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
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.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;

public class SmartGWTServerDataSource implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final Layout mainLayout = new VLayout(5);
		mainLayout.setWidth100();
		mainLayout.setHeight100();
		
		final Layout topLayout = new HLayout(2);
		topLayout.setWidth100();
		final Label titleLabel = new Label("<h1>Custom datasource test project</h1>");
		titleLabel.setWidth100();
		titleLabel.setHeight("40px");
		titleLabel.setAlign(Alignment.LEFT);
		titleLabel.setValign(VerticalAlignment.TOP);
		topLayout.addMember(titleLabel);
		mainLayout.addMember(topLayout);
		
		final Layout formLayout = new HLayout(2);
		final DynamicForm formSearch = new DynamicForm();
		final TextItem inputUserName = new TextItem();
		inputUserName.setTitle("Username");
		final IButton searchButton = new IButton("Search");
		formSearch.setFields(inputUserName);
		final IButton exportButton = new IButton("Export All");
		formLayout.addMember(formSearch);
		formLayout.addMember(searchButton);
		formLayout.addMember(exportButton);
		mainLayout.addMember(formLayout);

		
		final Layout listGridLayout = new HLayout(2);
		listGridLayout.setWidth100();
		listGridLayout.setHeight100();
		
		DataSource clientsDS = DataSource.get("clients");
		final ListGrid clientsGrid = new ListGrid();
		clientsGrid.setDataSource(clientsDS);
		listGridLayout.addMember(clientsGrid);
		
		mainLayout.addMember(listGridLayout);
		
		searchButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Criteria c = new Criteria();
				c.addCriteria("username",(String) inputUserName.getValue() );
				clientsGrid.fetchData(c);
			}
		});
		exportButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				DSRequest dsRequestProperties = new DSRequest();
				dsRequestProperties.setExportAs(ExportFormat.XLS);
				dsRequestProperties.setExportDisplay(ExportDisplay.WINDOW);
				clientsGrid.exportData(dsRequestProperties);
			}
		});
		
		RootPanel.get("content").add(mainLayout);
	}
}
