001/*
002 * (C) Copyright 2013 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.tag.jsf;
020
021import javax.el.ValueExpression;
022import javax.faces.component.UIComponent;
023import javax.faces.component.ValueHolder;
024import javax.faces.view.facelets.FaceletContext;
025import javax.faces.view.facelets.MetaRule;
026import javax.faces.view.facelets.Metadata;
027import javax.faces.view.facelets.MetadataTarget;
028import javax.faces.view.facelets.TagAttribute;
029
030import org.nuxeo.ecm.platform.ui.web.binding.DefaultValueExpression;
031import org.nuxeo.ecm.platform.ui.web.tag.handler.GenericHtmlComponentHandler;
032
033/**
034 * Value holder rule, handling a default value using a {@link DefaultValueExpression} when a "defaultValue" attribute is
035 * set on the tag.
036 * <p>
037 * Assumes the standard value holder rule has already been processed, so that the corresponding value expression is
038 * already available on the component.
039 *
040 * @since 5.7.3
041 * @deprecated since 7.2: since this is a mixup between several attributes, this is not handled by rules anymore, see
042 *             {@link GenericHtmlComponentHandler#onComponentCreated(FaceletContext, UIComponent, UIComponent)}
043 */
044public class DefaultValueHolderRule extends MetaRule {
045
046    public static final DefaultValueHolderRule Instance = new DefaultValueHolderRule();
047
048    static final class LiteralValueMetadata extends Metadata {
049
050        private final String value;
051
052        LiteralValueMetadata(String value) {
053            this.value = value;
054        }
055
056        @Override
057        public void applyMetadata(FaceletContext ctx, Object instance) {
058            ((ValueHolder) instance).setValue(value);
059        }
060    }
061
062    static final class DynamicDefaultValueExpressionMetadata extends Metadata {
063
064        private final TagAttribute attr;
065
066        DynamicDefaultValueExpressionMetadata(TagAttribute attr) {
067            this.attr = attr;
068        }
069
070        @Override
071        public void applyMetadata(FaceletContext ctx, Object instance) {
072            UIComponent comp = (UIComponent) instance;
073            ValueExpression defaultVe = attr.getValueExpression(ctx, Object.class);
074            ValueExpression ve = comp.getValueExpression("value");
075            comp.setValueExpression("value", new DefaultValueExpression(ve, defaultVe));
076        }
077    }
078
079    @Override
080    public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta) {
081        if (meta.isTargetInstanceOf(ValueHolder.class)) {
082
083            if ("defaultValue".equals(name)) {
084                if (attribute.isLiteral()) {
085                    return new LiteralValueMetadata(attribute.getValue());
086                } else {
087                    return new DynamicDefaultValueExpressionMetadata(attribute);
088                }
089            }
090
091        }
092        return null;
093    }
094
095}