Class ComponentPageElementImpl
- java.lang.Object
-
- org.apache.tapestry5.ioc.BaseLocatable
-
- org.apache.tapestry5.internal.structure.ComponentPageElementImpl
-
- All Implemented Interfaces:
Locatable,ComponentResourcesCommon,InternalComponentResourcesCommon,BodyPageElement,ComponentPageElement,RenderCommand
public class ComponentPageElementImpl extends BaseLocatable implements ComponentPageElement
ImplementsRenderCommandand represents a component within an overall page. Much of a component page element's behavior is delegated to user code, via aComponentinstance. Once instantiated, a ComponentPageElement should be registered as a lifecycle listener. This could be done inside the constructors, but that tends to complicate unit tests, so its done byPageElementFactoryImpl. There's still a bit of refactoring in this class (and its many inner classes) that can improve overall efficiency. Modified for Tapestry 5.2 to adjust for the no-pooling approach (shared instances with externalized mutable state).
-
-
Constructor Summary
Constructors Constructor Description ComponentPageElementImpl(Page page, Instantiator instantiator, ComponentPageElementResources elementResources)Constructor for the root component of a page.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddBlock(java.lang.String blockId, Block block)Used during the contruction of a page to add a non-anonymous Block to the component.voidaddMixin(java.lang.String mixinId, Instantiator instantiator, java.lang.String... order)Adds a mixin.voidaddToBody(RenderCommand element)Used during the construction of the page.voidaddToTemplate(RenderCommand element)Used during the construction of a page.voidbindMixinParameter(java.lang.String mixinId, java.lang.String parameterName, Binding binding)voidbindParameter(java.lang.String parameterName, Binding binding)Used during construction of the page to identify the binding for a particular parameter.LinkcreateEventLink(java.lang.String eventType, java.lang.Object... context)Creates a component event request link as a callback for this component.LinkcreateFormEventLink(java.lang.String eventType, java.lang.Object... context)Creates a component event request link as a callback for this component.protected RenderPhaseEventcreateRenderEvent(RenderQueue queue)booleandispatchEvent(ComponentEvent event)Asks each mixin and component toComponent.dispatchComponentEvent(ComponentEvent), returning true if any handler was found.voidenqueueBeforeRenderBody(RenderQueue queue)Invoked when the component should render its body.BlockfindBlock(java.lang.String id)As withComponentResourcesCommon.getBlock(String), but returns null if the block is not found.BindinggetBinding(java.lang.String parameterName)Returns the binding for the given parameter name, or null.BlockgetBlock(java.lang.String id)Returns a block from the component's template, referenced by its id.BlockgetBody()Returns the body of this component as a (possibly empty) block.java.lang.StringgetCompleteId()Returns a string consisting of the logical name of the containing page, and thenested idof this component, separated by a colon.ComponentgetComponent()Returns the core component associated with this page element (as opposed to any mixins attached to the component).InternalComponentResourcesgetComponentResources()Returns the resources associated with the core component.ComponentPageElementgetContainerElement()Containing component (or null for the root component of a page).PagegetContainingPage()Returns the page which contains this component.java.lang.StringgetElementName(java.lang.String defaultElementName)Returns the name of element that represents the component in its template, or the provided default element name if the element was a component type (in the Tapestry namespace).ComponentPageElementgetEmbeddedElement(java.lang.String embeddedId)Retrieves a component page element by its id.java.util.Set<java.lang.String>getEmbeddedElementIds()Returns the ids of all embedded elements defined within the component.org.slf4j.LoggergetEventLogger()Returns a logger used to for logging event dispatch and event method invocation.java.lang.StringgetId()Returns the simple (or local) id of the component.java.util.Map<java.lang.String,Binding>getInformalParameterBindings()Constructs a map linking informal parameters to the corresponding bindings.java.util.LocalegetLocale()Returns the locale for the page containing this component.org.slf4j.LoggergetLogger()Returns the log instance associated with the component (which is based on the component or mixin's class name).ComponentgetMixinByClassName(java.lang.String mixinClassName)Returns the mixin instance for the fully qualfied mixin class name.ComponentResourcesgetMixinResources(java.lang.String mixinId)Returns theComponentResourcesfor a mixin attached to this component element.java.lang.StringgetNestedId()Return a string consisting the concatenated ids of all containing components, separated by periods.java.lang.StringgetPageName()Returns the logical name of the page containing this component.ComponentResourceSelectorgetResourceSelector()Returns the selector used when constructing the component and its containing page.booleanhasBody()Returns true if the element has a body and false otherwise.booleanisLoaded()Returns true if the component has finished loading.booleanisRendering()Returns true if the component is currently rendering, false otherwise.ComponentPageElementnewChild(java.lang.String id, java.lang.String nestedId, java.lang.String completeId, java.lang.String elementName, Instantiator instantiator, Location location)Creates a new child component of the invoked component.voidrender(MarkupWriter writer, RenderQueue queue)Pushes the SetupRender phase state onto the queue.java.lang.StringtoString()booleantriggerContextEvent(java.lang.String eventType, EventContext context, ComponentEventCallback callback)Triggers a component event.booleantriggerEvent(java.lang.String eventType, java.lang.Object[] contextValues, ComponentEventCallback callback)A convenience method for invokingComponentResourcesCommon.triggerContextEvent(String, EventContext , ComponentEventCallback).-
Methods inherited from class org.apache.tapestry5.ioc.BaseLocatable
getLocation
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.tapestry5.commons.Locatable
getLocation
-
-
-
-
Constructor Detail
-
ComponentPageElementImpl
public ComponentPageElementImpl(Page page, Instantiator instantiator, ComponentPageElementResources elementResources)
Constructor for the root component of a page.
-
-
Method Detail
-
newChild
public ComponentPageElement newChild(java.lang.String id, java.lang.String nestedId, java.lang.String completeId, java.lang.String elementName, Instantiator instantiator, Location location)
Description copied from interface:ComponentPageElementCreates a new child component of the invoked component. The new element will be added as an embedded element of its container.- Specified by:
newChildin interfaceComponentPageElement- Parameters:
id- simple id of the new componentelementName- name of the component's element in its container's templateinstantiator- used to create a component instance, and access the component's modellocation- location of the element within its container's template @return the new component
-
addMixin
public void addMixin(java.lang.String mixinId, Instantiator instantiator, java.lang.String... order)
Description copied from interface:ComponentPageElementAdds a mixin.- Specified by:
addMixinin interfaceComponentPageElement- Parameters:
mixinId- a unique id for the mixin, the last term of the mixin's class nameinstantiator- used to instantiate an instance of the mixinorder- Ordering strings used to determine the order of mixin execution.
-
bindMixinParameter
public void bindMixinParameter(java.lang.String mixinId, java.lang.String parameterName, Binding binding)
- Specified by:
bindMixinParameterin interfaceComponentPageElement- Parameters:
mixinId- id of previously added mixinparameterName- simple (unqualified) name of parameterbinding- binding for parameter
-
getBinding
public Binding getBinding(java.lang.String parameterName)
Description copied from interface:InternalComponentResourcesCommonReturns the binding for the given parameter name, or null.- Specified by:
getBindingin interfaceInternalComponentResourcesCommon- Parameters:
parameterName- name of component parameter- Returns:
- binding if bound, or null
-
bindParameter
public void bindParameter(java.lang.String parameterName, Binding binding)
Description copied from interface:InternalComponentResourcesCommonUsed during construction of the page to identify the binding for a particular parameter.- Specified by:
bindParameterin interfaceInternalComponentResourcesCommon
-
addToBody
public void addToBody(RenderCommand element)
Description copied from interface:BodyPageElementUsed during the construction of the page. Adds a page element as part of the body of the component. The body of a component is defined as the portion of the container's template directly enclosed by component's start and end elements.- Specified by:
addToBodyin interfaceBodyPageElement
-
addToTemplate
public void addToTemplate(RenderCommand element)
Description copied from interface:ComponentPageElementUsed during the construction of a page. Adds a page element as part of the template for this page element. A page element will eventually render by sequentially rendering these elements. A page elements template is really just the outermost portions of the component's template ... where a template contains elements that are all components, those components will receive portions of the template as their body.- Specified by:
addToTemplatein interfaceComponentPageElement
-
enqueueBeforeRenderBody
public void enqueueBeforeRenderBody(RenderQueue queue)
Description copied from interface:ComponentPageElementInvoked when the component should render its body.- Specified by:
enqueueBeforeRenderBodyin interfaceComponentPageElement
-
getCompleteId
public java.lang.String getCompleteId()
Description copied from interface:ComponentResourcesCommonReturns a string consisting of the logical name of the containing page, and thenested idof this component, separated by a colon. I.e., "MyPage:foo.bar.baz". For a page, returns just the page's name. This value is often used to obtain an equivalent component instance in a later request.- Specified by:
getCompleteIdin interfaceComponentResourcesCommon- See Also:
ComponentSource.getComponent(String)
-
getComponent
public Component getComponent()
Description copied from interface:ComponentPageElementReturns the core component associated with this page element (as opposed to any mixins attached to the component).- Specified by:
getComponentin interfaceComponentPageElement
-
getComponentResources
public InternalComponentResources getComponentResources()
Description copied from interface:ComponentPageElementReturns the resources associated with the core component.- Specified by:
getComponentResourcesin interfaceComponentPageElement
-
getContainerElement
public ComponentPageElement getContainerElement()
Description copied from interface:ComponentPageElementContaining component (or null for the root component of a page).- Specified by:
getContainerElementin interfaceComponentPageElement
-
getContainingPage
public Page getContainingPage()
Description copied from interface:ComponentPageElementReturns the page which contains this component.- Specified by:
getContainingPagein interfaceComponentPageElement
-
getEmbeddedElement
public ComponentPageElement getEmbeddedElement(java.lang.String embeddedId)
Description copied from interface:ComponentPageElementRetrieves a component page element by its id. The search is caseless.- Specified by:
getEmbeddedElementin interfaceComponentPageElement- Parameters:
embeddedId- used to locate the element- Returns:
- the page element
-
getEmbeddedElementIds
public java.util.Set<java.lang.String> getEmbeddedElementIds()
Description copied from interface:ComponentPageElementReturns the ids of all embedded elements defined within the component.- Specified by:
getEmbeddedElementIdsin interfaceComponentPageElement
-
getId
public java.lang.String getId()
Description copied from interface:ComponentResourcesCommonReturns the simple (or local) id of the component. The id will be unique within the component's immediate container. For a page's root component, the value null is returned.- Specified by:
getIdin interfaceComponentResourcesCommon
-
getLogger
public org.slf4j.Logger getLogger()
Description copied from interface:ComponentResourcesCommonReturns the log instance associated with the component (which is based on the component or mixin's class name).- Specified by:
getLoggerin interfaceComponentResourcesCommon- See Also:
ComponentModel.getLogger()
-
getMixinByClassName
public Component getMixinByClassName(java.lang.String mixinClassName)
Description copied from interface:InternalComponentResourcesCommonReturns the mixin instance for the fully qualfied mixin class name.- Specified by:
getMixinByClassNamein interfaceInternalComponentResourcesCommon- Parameters:
mixinClassName- fully qualified class name- Returns:
- IllegalArgumentException if no such mixin is associated with the core component
-
getMixinResources
public ComponentResources getMixinResources(java.lang.String mixinId)
Description copied from interface:ComponentPageElementReturns theComponentResourcesfor a mixin attached to this component element. Mixin ids are the simple names of the mixin class.- Specified by:
getMixinResourcesin interfaceComponentPageElement- Parameters:
mixinId- the mixin id (case insensitive)- Returns:
- the resources for the component
-
getNestedId
public java.lang.String getNestedId()
Description copied from interface:ComponentResourcesCommonReturn a string consisting the concatenated ids of all containing components, separated by periods. In addition, nested ids are always all lower case. I.e., "foo.bar.baz". Returns null for the root component of a page.- Specified by:
getNestedIdin interfaceComponentResourcesCommon
-
dispatchEvent
public boolean dispatchEvent(ComponentEvent event)
Description copied from interface:ComponentPageElementAsks each mixin and component toComponent.dispatchComponentEvent(ComponentEvent), returning true if any handler was found.- Specified by:
dispatchEventin interfaceComponentPageElement- Parameters:
event- to be handled- Returns:
- true if a handler was found
-
isLoaded
public boolean isLoaded()
Description copied from interface:InternalComponentResourcesCommonReturns true if the component has finished loading. Initially, this value will be false.- Specified by:
isLoadedin interfaceInternalComponentResourcesCommon- See Also:
PageLifecycleListener.containingPageDidLoad()
-
isRendering
public boolean isRendering()
Description copied from interface:ComponentResourcesCommonReturns true if the component is currently rendering, false otherwise. This is most often used to determine if parameter values should be cached.- Specified by:
isRenderingin interfaceComponentResourcesCommon
-
render
public final void render(MarkupWriter writer, RenderQueue queue)
Pushes the SetupRender phase state onto the queue.- Specified by:
renderin interfaceRenderCommand
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
triggerEvent
public boolean triggerEvent(java.lang.String eventType, java.lang.Object[] contextValues, ComponentEventCallback callback)
Description copied from interface:ComponentResourcesCommonA convenience method for invokingComponentResourcesCommon.triggerContextEvent(String, EventContext , ComponentEventCallback). Wraps the context values into anEventContext.- Specified by:
triggerEventin interfaceComponentResourcesCommon- Parameters:
eventType- event type (as determined from the request, or otherwise by design)contextValues- Values that may be provided to the event handler method as method parameters, or null if no context values are availablecallback- the handler to be informed of the result, or null if the event is a notification that does not support return values from event handler methods (the value true is allowed even if the handler is null).- Returns:
- true if any event handler was invoked (even if no event handler method returns a non-null value)
- See Also:
OnEventWorker,OnEvent
-
triggerContextEvent
public boolean triggerContextEvent(java.lang.String eventType, EventContext context, ComponentEventCallback callback)
Description copied from interface:ComponentResourcesCommonTriggers a component event. A search for an event handling method will occur, first in the component, then its container, and so on. When a matching event handler method is located, it is invoked. If the method returns a value, the value is passed to the callback (if callback is null, then it is an error for a method to return a non-null value). Resolution of event type to event handler methods is case insensitive.- Specified by:
triggerContextEventin interfaceComponentResourcesCommon- Parameters:
eventType- event type (as determined from the request, or otherwise by design)context- the context (as extracted from the request, or provided by the triggering component); these values may be provided to event handler methods via their parameters (may not be null)callback- the handler to be informed of the result, or null if the event is a notification that does not support return values from event handler methods (the value true is allowed even if the handler is null).- Returns:
- true if any event handler was invoked (even if no event handler method returns a non-null value)
- See Also:
OnEventWorker,OnEvent
-
getLocale
public java.util.Locale getLocale()
Description copied from interface:ComponentResourcesCommonReturns the locale for the page containing this component.- Specified by:
getLocalein interfaceComponentResourcesCommon- See Also:
ComponentResourcesCommon.getResourceSelector()
-
getElementName
public java.lang.String getElementName(java.lang.String defaultElementName)
Description copied from interface:ComponentResourcesCommonReturns the name of element that represents the component in its template, or the provided default element name if the element was a component type (in the Tapestry namespace).- Specified by:
getElementNamein interfaceComponentResourcesCommon- Parameters:
defaultElementName- element name to return if the element name is not known (may be null)- Returns:
- the element name
-
getBlock
public Block getBlock(java.lang.String id)
Description copied from interface:ComponentResourcesCommonReturns a block from the component's template, referenced by its id.- Specified by:
getBlockin interfaceComponentResourcesCommon- Parameters:
id- the id of the block (case insensitive)- Returns:
- the identified Block
- See Also:
ComponentResourcesCommon.findBlock(String)
-
findBlock
public Block findBlock(java.lang.String id)
Description copied from interface:ComponentResourcesCommonAs withComponentResourcesCommon.getBlock(String), but returns null if the block is not found.- Specified by:
findBlockin interfaceComponentResourcesCommon- Parameters:
id- the id of the block (case insensitive)- Returns:
- the block, or null
-
addBlock
public void addBlock(java.lang.String blockId, Block block)
Description copied from interface:ComponentPageElementUsed during the contruction of a page to add a non-anonymous Block to the component.- Specified by:
addBlockin interfaceComponentPageElement- See Also:
ComponentResourcesCommon.getBlock(String)
-
getPageName
public java.lang.String getPageName()
Description copied from interface:ComponentResourcesCommonReturns the logical name of the page containing this component. This is the short name (it often appears in URLs)- Specified by:
getPageNamein interfaceComponentResourcesCommon- Returns:
- the logical name of the page which contains this component
-
hasBody
public boolean hasBody()
Description copied from interface:ComponentResourcesCommonReturns true if the element has a body and false otherwise. Only components may have a body; pages and mixins will return false.- Specified by:
hasBodyin interfaceComponentResourcesCommon
-
getBody
public Block getBody()
Description copied from interface:ComponentResourcesCommonReturns the body of this component as a (possibly empty) block. When invoked on a mixin, returns the containing component's body.- Specified by:
getBodyin interfaceComponentResourcesCommon
-
getInformalParameterBindings
public java.util.Map<java.lang.String,Binding> getInformalParameterBindings()
Description copied from interface:InternalComponentResourcesCommonConstructs a map linking informal parameters to the corresponding bindings.- Specified by:
getInformalParameterBindingsin interfaceInternalComponentResourcesCommon- Returns:
- map, possible empty
-
getEventLogger
public org.slf4j.Logger getEventLogger()
Description copied from interface:ComponentPageElementReturns a logger used to for logging event dispatch and event method invocation.- Specified by:
getEventLoggerin interfaceComponentPageElement
-
createEventLink
public Link createEventLink(java.lang.String eventType, java.lang.Object... context)
Description copied from interface:ComponentResourcesCommonCreates a component event request link as a callback for this component. The event type and context (as well as the page name and nested component id) will be encoded into a URL. A request for the URL will ComponentResourcesCommon.triggerEvent(String, Object[], org.apache.tapestry5.ComponentEventCallback) trigger} the named event on the component.- Specified by:
createEventLinkin interfaceComponentResourcesCommon- Parameters:
eventType- the type of event to be triggered. Event types should be Java identifiers (contain only letters, numbers and the underscore).context- additional objects to be encoded into the path portion of the link; each is converted to a string and URI encoded- Returns:
- link object for the callback
-
createFormEventLink
public Link createFormEventLink(java.lang.String eventType, java.lang.Object... context)
Description copied from interface:ComponentResourcesCommonCreates a component event request link as a callback for this component. The event type and context (as well as the page name and nested component id) will be encoded into a URL. A request for the URL will ComponentResourcesCommon.triggerEvent(String, Object[], org.apache.tapestry5.ComponentEventCallback) trigger} the named event on the component. This is only used for form submission events, as extra data may be encoded in the form as hidden fields.- Specified by:
createFormEventLinkin interfaceComponentResourcesCommon- Parameters:
eventType- the type of event to be triggered. Event types should be Java identifiers (contain only letters, numbers and the underscore).context- additional objects to be encoded into the path portion of the link; each is converted to a string and URI encoded- Returns:
- link object for the callback
-
createRenderEvent
protected RenderPhaseEvent createRenderEvent(RenderQueue queue)
-
getResourceSelector
public ComponentResourceSelector getResourceSelector()
Description copied from interface:ComponentResourcesCommonReturns the selector used when constructing the component and its containing page.- Specified by:
getResourceSelectorin interfaceComponentResourcesCommon
-
-