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
015package org.apache.tapestry5.internal.transform;
016
017import org.apache.tapestry5.ioc.Invokable;
018import org.apache.tapestry5.ioc.OperationTracker;
019import 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 */
028public class EventHandlerMethodParameterSource
029{
030    private static final class ParameterExtractor implements Invokable<Object> {
031        private final EventHandlerMethodParameterProvider[] providers;
032        private final int index;
033        private final ComponentEvent event;
034
035        private ParameterExtractor(EventHandlerMethodParameterProvider[] providers, int index, ComponentEvent event) {
036            this.providers = providers;
037            this.index = index;
038            this.event = event;
039        }
040
041        public Object invoke()
042        {
043            return providers[index].valueForEventHandlerMethodParameter(event);
044        }
045    }
046
047    private final String methodIdentifier;
048
049    private final OperationTracker operationTracker;
050
051    private final EventHandlerMethodParameterProvider[] providers;
052
053    public EventHandlerMethodParameterSource(String methodIdentifier, OperationTracker operationTracker, EventHandlerMethodParameterProvider[] providers)
054    {
055
056        this.methodIdentifier = methodIdentifier;
057        this.operationTracker = operationTracker;
058        this.providers = providers;
059    }
060
061    public Object get(final ComponentEvent event, final int index)
062    {
063        // Hopefully this will not be too much overhead; it's really nice to be able to track what parameter
064        // caused a failure.
065
066        return operationTracker.invoke("Obtaining value for parameter #" + (index + 1) + " of "+ methodIdentifier,
067                new ParameterExtractor(providers, index, event));
068    }
069}