package com.interpress_project.modernshare.client.view.wizard;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * @stereotype Wizard page1
 */
public class ChgPwdWizardPage1 extends WizardPage {
	protected String newPassword1 = "", newPassword2 = "";

	/**
	 * ChgPwdWizardPage1
	 */
	public ChgPwdWizardPage1() {
		super("ChgPwdWizardPage1");

		this.setTitle("パスワードの変更");
		this.setDescription("パスワードを変更します。");
		this.setPageComplete(false);
	}

	/**
	 * createControl
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 9;
		/**
		 * 新しいパスワードの入力ボックス
		 */
		Label label2 = new Label(composite, SWT.NULL);
		label2.setText("新しいパスワードの入力:");

		final Text txtNewPassword1 = new Text(composite, SWT.BORDER);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		txtNewPassword1.setLayoutData(gd);
		txtNewPassword1.setEchoChar('*');

		txtNewPassword1.addVerifyListener(new VerifyListener() {
			public void verifyText(VerifyEvent event) {
				if (event.character == ' ') {
					pageIncomplete("無効な文字が入力されました。(英数字と記号のみ)");
					event.doit = false;
					return;
				}
			}
		});

		txtNewPassword1.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent modifyEvent) {
				newPassword1 = txtNewPassword1.getText().trim();
				pageComplete();
			}
		});

		/**
		 * 新しいパスワードの入力ボックス2
		 */
		Label label3 = new Label(composite, SWT.NULL);
		label3.setText("新しいパスワードの確認入力:");

		final Text txtNewPassword2 = new Text(composite, SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		txtNewPassword2.setLayoutData(gd);
		txtNewPassword2.setEchoChar('*');

		txtNewPassword2.addVerifyListener(new VerifyListener() {
			public void verifyText(VerifyEvent event) {
				if (event.character == ' ') {
					pageIncomplete("無効な文字が入力されました。(英数字と記号のみ)");
					event.doit = false;
					return;
				}
			}
		});

		txtNewPassword2.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent modifyEvent) {
				setErrorMessage(null);
				newPassword2 = txtNewPassword2.getText().trim();
				if (!newPassword1.equals(newPassword2)) {
					pageIncomplete("パスワードが一致しません。");
					return;
				}
				pageComplete();
			}
		});

		/**
		 * コントロールの登録
		 */
		setControl(composite);
	}

	/**
	 * pageIncomplete
	 * @param msg
	 */
	protected void pageIncomplete(String msg) {
		setErrorMessage(msg);
		setPageComplete(false);
	}

	/**
	 * pageComplete
	 */
	protected void pageComplete() {
		setErrorMessage(null);
		setPageComplete(newPassword1.length() > 0 && newPassword2.length() > 0);
	}

	/**
	 * getPassword
	 * @return
	 */
	public String getPassword() {
		return newPassword1;
	}
}