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 */ 044@Deprecated 045public class DefaultValueHolderRule extends MetaRule { 046 047 public static final DefaultValueHolderRule Instance = new DefaultValueHolderRule(); 048 049 static final class LiteralValueMetadata extends Metadata { 050 051 private final String value; 052 053 LiteralValueMetadata(String value) { 054 this.value = value; 055 } 056 057 @Override 058 public void applyMetadata(FaceletContext ctx, Object instance) { 059 ((ValueHolder) instance).setValue(value); 060 } 061 } 062 063 static final class DynamicDefaultValueExpressionMetadata extends Metadata { 064 065 private final TagAttribute attr; 066 067 DynamicDefaultValueExpressionMetadata(TagAttribute attr) { 068 this.attr = attr; 069 } 070 071 @Override 072 public void applyMetadata(FaceletContext ctx, Object instance) { 073 UIComponent comp = (UIComponent) instance; 074 ValueExpression defaultVe = attr.getValueExpression(ctx, Object.class); 075 ValueExpression ve = comp.getValueExpression("value"); 076 comp.setValueExpression("value", new DefaultValueExpression(ve, defaultVe)); 077 } 078 } 079 080 @Override 081 public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta) { 082 if (meta.isTargetInstanceOf(ValueHolder.class)) { 083 084 if ("defaultValue".equals(name)) { 085 if (attribute.isLiteral()) { 086 return new LiteralValueMetadata(attribute.getValue()); 087 } else { 088 return new DynamicDefaultValueExpressionMetadata(attribute); 089 } 090 } 091 092 } 093 return null; 094 } 095 096}