001    // Copyright 2006, 2007, 2008 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    
015    package org.apache.tapestry5;
016    
017    import org.apache.tapestry5.ioc.AnnotationProvider;
018    import org.apache.tapestry5.ioc.Messages;
019    import org.apache.tapestry5.ioc.Resource;
020    import org.apache.tapestry5.model.ComponentModel;
021    import org.apache.tapestry5.runtime.Component;
022    import org.apache.tapestry5.runtime.PageLifecycleListener;
023    
024    import java.lang.annotation.Annotation;
025    import java.util.List;
026    
027    /**
028     * Provides a component instance with the resources provided by the framework. In many circumstances, the resources
029     * object can be considered the component itself; in others, it is the {@linkplain #getComponent() component property},
030     * an instance of a class provided by the application developer (though transformed in many ways while being loaded)
031     * that is the true component. In reality, it is the combination of the resources object with the user class instance
032     * that forms the components; neither is useful without the other.
033     */
034    public interface ComponentResources extends ComponentResourcesCommon
035    {
036        /**
037         * Returns the base resource for the component, which will represent the class's location within the classpath (this
038         * is used to resolve relative assets).
039         */
040        Resource getBaseResource();
041    
042        /**
043         * Returns the component model object that defines the behavior of the component.
044         */
045        ComponentModel getComponentModel();
046    
047        /**
048         * Returns the component this object provides resources for.
049         */
050        Component getComponent();
051    
052        /**
053         * Returns the component which contains this component, or null for the root component. For mixins, this returns the
054         * componet to which the mixin is attached.
055         */
056        Component getContainer();
057    
058        /**
059         * Returns the {@link ComponentResources} for the container, or null if the this is the root component (that has no
060         * container). As a special case, for a mixin, this returns the core component's resources.
061         */
062        ComponentResources getContainerResources();
063    
064        /**
065         * Returns the {@link Messages} from the container, or null if this is the root component (with no container). As a
066         * special case, for a mixin, this return the core component's messages.
067         */
068        Messages getContainerMessages();
069    
070        /**
071         * Returns the page that contains this component. Technically, the page itself is an internal object in Tapestry and
072         * this returns the root component of the actual page, but from an application developer point of view, this is the
073         * page.
074         */
075        Component getPage();
076    
077        /**
078         * Returns an embedded component, given the component's id.
079         *
080         * @param embeddedId selects the embedded component (case is ignored)
081         * @throws IllegalArgumentException if this component does not contain a component with the given id
082         */
083    
084        Component getEmbeddedComponent(String embeddedId);
085    
086        /**
087         * Returns true if the named parameter is bound, false if not.
088         */
089        boolean isBound(String parameterName);
090    
091        /**
092         * Obtains an annotation provided by a parameter.
093         *
094         * @param parameterName  name of parameter to search for the annotation
095         * @param annotationType the type of annotation
096         * @return the annotation if found or null otherwise
097         */
098        <T extends Annotation> T getParameterAnnotation(String parameterName, Class<T> annotationType);
099    
100        /**
101         * Indentifies all parameters that are not formal parameters and writes each as a attribute/value pair into the
102         * current element of the markup writer.
103         *
104         * @param writer to which {@link MarkupWriter#attributes(Object[]) attributes} will be written
105         */
106        void renderInformalParameters(MarkupWriter writer);
107    
108        /**
109         * Returns the message catalog for this component.
110         */
111        Messages getMessages();
112    
113        /**
114         * Returns the actual type of the bound parameter, or null if the parameter is not bound. This is primarily used
115         * with property bindings, and is used to determine the actual type of the property, rather than the type of
116         * parameter (remember that type coercion automatically occurs, which can mask significant differences between the
117         * parameter type and the bound property type).
118         *
119         * @param parameterName used to select the parameter (case is ignored)
120         * @return the type of the bound parameter, or null if the parameter is not bound
121         * @see Binding#getBindingType()
122         */
123        Class getBoundType(String parameterName);
124    
125        /**
126         * Returns an annotation provider, used to obtain annotations related to the parameter.
127         *
128         * @param parameterName used to select the parameter (case is ignored)
129         * @return the annotation provider, or null if the parameter is not bound
130         */
131        AnnotationProvider getAnnotationProvider(String parameterName);
132    
133        /**
134         * Used to access an informal parameter that's a Block.
135         *
136         * @param parameterName the name of the informal parameter (case is ignored)
137         * @return the informal Block parameter, or null if not bound
138         */
139        Block getBlockParameter(String parameterName);
140    
141        /**
142         * Returns a previously stored render variable.
143         *
144         * @param name of the variable (case will be ignored)
145         * @return the variable's value
146         * @throws IllegalArgumentException if the name doesn't correspond to a stored value
147         */
148        Object getRenderVariable(String name);
149    
150        /**
151         * Stores a render variable, accessible with the provided name.
152         *
153         * @param name  of value to store
154         * @param value value to store (may not be null)
155         * @throws IllegalStateException if the component is not currently rendering
156         */
157        void storeRenderVariable(String name, Object value);
158    
159        /**
160         * Adds a listener object that will be notified about page lifecycle events.
161         */
162        void addPageLifecycleListener(PageLifecycleListener listener);
163    
164    
165        /**
166         * Discards all persistent field changes for the page containing the component.  Changes are eliminated from
167         * persistent storage (such as the {@link org.apache.tapestry5.services.Session}) which will take effect in the
168         * <em>next</em> request (the attached page instance is not affected).
169         */
170        void discardPersistentFieldChanges();
171    
172        /**
173         * Returns the name of element that represents the component in its template, or null if not known.
174         *
175         * @return the element name or null
176         */
177        String getElementName();
178    
179        /**
180         * Returns a list of the names of any informal parameters bound to this component.
181         *
182         * @return the name sorted alphabetically
183         * @see org.apache.tapestry5.annotations.SupportsInformalParameters
184         */
185        List<String> getInformalParameterNames();
186    
187        /**
188         * Reads an informal parameter and {@linkplain org.apache.tapestry5.ioc.services.TypeCoercer coercers) the bound
189         * value to the indicated type.
190         *
191         * @param name name of informal parameter
192         * @param type output value type
193         * @return instance of type
194         */
195        <T> T getInformalParameter(String name, Class<T> type);
196    }