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;
014
015import org.apache.tapestry5.ioc.Locatable;
016import org.apache.tapestry5.services.pageload.ComponentResourceSelector;
017import org.slf4j.Logger;
018
019import java.util.Locale;
020
021/**
022 * Operations shared by the public {@link org.apache.tapestry5.ComponentResources} interface and
023 * {@link org.apache.tapestry5.internal.structure.ComponentPageElement} interface (on the internal side).
024 */
025@SuppressWarnings(
026{ "JavaDoc" })
027public interface ComponentResourcesCommon extends Locatable
028{
029    /**
030     * Returns the simple (or local) id of the component. The id will be unique within the component's immediate
031     * container. For a page's root component, the value null is returned.
032     */
033    String getId();
034
035    /**
036     * Return a string consisting the concatenated ids of all containing components, separated by periods. In addition,
037     * nested ids are always all lower case. I.e., "foo.bar.baz". Returns null for the root component of a page.
038     */
039    String getNestedId();
040
041    /**
042     * Returns a string consisting of the logical name of the containing page, and the {@link #getNestedId() nested id}
043     * of this component, separated by a colon. I.e., "MyPage:foo.bar.baz". For a page, returns just the page's name.
044     *
045     * This value is often used to obtain an equivalent component instance in a later request.
046     * 
047     * @see org.apache.tapestry5.services.ComponentSource#getComponent(String)
048     */
049    String getCompleteId();
050
051    /**
052     * A convenience method for invoking {@link #triggerContextEvent(String, EventContext , ComponentEventCallback)}. Wraps
053     * the context values into an {@link org.apache.tapestry5.EventContext}.
054     * 
055     * @param eventType
056     *            event type (as determined from the request, or otherwise by design)
057     * @param contextValues
058     *            Values that may be provided to the event handler method as method parameters, or null if no
059     *            context values are available
060     * @param callback
061     *            the handler to be informed of the result, or null if the event is a notification that does
062     *            not support return values from event handler methods (the value true is allowed even if the
063     *            handler is null).
064     * @return true if any event handler was invoked (even if no event handler method returns a non-null value)
065     * @throws org.apache.tapestry5.runtime.ComponentEventException
066     *             if an event handler method throws a checked or unchecked exception
067     * @see org.apache.tapestry5.internal.transform.OnEventWorker
068     * @see org.apache.tapestry5.annotations.OnEvent
069     */
070    boolean triggerEvent(String eventType, Object[] contextValues, ComponentEventCallback callback);
071
072    /**
073     * Triggers a component event. A search for an event handling method will occur, first in the component, then its
074     * container, and so on. When a matching event handler method is located, it is invoked. If the method returns a
075     * value, the value is passed to the callback (if callback is null, then it is an error for a method to return a
076     * non-null value).
077     *
078     * Resolution of event type to event handler methods is case insensitive.
079     * 
080     * @param eventType
081     *            event type (as determined from the request, or otherwise by design)
082     * @param context
083     *            the context (as extracted from the request, or provided by the triggering component); these
084     *            values may be provided to event handler methods via their parameters (may not be null)
085     * @param callback
086     *            the handler to be informed of the result, or null if the event is a notification that does not
087     *            support return values from event handler methods (the value true is allowed even if the handler
088     *            is null).
089     * @return true if any event handler was invoked (even if no event handler method returns a non-null value)
090     * @throws org.apache.tapestry5.runtime.ComponentEventException
091     *             if an event handler method throws a checked or unchecked exception
092     * @see org.apache.tapestry5.internal.transform.OnEventWorker
093     * @see org.apache.tapestry5.annotations.OnEvent
094     */
095    boolean triggerContextEvent(String eventType, EventContext context, ComponentEventCallback callback);
096
097    /**
098     * Returns true if the component is currently rendering, false otherwise. This is most often used to determine if
099     * parameter values should be cached.
100     */
101    boolean isRendering();
102
103    /**
104     * Returns the log instance associated with the component (which is based on the component or mixin's class name).
105     * 
106     * @see org.apache.tapestry5.model.ComponentModel#getLogger()
107     */
108    Logger getLogger();
109
110    /**
111     * Returns the locale for the page containing this component.
112     * 
113     * @see #getResourceSelector()
114     */
115    Locale getLocale();
116
117    /**
118     * Returns the selector used when constructing the component and its containing page.
119     * 
120     * @since 5.3
121     */
122    ComponentResourceSelector getResourceSelector();
123
124    /**
125     * Returns the name of element that represents the component in its template, or the provided default element name
126     * if the element was a component type (in the Tapestry namespace).
127     * 
128     * @param defaultElementName
129     *            element name to return if the element name is not known (may be null)
130     * @return the element name
131     */
132    String getElementName(String defaultElementName);
133
134    /**
135     * Returns a block from the component's template, referenced by its id.
136     * 
137     * @param blockId
138     *            the id of the block (case insensitive)
139     * @return the identified Block
140     * @throws BlockNotFoundException
141     *             if no block with the given id exists
142     * @see #findBlock(String)
143     */
144    Block getBlock(String blockId);
145
146    /**
147     * As with {@link #getBlock(String)}, but returns null if the block is not found.
148     * 
149     * @param blockId
150     *            the id of the block (case insensitive)
151     * @return the block, or null
152     */
153    Block findBlock(String blockId);
154
155    /**
156     * Returns the <em>logical</em> name of the page containing this component. This is the short name (it often appears
157     * in URLs)
158     * 
159     * @return the logical name of the page which contains this component
160     */
161    String getPageName();
162
163    /**
164     * Returns true if the element has a body and false otherwise. Only components may have a body; pages and mixins
165     * will return false.
166     */
167    boolean hasBody();
168
169    /**
170     * Returns the body of this component as a (possibly empty) block. When invoked on a mixin, returns the containing
171     * component's body.
172     */
173    Block getBody();
174
175    /**
176     * Creates a component event request link as a callback for this component. The event type and context (as well as
177     * the page name and nested component id) will be encoded into a URL. A request for the URL will
178     * {@linkplain #triggerEvent(String, Object[], org.apache.tapestry5.ComponentEventCallback)} trigger} the named
179     * event on the
180     * component.
181     * 
182     * @param eventType
183     *            the type of event to be triggered. Event types should be Java identifiers (contain only
184     *            letters, numbers and the underscore).
185     * @param context
186     *            additional objects to be encoded into the path portion of the link; each is converted to a
187     *            string and URI encoded
188     * @return link object for the callback
189     */
190    Link createEventLink(String eventType, Object... context);
191
192    /**
193     * Creates a component event request link as a callback for this component. The event type and context (as well as
194     * the page name and nested component id) will be encoded into a URL. A request for the URL will
195     * {@linkplain #triggerEvent(String, Object[], org.apache.tapestry5.ComponentEventCallback)} trigger} the named
196     * event on the
197     * component. This is only used for form submission events, as extra data may be encoded in the form as hidden
198     * fields.
199     * 
200     * @param eventType
201     *            the type of event to be triggered. Event types should be Java identifiers (contain only
202     *            letters, numbers and the underscore).
203     * @param context
204     *            additional objects to be encoded into the path portion of the link; each is converted to a
205     *            string and URI encoded
206     * @return link object for the callback
207     */
208    Link createFormEventLink(String eventType, Object... context);
209}