/*
 * Copyright (C) 2002-2004 Red Hat Inc. All Rights Reserved.
 *
 * The contents of this file are subject to the CCM Public
 * License (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the
 * License at http://www.redhat.com/licenses/ccmpl.html.
 *
 * Software distributed under the License is distributed on an
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
 * or implied. See the License for the specific language
 * governing rights and limitations under the License.
 *
 */
package com.arsdigita.formbuilder.ui.editors;


import com.arsdigita.formbuilder.util.GlobalizationUtil ; 



import com.arsdigita.bebop.FormData;
import com.arsdigita.bebop.FormProcessException;
import com.arsdigita.bebop.FormSection;
import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.PageState;

import com.arsdigita.bebop.event.FormSectionEvent;

import com.arsdigita.bebop.form.TextField;

import com.arsdigita.bebop.parameters.StringParameter;

import com.arsdigita.domain.DataObjectNotFoundException;

import com.arsdigita.formbuilder.PersistentSubmit;


import java.math.BigDecimal;
import com.arsdigita.bebop.SingleSelectionModel;
import com.arsdigita.formbuilder.PersistentWidget;
import com.arsdigita.bebop.parameters.StringInRangeValidationListener;
import com.arsdigita.bebop.parameters.NotNullValidationListener;
import com.arsdigita.bebop.ColumnPanel;


public class SubmitForm extends WidgetForm {
    private TextField m_value;

    public SubmitForm(String name,
                      SingleSelectionModel form,
                      SingleSelectionModel control) {
        super(name, form, control);
    }

    protected void addWidgets(FormSection section) {
        m_value = new TextField(new StringParameter("value"));
        m_value.addValidationListener(new NotNullValidationListener());
        m_value.addValidationListener(new StringInRangeValidationListener(1, 50));
        section.add(new Label(GlobalizationUtil.globalize("formbuilder.ui.editors.label")), ColumnPanel.RIGHT);
        section.add(m_value);

        m_value.setOnFocus("if (this.form." + NAME + ".value == '') { " +
                           " defaulting = true; this.form." + NAME +
                           ".value = urlize(this.value); }");
        m_value.setOnKeyUp(
            "if (defaulting) { this.form." + NAME +
            ".value = urlize(this.value) }"
            );

        super.addWidgets(section);

        automaticallySetName(m_value.getParameterModel());
    }

    protected PersistentWidget getWidget() {
        return new PersistentSubmit();
    }

    protected PersistentWidget getWidget(BigDecimal id)
        throws DataObjectNotFoundException {

        return new PersistentSubmit(id);
    }

    protected void initWidgets(FormSectionEvent e,
                               PersistentWidget w)
        throws FormProcessException {
        super.initWidgets(e, w);

        PersistentSubmit widget = (PersistentSubmit)w;

        PageState state = e.getPageState();

        if (widget == null) {
            m_value.setValue(state, "");
        } else {
            m_value.setValue(state, widget.getDefaultValue());
        }
    }

    protected void processWidgets(FormSectionEvent e,
                                  PersistentWidget w)
        throws FormProcessException {
        super.processWidgets(e, w);

        PersistentSubmit widget = (PersistentSubmit)w;

        FormData data = e.getFormData();

        String value = (String)data.get("value");

        widget.setDefaultValue(value);
    }
}
