001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005//     http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.annotations;
014
015import org.apache.tapestry5.EventConstants;
016import org.apache.tapestry5.ioc.annotations.UseWith;
017
018import java.lang.annotation.Documented;
019import java.lang.annotation.ElementType;
020import java.lang.annotation.Retention;
021import java.lang.annotation.Target;
022
023import static java.lang.annotation.RetentionPolicy.RUNTIME;
024import static org.apache.tapestry5.ioc.annotations.AnnotationUseContext.*;
025
026/**
027 * Marks a method as a handler for a client side event. The handler method will be invoked when a component triggers an
028 * event. Filters on the type of event and on the originating component ensure that only the appropriate methods are
029 * invoked.
030 *
031 * Client events include a <em>context</em> of one or more values. These context values are included in the action URI.
032 * The values are optionally supplied to the handler method as parameters. Automatic {@linkplain
033 * org.apache.tapestry5.ValueEncoder conversion} from string to the type of the actual parameter occur.
034 *
035 * Handlers may return a value. Returning a non-null value will abort the handling of the event, and will usually
036 * control the response sent to the client web browser. The details are somewhat specific to the type of event and the
037 * component involved.
038 *
039 * If a handler is not found within the originating component (or no handler aborts the event handling), then handlers
040 * within the containing component will be searched. This continues up the page hierarchy. In some cases, having no
041 * handlers (or no aborting handlers) is considered acceptible; in others, it is an error. Again, this is defined by the
042 * type of originating component, and the type of event.
043 *
044 * <strong>If you fail to provide filters on either component or event type, then your method will be invoked for all
045 * component events, possibly including events that bubble up from embedded sub-components. </strong>
046 */
047@Target(ElementType.METHOD)
048@Retention(RUNTIME)
049@Documented
050@UseWith({COMPONENT,MIXIN,PAGE})
051public @interface OnEvent
052{
053
054    /**
055     * The event type to match. The handler will only be invoked if the client event type matches the value. The default
056     * value is "action".  Matching is case-insensitive.
057     *
058     * @see org.apache.tapestry5.EventConstants
059     */
060    String value() default EventConstants.ACTION;
061
062    /**
063     * The local id of the component from which the event originates. If not specified, then the default is to match any
064     * component. If an event from a component is not handled in the component's container, it is re-triggered inside
065     * the component's grand-container and will appear to originate from the container. Thus events that escape a
066     * component will appear to originate in the component's container, and so forth.
067     *
068     * Matching by component id is case insensitive.
069     */
070    String component() default "";
071}