001/*
002 * (C) Copyright 2014 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.component.holder;
020
021import java.io.IOException;
022
023import javax.faces.component.UIComponent;
024import javax.faces.context.FacesContext;
025import javax.faces.context.ResponseWriter;
026import javax.faces.convert.ConverterException;
027
028import com.sun.faces.renderkit.RenderKitUtils;
029import com.sun.faces.renderkit.html_basic.HiddenRenderer;
030
031/**
032 * Extend hidden renderer to provide client behaviours (onchange events management).
033 *
034 * @since 6.0
035 */
036public class ValueHolderRenderer extends HiddenRenderer {
037
038    @Override
039    public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue)
040            throws ConverterException {
041        // make sure submitted value is converted to String first
042        String submitted = null;
043        if (submittedValue != null) {
044            submitted = submittedValue.toString();
045        }
046        return super.getConvertedValue(context, component, submitted);
047    }
048
049    @Override
050    protected void getEndTextToRender(FacesContext context, UIComponent component, String currentValue)
051            throws IOException {
052
053        ResponseWriter writer = context.getResponseWriter();
054        assert (writer != null);
055
056        writer.startElement("input", component);
057        writeIdAttributeIfNecessary(context, writer, component);
058        writer.writeAttribute("type", "hidden", "type");
059        String clientId = component.getClientId(context);
060        writer.writeAttribute("name", clientId, "clientId");
061
062        // render default text specified
063        if (currentValue != null) {
064            writer.writeAttribute("value", currentValue, "value");
065        }
066
067        // Nuxeo patch
068        RenderKitUtils.renderOnchange(context, component, false);
069
070        writer.endElement("input");
071
072    }
073
074}