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.corelib.components;
014
015import org.apache.tapestry5.BindingConstants;
016import org.apache.tapestry5.MarkupWriter;
017import org.apache.tapestry5.annotations.Parameter;
018import org.apache.tapestry5.commons.util.CommonsUtils;
019import org.apache.tapestry5.corelib.base.AbstractLink;
020import org.apache.tapestry5.http.Link;
021import org.apache.tapestry5.ioc.annotations.Inject;
022import org.apache.tapestry5.services.PageRenderLinkSource;
023
024/**
025 * Generates a render request link to some other page in the application. If an activation context is supplied (as the
026 * context parameter), then the context values will be encoded into the URL. If no context is supplied, then the target
027 * page itself will supply the context via a passivate event.
028 *
029 * Pages are not required to have an activation context. When a page does have an activation context, the value
030 * typically represents the identity of some object displayed or otherwise manipulated by the page.
031 *
032 * @tapestrydoc
033 */
034public class PageLink extends AbstractLink
035{
036    /**
037     * The page to link to. If a <code>String</code>, as usual, it should be the page logical name.
038     * If it's a <code>Class</code> instance, it's treated as the target page. 
039     * If it's not a <code>String</code> nor an <code>Class</code>, the target page will be
040     * the result of calling <code>page.getClass()</code>.
041     * Notice you'll need to use the <code>prop</code> binding when passing a value which
042     * isn't a <code>String</code>. 
043     */
044    @Parameter(required = true, allowNull = false, defaultPrefix = BindingConstants.LITERAL)
045    private Object page;
046
047    /**
048     * If provided, this is the activation context for the target page (the information will be encoded into the URL).
049     * If not provided, then the target page will provide its own activation context.
050     */
051    @Parameter
052    private Object[] context;
053
054    @Inject
055    private PageRenderLinkSource linkSource;
056
057    void beginRender(MarkupWriter writer)
058    {
059        if (isDisabled()) return;
060
061        Link link;
062        if (page instanceof String) {
063            final String pageName = (String) page; 
064            link = resources.isBound("context")
065                ? linkSource.createPageRenderLinkWithContext(pageName, context == null ? CommonsUtils.EMPTY_STRING_ARRAY : context)
066                : linkSource.createPageRenderLink(pageName);
067        }
068        else {
069            // If page is a Class, use it directly. If not, use its class (type)
070            Class<?> clasz = page instanceof Class<?> ? (Class<?>) page : page.getClass();
071            link = resources.isBound("context")
072                    ? linkSource.createPageRenderLinkWithContext(clasz, context == null ? CommonsUtils.EMPTY_STRING_ARRAY : context)
073                    : linkSource.createPageRenderLink(clasz);
074        }
075
076        writeLink(writer, link);
077    }
078
079    void afterRender(MarkupWriter writer)
080    {
081        if (isDisabled()) return;
082
083        writer.end(); // <a>
084    }
085}