001/*
002 * (C) Copyright 2007 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id: MetaActionSourceRule.java 21703 2007-07-01 20:48:16Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.ui.web.tag.jsf;
023
024import javax.el.MethodExpression;
025import javax.faces.component.ActionSource;
026import javax.faces.component.ActionSource2;
027import javax.faces.view.facelets.FaceletContext;
028import javax.faces.view.facelets.MetaRule;
029import javax.faces.view.facelets.Metadata;
030import javax.faces.view.facelets.MetadataTarget;
031import javax.faces.view.facelets.TagAttribute;
032
033import org.nuxeo.ecm.platform.ui.web.binding.MetaMethodExpression;
034
035/**
036 * Meta rule set that wires a method binding to a {@link MetaMethodBinding} when invoking the method.
037 *
038 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
039 */
040public class MetaActionSourceRule extends MetaRule {
041
042    public static final Class[] ACTION_SIG = new Class[0];
043
044    public static final MetaActionSourceRule Instance = new MetaActionSourceRule();
045
046    static final class ActionExpressionMapper extends Metadata {
047
048        private final TagAttribute attr;
049
050        ActionExpressionMapper(TagAttribute attr) {
051            this.attr = attr;
052        }
053
054        @Override
055        public void applyMetadata(FaceletContext ctx, Object instance) {
056            ActionSource2 as = (ActionSource2) instance;
057            MethodExpression originalExpression = attr.getMethodExpression(ctx, String.class, ACTION_SIG);
058            as.setActionExpression(
059                    new MetaMethodExpression(originalExpression, ctx.getFunctionMapper(), ctx.getVariableMapper()));
060        }
061    }
062
063    @Override
064    public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta) {
065        if (meta.isTargetInstanceOf(ActionSource.class)) {
066            if ("action".equals(name)) {
067                return new ActionExpressionMapper(attribute);
068            }
069        }
070        return null;
071    }
072}