001// Copyright 2006, 2007, 2008, 2010 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
015package org.apache.tapestry5.runtime;
016
017import org.apache.tapestry5.ComponentResourcesCommon;
018import org.apache.tapestry5.EventContext;
019
020/**
021 * An event that may originate in application logic, or as a result of a client interaction (a GET or POST from the
022 * client).
023 * 
024 * @see ComponentResourcesCommon#triggerEvent(String, Object[], org.apache.tapestry5.ComponentEventCallback)
025 * @see org.apache.tapestry5.ComponentEventCallback
026 */
027public interface ComponentEvent extends Event
028{
029    /**
030     * Returns true if the event matches the provided criteria and the event has not yet been aborted.
031     * 
032     * @param eventType
033     *            the type of event (case insensitive match)
034     * @param componentId
035     *            component is to match against (case insensitive), or the empty string
036     * @param parameterCount
037     *            minimum number of context values
038     * @return true if the event matches (and has not yet been aborted)
039     */
040    boolean matches(String eventType, String componentId, int parameterCount);
041
042    /**
043     * Returns true if the event matches the provided criteria and the event has not yet been aborted.
044     * 
045     * @param eventType
046     *            the type of event (case insensitive match)
047     * @param componentId
048     *            component is to match against (case insensitive), or the empty string
049     * @param parameterCount
050     *            minimum number of context values
051     * @param staticActivationContextValues
052     *            a String array. If null, there are no static activation context values. If
053     *            any value in the array is null, it's considered dynamic and ignored.
054     *            I any value in the arra isn't null, it's compared to the corresponding
055     *            activation context value. If it doesn't match, this method will return null.
056     * @return true if the event matches (and has not yet been aborted)
057     * @since 5.8.0
058     */
059    default boolean matches(String eventType, String componentId, int parameterCount, String[] staticActivationContextValues)
060    {
061        boolean matches = matches(eventType, componentId, parameterCount);
062        if (matches && staticActivationContextValues != null)
063        {
064            for (int i = 0; i < staticActivationContextValues.length; i++)
065            {
066                if (staticActivationContextValues[i] != null && 
067                        !staticActivationContextValues[i].equals(getEventContext().get(String.class, i)))
068                {
069                    matches = false;
070                    break;
071                }
072            }
073        }
074        return matches;
075    }
076
077    /**
078     * Coerces a context value to a particular type. The context is an array of objects; typically it is an array of
079     * strings of extra path information encoded into the action URL.
080     * 
081     * @param index
082     *            the index of the context value
083     * @param desiredTypeName
084     *            the desired type
085     * @return the coerced value (a wrapper type if the desired type is a primitive)
086     */
087    Object coerceContext(int index, String desiredTypeName);
088
089    /**
090     * Returns the underlying {@link org.apache.tapestry5.EventContext} as a (possibly empty) array.
091     */
092    Object[] getContext();
093
094    /**
095     * Returns the underlying event context.
096     */
097    EventContext getEventContext();
098}