001/*
002 * (C) Copyright 2014 JBoss RichFaces and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017
018package org.nuxeo.ecm.platform.ui.web.component.radio;
019
020import java.util.ArrayList;
021import java.util.List;
022
023import javax.faces.component.UIComponent;
024import javax.faces.component.UIOutput;
025import javax.faces.component.behavior.ClientBehaviorHolder;
026import javax.faces.context.FacesContext;
027import javax.faces.model.SelectItem;
028
029import com.sun.faces.renderkit.SelectItemsIterator;
030
031/**
032 * Component representing a single radio button, referencing the original select and an index, for original button
033 * attributes retrieval.
034 *
035 * @since 6.0
036 */
037public class UIRadio extends UIOutput implements ClientBehaviorHolder {
038
039    public static final String COMPONENT_FAMILY = "org.nuxeo.Radio";
040
041    public static final String COMPONENT_TYPE = "org.nuxeo.Radio";
042
043    enum PropertyKeys {
044        forValue("for"), index, onChange;
045
046        String toString;
047
048        PropertyKeys(String toString) {
049            this.toString = toString;
050        }
051
052        PropertyKeys() {
053        }
054
055        @Override
056        public String toString() {
057            return ((toString != null) ? toString : super.toString());
058        }
059    }
060
061    @Override
062    public String getFamily() {
063        return COMPONENT_FAMILY;
064    }
065
066    public static List<SelectItem> getSelectItems(FacesContext context, UIComponent component) {
067        if (context == null) {
068            throw new IllegalArgumentException("Faces context is null");
069        }
070
071        ArrayList<SelectItem> list = new ArrayList<SelectItem>();
072        final SelectItemsIterator<SelectItem> iterator = new SelectItemsIterator<SelectItem>(context, component);
073        while (iterator.hasNext()) {
074            final SelectItem next = iterator.next();
075            list.add(new SelectItem(next.getValue(), next.getLabel(), next.getDescription(), next.isDisabled(),
076                    next.isEscape(), next.isNoSelectionOption()));
077        }
078        return list;
079    }
080
081    public String getFor() {
082        return (String) getStateHelper().eval(PropertyKeys.forValue);
083    }
084
085    public void setFor(String forValue) {
086        getStateHelper().put(PropertyKeys.forValue, forValue);
087    }
088
089    public Integer getIndex() {
090        return (Integer) getStateHelper().eval(PropertyKeys.index);
091    }
092
093    public void setIndex(Integer index) {
094        getStateHelper().put(PropertyKeys.index, index);
095    }
096
097    @SuppressWarnings("boxing")
098    public SelectItem getSelectItem(FacesContext context, UIComponent targetComponent) {
099        final List<SelectItem> list = getSelectItems(context, targetComponent);
100        try {
101            return list.get(getIndex());
102        } catch (IndexOutOfBoundsException e) {
103            throw new IllegalArgumentException("Component ''" + getId() + "'' has wrong value of index attribute ("
104                    + getIndex() + "). Target component ''" + targetComponent.getId() + "'' has only " + list.size()
105                    + " items.");
106        }
107    }
108
109}