/*
 * Copyright 2012 s_wolff.
 *
 * 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.
 */
package org.aegee.runanddine.util.forms;

import java.io.Serializable;
import org.apache.wicket.behavior.SimpleAttributeModifier;

/**
 * Class for displaying text areas.
 *
 * @author s_wolff
 * @see AbstractFormField
 * @see org.apache.wicket.markup.html.form.TextArea
 */
public class TextArea<T extends Serializable> extends AbstractFormField<T> {

    /**
     * Create a new text area. The type of the field provided by the
     * <code>fieldName</code> parameter should be of type
     * <code>String</code> as the text area only dipslays
     * <code>String<code>s.
     *
     * @param id the component markup id
     * @param obj the object that backens the form field
     * @param fieldName the field name of the object that backens the form field
     * @param labelText the label text that shall be displayed for this field
     * @param required whether this field is required or optional
     */
    public TextArea(String id, T obj, String fieldName, String labelText, boolean required) {
        this(id, obj, fieldName, labelText, required, 10);
    }

    /**
     * Create a new text area. The type of the field provided by the
     * <code>fieldName</code> parameter should be of type
     * <code>String</code> as the text area only dipslays
     * <code>String<code>s.
     *
     * @param id the component markup id
     * @param obj the object that backens the form field
     * @param fieldName the field name of the object that backens the form field
     * @param labelText the label text that shall be displayed for this field
     * @param required whether this field is required or optional
     * @param size the number of rows that shall be displayed
     */
    public TextArea(String id, T obj, String fieldName, String labelText, boolean required, int size) {
        super(id, obj, new org.apache.wicket.markup.html.form.TextArea(AbstractFormField.ID_INPUT), fieldName, labelText, required);
        this.setSize(size);
    }

    /**
     * Resizes the text area to given size.
     *
     * @param size the number of row that shall be displayed
     * @return
     * <code>this</code>
     */
    public final TextArea<T> setSize(int size) {
        super.addInputBehaviour(new TextAreaSizer(size));
        return this;
    }

    private class TextAreaSizer extends SimpleAttributeModifier {

        public TextAreaSizer(int rows) {
            super("rows", "" + rows);
        }
    }
}
