001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) 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.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 */
017package org.nuxeo.ecm.platform.ui.web.renderer;
018
019import javax.faces.component.UIComponent;
020import javax.faces.component.ValueHolder;
021import javax.faces.context.FacesContext;
022import javax.faces.convert.Converter;
023import javax.faces.convert.ConverterException;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027
028import com.sun.faces.renderkit.html_basic.CheckboxRenderer;
029import com.sun.faces.util.RequestStateManager;
030
031/**
032 * Renderer that does not ignore the converter set on the component on submit
033 *
034 * @author Anahide Tchertchian
035 */
036public class NXCheckboxRenderer extends CheckboxRenderer {
037
038    private static final Log log = LogFactory.getLog(NXCheckboxRenderer.class);
039
040    public static final String RENDERER_TYPE = "javax.faces.NXCheckbox";
041
042    @Override
043    public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue)
044            throws ConverterException {
045
046        String newValue = null;
047        if (submittedValue instanceof Boolean) {
048            newValue = ((Boolean) submittedValue).toString();
049        } else if (submittedValue instanceof String) {
050            newValue = (String) submittedValue;
051        } else if (submittedValue != null) {
052            log.error("Unsupported submitted value, should be a string or boolean: '" + submittedValue
053                    + "' => using false");
054        }
055
056        Converter converter = null;
057        // If there is a converter attribute, use it to to ask application
058        // instance for a converter with this identifer.
059        if (component instanceof ValueHolder) {
060            converter = ((ValueHolder) component).getConverter();
061        }
062
063        if (converter != null) {
064            // If the conversion eventually falls to needing to use EL type
065            // coercion,
066            // make sure our special ConverterPropertyEditor knows about this
067            // value.
068            RequestStateManager.set(context, RequestStateManager.TARGET_COMPONENT_ATTRIBUTE_NAME, component);
069            return converter.getAsObject(context, component, newValue);
070        } else {
071            return Boolean.valueOf(newValue);
072        }
073    }
074
075}