All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.efaps.ui.wicket.components.FormContainer Maven / Gradle / Ivy

Go to download

eFaps WebApp provides a web interface as the User Interface for eFaps which can be easily expanded and altered.

There is a newer version: 3.2.0
Show newest version
/*
 * Copyright 2003 - 2010 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Revision:        $Rev: 7151 $
 * Last Changed:    $Date: 2011-09-15 23:39:19 -0500 (Thu, 15 Sep 2011) $
 * Last Changed By: $Author: [email protected] $
 */

package org.efaps.ui.wicket.components;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.wicket.Component;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IFormSubmitListener;
import org.apache.wicket.util.string.JavascriptUtils;
import org.efaps.ui.wicket.components.date.DateTimePanel;
import org.efaps.ui.wicket.components.date.IDateListener;
import org.efaps.ui.wicket.models.objects.AbstractUIObject;
import org.efaps.ui.wicket.models.objects.UIForm;

/**
 * Class for a form. Needed for file upload.
 *
 * @author The eFaps Team
 * @version $Id: FormContainer.java 7151 2011-09-16 04:39:19Z [email protected] $
 */
public class FormContainer
    extends Form
    implements IDateListener
{

    /**
     * Needed for serialization.
     */
    private static final long serialVersionUID = 1L;

    /**
     * Url for the action that must be called.
     */
    private String actionUrl;

    /**
     * Is this form a used to upload a file.
     */
    private boolean fileUpload = false;

    /**
     * Set contains the date components of this formpanel.
     */
    private final Set dateComponents = new HashSet();

    /**
     * Constructor setting the wicket id of this component.
     *
     * @param _wicketId wicket id of this component
     */
    public FormContainer(final String _wicketId)
    {
        super(_wicketId);
    }

    /**
     * On component tag.
     *
     * @param _tag the tag
     *
     * @see org.apache.wicket.markup.html.form.Form
     *      #onComponentTag(org.apache.wicket.markup.ComponentTag)
     */
    @Override
    protected void onComponentTag(final ComponentTag _tag)
    {
        final Object uiObject = getPage().getDefaultModelObject();
        if (uiObject instanceof UIForm && ((UIForm) uiObject).isFileUpload()
                        && (((UIForm) uiObject).isCreateMode() || ((UIForm) uiObject).isEditMode())) {
            setMultiPart(true);
            setMaxSize(getApplication().getApplicationSettings().getDefaultMaximumUploadSize());
        }
        super.onComponentTag(_tag);
        this.actionUrl = urlFor(IFormSubmitListener.INTERFACE).toString();
        if (getPage().getDefaultModelObject() != null) {
            // only on SearchMode we want normal submit, in any other case we
            // use AjaxSubmit
            if (!((AbstractUIObject) getPage().getDefaultModelObject()).isSearchMode()) {
                //_tag.put("onSubmit", "return false;");
                _tag.put("action", "");
            }
        }
    }

    /**
     * This is the getter method for the instance variable {@link #actionUrl}.
     *
     * @return value of instance variable {@link #actionUrl}
     */
    public String getActionUrl()
    {
        return this.actionUrl;
    }

    /**
     * On submit it is checked if it is a file upload from and in case that it
     * is the listeners executed.
     *
     * @see org.apache.wicket.markup.html.form.Form#onSubmit()
     */
    @Override
    protected void onSubmit()
    {
        super.onSubmit();
        if (this.fileUpload) {
            final List uploadListeners = this.getBehaviors(IFileUploadListener.class);
            for (final IFileUploadListener listener : uploadListeners) {
                listener.onSubmit();
            }
        }
    }

    /**
     * This is the getter method for the instance variable {@link #fileUpload}.
     *
     * @return value of instance variable {@link #fileUpload}
     */
    public boolean isFileUpload()
    {
        return this.fileUpload;
    }

    /**
     * This is the setter method for the instance variable {@link #fileUpload}.
     *
     * @param _fileUpload the fileUpload to set
     */
    public void setFileUpload(final boolean _fileUpload)
    {
        this.fileUpload = _fileUpload;
    }

    /**
     * Overwritten due to the reason that the mulitpart is handelt using
     * the Context.
     * @see org.apache.wicket.markup.html.form.Form#handleMultiPart()
     * @return true
     */
    @Override
    protected boolean handleMultiPart()
    {
        return true;
    }

    /**
     * If a default IFormSubmittingComponent was set on this form, this method
     * will be called to render an extra field with an invisible style so that
     * pressing enter in one of the textfields will do a form submit using
     * this component.
     * The method is overwritten to correct some script problems with the
     * default behavior from Firefox, that actually sends and reloads the form.
     *
     * @param _markupStream The markup stream
     * @param _openTag The open tag for the body
     */
    @Override
    protected void appendDefaultButtonField(final MarkupStream _markupStream,
                                            final ComponentTag _openTag)
    {
        final StringBuilder divBldr = new StringBuilder();
        // div that is not visible (but not display:none either)
        divBldr.append("
"); // add an empty textfield (otherwise IE doesn't work) divBldr.append(""); final Component submittingComponent = (Component) getDefaultButton(); divBldr.append("
"); getResponse().write(divBldr); // this trick prevents that the default behavior is executed final StringBuilder bldr = new StringBuilder(); bldr.append("Wicket.Event.add(Wicket.$('").append(this.getMarkupId()) .append("'), 'submit', function (evt){ evt.preventDefault();});"); JavascriptUtils.writeJavascript(getResponse(), bldr.toString()); } /** * Add a date component. * * @param _dateTimePanel date picker */ public void addDateComponent(final DateTimePanel _dateTimePanel) { this.dateComponents.add(_dateTimePanel); } /** * Getter method for instance variable {@link #dateComponents}. * * @return instance variable {@link #dateComponents} */ public Set getDateComponents() { return this.dateComponents; } }