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