001/*
002 * (C) Copyright 2007 Nuxeo SAS (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 *     Nuxeo - initial API and implementation
016 *
017 * $Id: GenericHtmlComponentHandler.java 28610 2008-01-09 17:13:52Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.ui.web.tag.handler;
021
022import javax.el.ValueExpression;
023import javax.faces.component.UIComponent;
024import javax.faces.component.ValueHolder;
025import javax.faces.view.facelets.ComponentConfig;
026import javax.faces.view.facelets.FaceletContext;
027
028import org.nuxeo.ecm.platform.ui.web.binding.DefaultValueExpression;
029
030import com.sun.faces.facelets.tag.jsf.html.HtmlComponentHandler;
031
032/**
033 * Generic HTML component handler.
034 * <p>
035 * Handler that manages a defaultValue attribute set on the tag, to default to this value when value is null or empty.
036 *
037 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
038 */
039public class GenericHtmlComponentHandler extends HtmlComponentHandler {
040
041    public GenericHtmlComponentHandler(ComponentConfig config) {
042        super(config);
043    }
044
045    /**
046     * Overrides the "value" value expression to handle default value mapping.
047     */
048    public void onComponentCreated(FaceletContext ctx, UIComponent c, UIComponent parent) {
049        if (c instanceof ValueHolder) {
050            ValueExpression dve = c.getValueExpression("defaultValue");
051            if (dve != null) {
052                c.setValueExpression("value", new DefaultValueExpression(c.getValueExpression("value"), dve));
053            }
054        }
055    }
056
057}