001    //
002    // Copyright 2011 The Apache Software Foundation
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    
015    package org.apache.tapestry5.internal.transform;
016    
017    import org.apache.tapestry5.ioc.Invokable;
018    import org.apache.tapestry5.ioc.OperationTracker;
019    import org.apache.tapestry5.runtime.ComponentEvent;
020    
021    /**
022     * Used  to encapsulate the list of {@link EventHandlerMethodParameterProvider}s for a particular
023     * method of a particular component, providing {@link OperationTracker} behavior as parameter values
024     * are obtained/computed/coerced.
025     *
026     * @since 5.3
027     */
028    public class EventHandlerMethodParameterSource
029    {
030        private final String methodIdentifier;
031    
032        private final OperationTracker operationTracker;
033    
034        private final EventHandlerMethodParameterProvider[] providers;
035    
036        public EventHandlerMethodParameterSource(String methodIdentifier, OperationTracker operationTracker, EventHandlerMethodParameterProvider[] providers)
037        {
038    
039            this.methodIdentifier = methodIdentifier;
040            this.operationTracker = operationTracker;
041            this.providers = providers;
042        }
043    
044        public Object get(final ComponentEvent event, final int index)
045        {
046            // Hopefully this will not be too much overhead; it's really nice to be able to track what parameter
047            // caused a failure.
048    
049            return operationTracker.invoke(String.format("Obtaining value for parameter #%d of %s", index + 1, methodIdentifier),
050                    new Invokable<Object>()
051                    {
052                        public Object invoke()
053                        {
054                            return providers[index].valueForEventHandlerMethodParameter(event);
055                        }
056                    });
057        }
058    }