001/* 002 * (C) Copyright 2006-2010 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 * <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.ui.web.tag.handler; 023 024import java.io.IOException; 025 026import javax.el.ELException; 027import javax.faces.FacesException; 028import javax.faces.component.ActionSource; 029import javax.faces.component.UIComponent; 030import javax.faces.event.ActionEvent; 031import javax.faces.event.ActionListener; 032import javax.faces.event.MethodExpressionActionListener; 033import javax.faces.view.facelets.FaceletContext; 034import javax.faces.view.facelets.FaceletException; 035import javax.faces.view.facelets.TagAttribute; 036import javax.faces.view.facelets.TagConfig; 037import javax.faces.view.facelets.TagException; 038import javax.faces.view.facelets.TagHandler; 039 040import org.nuxeo.ecm.platform.ui.web.binding.MetaMethodExpression; 041 042/** 043 * Action listener method handler: makes it possible to add an action listener method to any action source component 044 * parent. 045 * <p> 046 * This is useful when declaring several action listeners on the same parent component, and when the order of calls 047 * needs to be respected: the action listener method declared on a component is the first one called. So this method 048 * makes it possible to add other action listeners before it, without having to declare a class (when using the 049 * f:actionListener tag). 050 * 051 * @author Anahide Tchertchian 052 */ 053public class ActionListenerMethodTagHandler extends TagHandler { 054 055 protected final TagAttribute value; 056 057 @SuppressWarnings("rawtypes") 058 public static final Class[] ACTION_LISTENER_SIG = { ActionEvent.class }; 059 060 public ActionListenerMethodTagHandler(TagConfig config) { 061 super(config); 062 value = getRequiredAttribute("value"); 063 } 064 065 @Override 066 public void apply(FaceletContext ctx, UIComponent parent) 067 throws IOException, FacesException, FaceletException, ELException { 068 if (parent instanceof ActionSource) { 069 // only process if parent was just created 070 if (parent.getParent() == null) { 071 ActionSource src = (ActionSource) parent; 072 ActionListener listener = new MethodExpressionActionListener( 073 new MetaMethodExpression(value.getMethodExpression(ctx, null, ACTION_LISTENER_SIG), 074 ctx.getFunctionMapper(), ctx.getVariableMapper())); 075 src.addActionListener(listener); 076 } 077 } else { 078 throw new TagException(tag, "Parent is not of type ActionSource, type is: " + parent); 079 } 080 } 081 082}