/*
 * Smart GWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * Smart GWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.  Smart GWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

package com.brianschrameck.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourcePasswordField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.ValuesManager;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;

public class FormMatchValueSample implements EntryPoint
{

    @Override
    public void onModuleLoad()
    {
        final MyForm form = new MyForm();
        form.setWidth(250);
        form.draw();
    }

    private class MyForm extends DynamicForm
    {
        private ValuesManager valuesManager = new ValuesManager();

        public MyForm()
        {
            setDataSource(new LocalDataSource());

            // UNCOMMENT THIS TO SHOW BEHAVIOR
            valuesManager.setDataSource(getDataSource());

            TextItem username = new TextItem();
            username.setName("username");
            username.setTitle("Username");
            username.setRequired(true);
            username.setDefaultValue("bob");

            TextItem email = new TextItem();
            email.setName("email");
            email.setTitle("Email");
            email.setRequired(true);
            email.setDefaultValue("bob@isomorphic.com");

            PasswordItem password = new PasswordItem();
            password.setName("password");
            password.setTitle("Password");
            password.setRequired(true);

            PasswordItem password2 = new PasswordItem();
            password2.setName("password2");
            password2.setTitle("Password again");
            password2.setRequired(true);
            // Add this to prevent sending password2 to the server upon saving.
            password2.setShouldSaveValue(false);

            final ButtonItem createAccount = new ButtonItem();
            createAccount.setName("createAccount");
            createAccount.setTitle("Create Account");
            createAccount.addClickHandler(new ClickHandler()
            {
                @Override
                public void onClick(ClickEvent event)
                {
                    // COMMENT THIS TO SHOW BEHAVIOR
                    // saveData();

                    // UNCOMMENT THIS TO SHOW BEHAVIOR
                    getValuesManager().saveData();
                }
            });

            setFields(username, email, password, password2, createAccount);

            // UNCOMMENT THIS TO SHOW BEHAVIOR
            setValuesManager(valuesManager);

            draw();
        }
    }

    private class LocalDataSource extends DataSource
    {
        public LocalDataSource()
        {
            setClientOnly(true);
            DataSourceTextField username = new DataSourceTextField("username");
            DataSourceTextField email = new DataSourceTextField("email");
            DataSourcePasswordField password = new DataSourcePasswordField("password");
            DataSourcePasswordField password2 = new DataSourcePasswordField("password2");

            setFields(username, email, password, password2);
        }
    }
}