001// Copyright 2006, 2009, 2011 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.MarkupWriter;
018import org.apache.tapestry5.annotations.OnEvent;
019
020/**
021 * Interface that defines the lifecycle of a component, within a page, allowing for callbacks into the component for
022 * many different events. This interface is part of the public API for Tapestry, but is <em>not</em> expected to be
023 * directly implemented by component classes; it should only be implemented as part of the component class
024 * transformation process.
025 * <p/>
026 * Most of the methods are related to render phases; see the corresponding annotations and component rendering
027 * documentation to see how they relate to each other.
028 * <p/>
029 * Starting in 5.3 this interface no longer implements {@link PageLifecycleListener}. Normally, this would be an incompatible
030 * change, but Component is not supposed to be directly implemented by user code.
031 */
032public interface Component extends ComponentResourcesAware
033{
034
035    /**
036     * Lifecycle method invoked at the end of the {@link org.apache.tapestry5.annotations.CleanupRender} render phase.
037     * There is no annotation for this method, it is part of CleanupRender, but is always invoked. Its specific use is
038     * to allow components to clean up cached parameter values.
039     */
040    void postRenderCleanup();
041
042    /**
043     * Invoked before rendering a component (or its template).
044     */
045    void setupRender(MarkupWriter writer, Event event);
046
047    /**
048     * Invoked to allow a component to render its tag (start tag and attributes).
049     */
050    void beginRender(MarkupWriter writer, Event event);
051
052    /**
053     * This phase is only invoked for components with templates.
054     */
055    void beforeRenderTemplate(MarkupWriter writer, Event event);
056
057    /**
058     * Invoked after rendering the template for a component (only for components with a template).
059     */
060    void afterRenderTemplate(MarkupWriter writer, Event event);
061
062    /**
063     * Invoked just before rendering the body of component.
064     */
065    void beforeRenderBody(MarkupWriter writer, Event event);
066
067    /**
068     * Invoked just after rendering the body of the component.
069     */
070    void afterRenderBody(MarkupWriter writer, Event event);
071
072    /**
073     * Generally used to write the close tag matching any open tag written by {@link
074     * #beginRender(org.apache.tapestry5.MarkupWriter, Event)}.
075     */
076    void afterRender(MarkupWriter writer, Event event);
077
078    /**
079     * Generally used to perform final cleanup of the component after rendering.
080     */
081    void cleanupRender(MarkupWriter writer, Event event);
082
083    /**
084     * Invoked to handle a component event. Methods with the {@link OnEvent} annotation (or the matching naming
085     * convention) will be invoked until one returns a non-null value.
086     *
087     * @param event
088     * @return true if any handler was found (and invoked), false otherwise
089     * @throws RuntimeException wrapping any checked exceptions that are thrown by individual event handler methods
090     */
091    boolean dispatchComponentEvent(ComponentEvent event);
092}