package com.smartgwt.sample.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.util.PageKeyHandler;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.BatchUploader;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.VLayout;

public class BuiltInDS implements EntryPoint {
	private VLayout mainLayout;
	private IButton recreateBtn;
	private DataSource uploadDS = DataSource.get("employeesUpload");
	private DataSource optionDS = DataSource.get("employees");

	public void onModuleLoad() {
		KeyIdentifier debugKey = new KeyIdentifier();
		debugKey.setCtrlKey(true);
		debugKey.setKeyName("D");

		Page.registerKey(debugKey, new PageKeyHandler() {
			public void execute(String keyName) {
				SC.showConsole();
			}
		});

		mainLayout = new VLayout(20);
		mainLayout.setWidth100();
		mainLayout.setHeight100();

		recreateBtn = new IButton("Recreate");
		recreateBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				recreate();
			}
		});
		mainLayout.addMember(recreateBtn);
		recreate();
		mainLayout.draw();
	}

	private void recreate() {
		Window w = new Window();
		w.setWidth("95%");
		w.setHeight("95%");
		w.setMembersMargin(0);
		w.setModalMaskOpacity(70);
		w.setTitle("Import data");
		w.setShowMinimizeButton(false);
		w.setIsModal(true);
		w.setShowModalMask(true);
		w.centerInPage();

		BatchUploader batchUploader = new BatchUploader();
		batchUploader.setWidth100();
		batchUploader.setUploadDataSource(uploadDS);

		ListGridField employeeIdLGF = new ListGridField("EmployeeId");
		
		ListGridField nameLGF = new ListGridField("Name");

		ListGridField managerLGF = new ListGridField("ReportsTo");
		managerLGF.setOptionDataSource(optionDS);
		managerLGF.setValueField("EmployeeId");
		managerLGF.setDisplayField("Name");

		ListGridField jobLGF = new ListGridField("Job");

		ListGridField genderLGF = new ListGridField("Gender");

		batchUploader.setGridFields(employeeIdLGF, genderLGF, nameLGF, managerLGF, jobLGF);

		w.addItem(batchUploader);
		w.show();
	}
}