package com.wolwa.emp.client;

import com.google.gwt.core.client.EntryPoint;
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.ValuesManager;
import com.smartgwt.client.widgets.form.fields.MultiFileItem;
import com.smartgwt.client.widgets.layout.VLayout;


public class TestCase implements EntryPoint {

	@Override
	public void onModuleLoad() {
		// Change to switch between using normal form or valuesManager
		final boolean useValuesManager = true;
		
		DataSource masterDS = DataSource.get("multiUploadMaster");
		DataSource fileDS = DataSource.get("multiUploadDetail");
		
		final ValuesManager vm = new ValuesManager();
		vm.setDataSource(masterDS);
		
		MultiFileItem files = new MultiFileItem("file", "File Attachments");
		files.setAttribute("dataSource", fileDS.getID());
		
		final DynamicForm filesForm = new DynamicForm();
		filesForm.setWidth(400);
		filesForm.setFields(files);
		
		if(useValuesManager)
		{
			filesForm.setValuesManager(vm);
		}
		else
		{
			filesForm.setDataSource(masterDS);
		}
		
		final VLayout mainLayout = new VLayout();
		
		Button fetchButton = new Button("Fetch PK");
		fetchButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(useValuesManager)
				{
					vm.fetchData(null);
				}
				else
				{
					filesForm.fetchData();
				}
			}
		});
		Button saveButton = new Button("Save Files");
		saveButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(useValuesManager)
				{
					vm.saveData();
				}
				else
				{
					filesForm.saveData();
				}
			}
		});
		

		mainLayout.addMember(fetchButton);
		mainLayout.addMember(saveButton);
		mainLayout.addMember(filesForm);
		
		mainLayout.draw();
	}
}

