/*
 * 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 java.util.Arrays;
import java.util.List;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.RadioChoice;

/**
 * Class capable of displaying boolean choices. I.e. it provides the choices
 * "Yes" and "No".
 *
 * @author s_wolff
 * @see AbstractFormField
 * @see RadioChoice
 */
public class YesNoChoice<T extends Serializable> extends AbstractFormField<T> {

    /**
     * Create a new choice form field.
     *
     * @param id the markup comonent 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 YesNoChoice(String id, T obj, String fieldName, String labelText, boolean required) {
        super(id, obj, createChoiceComponent(), fieldName, labelText, required);
    }

    private static FormComponent createChoiceComponent() {
        List<Boolean> choices = Arrays.asList(true, false);
        RadioChoice<Boolean> comp = new RadioChoice<Boolean>(AbstractFormField.ID_INPUT, choices,
                new ChoiceRenderer<Boolean>() {

                    @Override
                    public Object getDisplayValue(Boolean b) {
                        return b ? "Yes" : "No";
                    }
                });
        return comp;
    }
}
