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 org.apache.hivemind.ApplicationRuntimeException;
017    import org.apache.hivemind.Resource;
018    import org.apache.tapestry.enhance.EnhancementOperation;
019    import org.apache.tapestry.spec.IComponentSpecification;
020    
021    import java.lang.reflect.Method;
022    
023    
024    /**
025     * Performs {@link EventListener} annotation enhancements on components.
026     *
027     * @author jkuhnert
028     */
029    public class EventListenerAnnotationWorker implements SecondaryAnnotationWorker
030    {
031        /**
032         * {@inheritDoc}
033         */
034        public boolean canEnhance(Method method)
035        {
036            return method.getAnnotation(EventListener.class) != null;
037        }
038    
039        /**
040         * {@inheritDoc}
041         */
042        public void peformEnhancement(EnhancementOperation op, IComponentSpecification spec, Method method, Resource classResource)
043        {
044            EventListener listener = method.getAnnotation(EventListener.class);
045    
046            String[] targets = listener.targets();
047            String[] elements = listener.elements();
048            String formId = listener.submitForm();
049            boolean validateForm = listener.validateForm();
050            boolean async = listener.async();
051            boolean focus = listener.focus();
052            boolean autoSubmit = listener.autoSubmit();
053    
054            if (targets.length < 1 && elements.length < 1)
055                throw new ApplicationRuntimeException(AnnotationMessages.targetsNotFound(method));
056    
057            for (int i=0; i < targets.length; i++)
058            {
059                spec.addEventListener(targets[i], listener.events(),
060                                      method.getName(), formId, validateForm, async, focus, autoSubmit);
061            }
062    
063            for (int i=0; i < elements.length; i++)
064            {
065                spec.addElementEventListener(elements[i], listener.events(),
066                                             method.getName(), formId, validateForm, async, focus);
067            }
068        }
069    }