package com.ssdi.test.client;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
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.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class FormValidation extends Window {

	protected Window editWindow;
	public final String UserName_REG_EXP = "^(^([\\x00-\\x80]+))$";
	public final static String GROUP_NAME_REG_EXP2 = "[\\S]";
	protected RegExpValidator nameStringValidator = new RegExpValidator();
	protected RegExpValidator nameStringValidatorForSpace = new RegExpValidator();
	DynamicForm dForm = new DynamicForm();
	final TextItem nameTextItem = new TextItem();

	public void editfunction() {
		editWindow = new Window();
		editWindow.setHeight(150);
		editWindow.setWidth(350);
		editWindow.centerInPage();
		editWindow.setCanDragResize(true);

		VLayout vlay = new VLayout();
		vlay.setAlign(VerticalAlignment.CENTER);
		vlay.setMembersMargin(5);
		vlay.addMembers(getStudentDynamicForm());

		HLayout hLay = new HLayout(10);
		hLay.setMembers(getBottomLayout());
		hLay.setAlign(Alignment.CENTER);
		VLayout main = new VLayout(10);
		main.setAlign(Alignment.CENTER);
		main.addMembers(vlay, hLay);

		editWindow.setTitle("Edit Window");
		editWindow.addItem(main);
		editWindow.show();
	}

	public DynamicForm getStudentDynamicForm() {

		dForm = new DynamicForm();
		dForm.setRequiredTitlePrefix("");
		dForm.setColWidths(155);

		nameStringValidator.setExpression(UserName_REG_EXP);
		nameStringValidator.setStopIfFalse(true);
		nameStringValidator.setStopOnError(true);

		nameStringValidatorForSpace.setExpression(GROUP_NAME_REG_EXP2);
		nameStringValidatorForSpace.setStopIfFalse(true);

		nameTextItem.setTitle("User Name");
		nameTextItem.setName("nameTextItemField");
		nameTextItem.setCanEdit(true);
		nameTextItem.setWidth(150);
		nameTextItem.setHeight(30);
		nameTextItem.setRequired(true);
		nameTextItem.setShowErrorStyle(false);
		nameTextItem.setValidateOnExit(false);

		nameTextItem.setValidators(nameStringValidator,
				nameStringValidatorForSpace);
		nameTextItem.addBlurHandler(new BlurHandler() {

			@Override
			public void onBlur(BlurEvent event) {

				if (!(nameTextItem.validate())) {
					nameTextItem.clearValue();
				} else {}
			}
		});
		dForm.setItems(nameTextItem);
		return dForm;

	}

	public HLayout getBottomLayout() {
		HLayout footer = new HLayout(10);
		footer.setAlign(Alignment.CENTER);
		IButton ok = new IButton("OK");
		ok.setWidth(50);
		ok.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (dForm.validate()) {
					SC.say("validate");
				}
			}
		});

		IButton cancel = new IButton("cancel");
		cancel.setWidth(50);
		cancel.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				editWindow.destroy();
			}
		});
		footer.addMembers(ok, cancel);
		return footer;
	}
}
