package com.xxx.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.data.DataSource;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.util.SC;
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.TextItem;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

public class ValidationEntryPoint  implements EntryPoint {
	protected VLayout showVLayout = new VLayout();
	protected ValuesManager valManger = new ValuesManager();
	
	@Override
	public void onModuleLoad() {
		final ToolStripButton toolStripButton = new ToolStripButton("Save");
		toolStripButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(final ClickEvent event) {
				// TODO Auto-generated method stub
				valManger.setSaveOperationType(DSOperationType.ADD);
				final DSRequest request = new DSRequest();
                request.setWillHandleError(true);
				valManger.saveData(new DSCallback() {
					@Override
					public void execute(final DSResponse dsResponse, final Object data,
							final DSRequest dsRequest) {
						if (dsResponse.getStatus() == RPCResponse.STATUS_SUCCESS) {
							SC.say("Save Successfully.");
						} 
					}
				}, request);
			}});
		showVLayout.addMember(toolStripButton);
		final DynamicForm formFirst = new DynamicForm();
		
		final TextItem firstNm = new TextItem("firstNm");
		firstNm.setValidateOnExit(true);
		final DataSource firstDs = DataSource.get("FirstDs");
		firstNm.setTitle("First Name");
		
		valManger.setDataSource(firstDs);
		
		
		formFirst.setDataSource(firstDs);
		
		formFirst.setFields(firstNm);
		
		formFirst.setGroupTitle("Test First Form");
		formFirst.setIsGroup(true);
		
		valManger.addMember(formFirst);
		
		showVLayout.addMember(formFirst);
		
		final DynamicForm formSecond = new DynamicForm();
		
		formSecond.setDataSource(firstDs);
		final TextItem secondNm = new TextItem("secondNm");
		secondNm.setValidateOnExit(true);
		secondNm.setTitle("Second Name");
		
		formSecond.setFields(secondNm);
		
		formSecond.setGroupTitle("Test Second Form");
		formSecond.setIsGroup(true);
		
		valManger.addMember(formSecond);
		
		
		
		showVLayout.addMember(formSecond);
		showVLayout.setLeft(100);
		showVLayout.setVisible(true);
		showVLayout.draw();
	}
}

