001    // Copyright May 14, 2006 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    package org.apache.tapestry.annotations;
015    
016    import java.lang.reflect.Method;
017    
018    import org.apache.hivemind.ApplicationRuntimeException;
019    import org.apache.hivemind.Resource;
020    import org.apache.tapestry.enhance.EnhancementOperation;
021    import org.apache.tapestry.services.impl.ComponentEventInvoker;
022    import org.apache.tapestry.spec.IComponentSpecification;
023    
024    
025    /**
026     * Performs {@link EventListener} annotation enhancements on components.
027     * 
028     * @author jkuhnert
029     */
030    public class EventListenerAnnotationWorker implements SecondaryAnnotationWorker
031    {
032        private ComponentEventInvoker _invoker;
033        
034        /** 
035         * {@inheritDoc}
036         */
037        public boolean canEnhance(Method method)
038        {
039            return method.getAnnotation(EventListener.class) != null;
040        }
041        
042        /** 
043         * {@inheritDoc}
044         */
045        public void peformEnhancement(EnhancementOperation op, IComponentSpecification spec, 
046                Method method, Resource classResource)
047        {
048            EventListener listener = method.getAnnotation(EventListener.class);
049            
050            String[] targets = listener.targets();
051            String[] elements = listener.elements();
052            String formId = listener.submitForm();
053            boolean validateForm = listener.validateForm();
054            boolean async = listener.async();
055            
056            if (targets.length < 1 && elements.length < 1)
057                throw new ApplicationRuntimeException(AnnotationMessages.targetsNotFound(method));
058            
059            for (int i=0; i < targets.length; i++) {
060                
061                _invoker.addEventListener(targets[i], listener.events(), 
062                        method.getName(), formId, validateForm, async);
063                
064            }
065            
066            for (int i=0; i < elements.length; i++)
067                _invoker.addElementEventListener(elements[i], listener.events(), 
068                        method.getName(), formId, validateForm, async);
069        }
070        
071        /**
072         * Injected.
073         * @param invoker
074         */
075        public void setComponentEventInvoker(ComponentEventInvoker invoker)
076        {
077            _invoker = invoker;
078        }
079    }