package com.smartgwt.sample.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.FileItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HStack;

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

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
    	HStack hStack = new HStack(10);  
        hStack.setHeight(160);  
  
        final DynamicForm form = new DynamicForm();
        form.setIsGroup(true);
        form.setTitle("title");
		form.setHeight100();
		form.setValidateOnExit(true);
		form.setTitleAlign(Alignment.LEFT);
		form.setDataSource(ClientOnlyDS.getInstance());
		form.setTitleWidth(150);
		form.setFixedColWidths(true);
		form.setTitleSuffix("");
		form.setRequiredTitleSuffix("");
		form.setLayoutAlign(VerticalAlignment.BOTTOM);
		final FileItem fileItem = new FileItem("xls", "Name of import file");  
		fileItem.setWidth(300);
		fileItem.setRequired(true);
		fileItem.setDisabled(true);
		
		final ButtonItem uploadItem = new ButtonItem("save", "Upload");  
		uploadItem.setDisabled(true);
		uploadItem.addClickHandler(new ClickHandler() {  
            public void onClick(ClickEvent event) { 
            	if (!form.validate()) {
            		return;
            	}
            	form.saveData(new DSCallback() {  
                    public void execute(DSResponse response, Object data, DSRequest request) { 
                    	form.editNewRecord();
                    	uploadItem.setDisabled(true);
                    	fileItem.setDisabled(true);
                    }
            	});
            };
		});
		
		ButtonItem prechooseItem = new ButtonItem("xxx", "enable");  
		prechooseItem.setDisabled(false);
		prechooseItem.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				uploadItem.setDisabled(false);
				fileItem.setDisabled(false);
			}
		});
		form.setFields(prechooseItem, fileItem, uploadItem);
        
        hStack.addMember(form);  
  
        hStack.draw();  
    }
}
