001// Copyright 2010 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.internal.services;
016
017import org.apache.tapestry5.EventConstants;
018import org.apache.tapestry5.http.Link;
019import org.apache.tapestry5.model.ComponentModel;
020import org.apache.tapestry5.runtime.Component;
021import org.apache.tapestry5.services.ComponentClassResolver;
022import org.apache.tapestry5.services.ComponentEventRequestParameters;
023import org.apache.tapestry5.services.ComponentSource;
024import org.apache.tapestry5.services.LinkCreationListener2;
025import org.apache.tapestry5.services.PageRenderRequestParameters;
026
027/**
028 * A default {@link LinkCreationListener2} that triggers the {@link EventConstants#DECORATE_COMPONENT_EVENT_LINK} and
029 * {@link EventConstants#DECORATE_PAGE_RENDER_LINK} events as links are generated.
030 * 
031 * @since 5.2.0
032 */
033public class LinkDecorationListener implements LinkCreationListener2
034{
035    private final ComponentClassResolver resolver;
036
037    private final ComponentSource componentSource;
038
039    private final ComponentModelSource modelSource;
040
041    public LinkDecorationListener(ComponentClassResolver resolver, ComponentSource componentSource,
042            ComponentModelSource modelSource)
043    {
044        this.resolver = resolver;
045        this.componentSource = componentSource;
046        this.modelSource = modelSource;
047    }
048
049    public void createdComponentEventLink(Link link, ComponentEventRequestParameters parameters)
050    {
051        trigger(parameters.getActivePageName(), EventConstants.DECORATE_COMPONENT_EVENT_LINK, link, parameters);
052    }
053
054    public void createdPageRenderLink(Link link, PageRenderRequestParameters parameters)
055    {
056        trigger(parameters.getLogicalPageName(), EventConstants.DECORATE_PAGE_RENDER_LINK, link, parameters);
057    }
058
059    private void trigger(String pageName, String eventType, Link link, Object parameters)
060    {
061        String pageClassName = resolver.resolvePageNameToClassName(pageName);
062
063        ComponentModel model = modelSource.getModel(pageClassName);
064
065        if (model.handlesEvent(eventType))
066        {
067            Component page = componentSource.getPage(pageName);
068
069            page.getComponentResources().triggerEvent(eventType, new Object[]
070            { link, parameters }, null);
071        }
072    }
073}