001    // Copyright 2009 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.internal.services;
016    
017    import org.apache.tapestry5.services.PageRenderLinkSource;
018    import org.apache.tapestry5.services.ComponentClassResolver;
019    import org.apache.tapestry5.Link;
020    import org.apache.tapestry5.EventContext;
021    
022    public class PageRenderLinkSourceImpl implements PageRenderLinkSource
023    {
024        private final ComponentClassResolver resolver;
025    
026        private final LinkSource linkSource;
027    
028        public PageRenderLinkSourceImpl(LinkSource linkSource, ComponentClassResolver resolver)
029        {
030            this.linkSource = linkSource;
031            this.resolver = resolver;
032        }
033    
034        private String toPageName(Class pageClass)
035        {
036            return resolver.resolvePageClassNameToPageName(pageClass.getName());
037        }
038    
039        public Link createPageRenderLink(Class pageClass)
040        {
041            return createPageRenderLink(toPageName(pageClass));
042        }
043    
044        public Link createPageRenderLink(String pageName)
045        {
046            return linkSource.createPageRenderLink(pageName, false);
047        }
048    
049        public Link createPageRenderLinkWithContext(Class pageClass, Object... context)
050        {
051            return createPageRenderLinkWithContext(toPageName(pageClass), context);
052        }
053    
054        public Link createPageRenderLinkWithContext(Class pageClass, EventContext eventContext)
055        {
056            return createPageRenderLinkWithContext(toPageName(pageClass), eventContext);
057        }
058    
059        public Link createPageRenderLinkWithContext(String pageName, Object... context)
060        {
061            return linkSource.createPageRenderLink(pageName, true, context);
062        }
063    
064        public Link createPageRenderLinkWithContext(String pageName, EventContext eventContext)
065        {
066            int count = eventContext.getCount();
067    
068            Object[] pageActivationContext = new Object[count];
069    
070            for(int i = 0; i < count; i++)
071                pageActivationContext[i] = eventContext.get(Object.class, i);
072    
073            return linkSource.createPageRenderLink(pageName, true, pageActivationContext);
074        }
075    
076    }