001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.ui.web.renderer;
020
021import javax.faces.component.UIComponent;
022import javax.faces.component.ValueHolder;
023import javax.faces.context.FacesContext;
024import javax.faces.convert.Converter;
025import javax.faces.convert.ConverterException;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029
030import com.sun.faces.renderkit.html_basic.CheckboxRenderer;
031import com.sun.faces.util.RequestStateManager;
032
033/**
034 * Renderer that does not ignore the converter set on the component on submit
035 *
036 * @author Anahide Tchertchian
037 */
038public class NXCheckboxRenderer extends CheckboxRenderer {
039
040    private static final Log log = LogFactory.getLog(NXCheckboxRenderer.class);
041
042    public static final String RENDERER_TYPE = "javax.faces.NXCheckbox";
043
044    @Override
045    public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue)
046            throws ConverterException {
047
048        String newValue = null;
049        if (submittedValue instanceof Boolean) {
050            newValue = ((Boolean) submittedValue).toString();
051        } else if (submittedValue instanceof String) {
052            newValue = (String) submittedValue;
053        } else if (submittedValue != null) {
054            log.error("Unsupported submitted value, should be a string or boolean: '" + submittedValue
055                    + "' => using false");
056        }
057
058        Converter converter = null;
059        // If there is a converter attribute, use it to to ask application
060        // instance for a converter with this identifer.
061        if (component instanceof ValueHolder) {
062            converter = ((ValueHolder) component).getConverter();
063        }
064
065        if (converter != null) {
066            // If the conversion eventually falls to needing to use EL type
067            // coercion,
068            // make sure our special ConverterPropertyEditor knows about this
069            // value.
070            RequestStateManager.set(context, RequestStateManager.TARGET_COMPONENT_ATTRIBUTE_NAME, component);
071            return converter.getAsObject(context, component, newValue);
072        } else {
073            return Boolean.valueOf(newValue);
074        }
075    }
076
077}