package com.ssdi.test.client;

import java.util.Date;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.DateChooser;
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.DateItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class FormValidation extends Window {

	protected Window editWindow;
	DynamicForm dForm = new DynamicForm();
	DateChooser datetimeChooser = new DateChooser();
	DateItem startDateItem;

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

		VLayout vlay = new VLayout();
		vlay.setAlign(VerticalAlignment.TOP);
		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);
		startDateItem = new DateItem();
		startDateItem.setTitle("Select Date");
		startDateItem.setValue(new Date());
		startDateItem.setRequired(true);
		startDateItem.setShowErrorStyle(false);
		startDateItem.setTitleOrientation(TitleOrientation.LEFT);
		startDateItem.setAlign(Alignment.LEFT);
		startDateItem.setEnforceDate(true);
		startDateItem.setTitleColSpan(1);
		startDateItem.setPickerProperties(datetimeChooser);
		startDateItem.setWidth(180);
		startDateItem.setStopOnError(true);
		startDateItem.setUseTextField(true);
		// startDateItem.setTextAlign(Alignment.LEFT);

		startDateItem.addChangedHandler(new ChangedHandler() {
			@Override
			public void onChanged(ChangedEvent event) {
				startDateItem.validate();
			}
		});
		dForm.setItems(startDateItem);
		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) {
			}
		});

		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;
	}

}
