001 // Copyright 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.corelib.components;
016
017 import org.apache.tapestry5.BindingConstants;
018 import org.apache.tapestry5.ComponentResources;
019 import org.apache.tapestry5.Link;
020 import org.apache.tapestry5.MarkupWriter;
021 import org.apache.tapestry5.annotations.Parameter;
022 import org.apache.tapestry5.corelib.base.AbstractLink;
023 import org.apache.tapestry5.ioc.annotations.Inject;
024
025 /**
026 * Generates a render request link to some other page in the application. If an activation context is supplied (as the
027 * context parameter), then the context values will be encoded into the URL. If no context is supplied, then the target
028 * page itself will supply the context via a passivate event.
029 * <p/>
030 * Pages are not required to have an activation context. When a page does have an activation context, the value
031 * typically represents the identity of some object displayed or otherwise manipulated by the page.
032 */
033 public class PageLink extends AbstractLink
034 {
035 /**
036 * The logical name of the page to link to.
037 */
038 @Parameter(required = true, allowNull = false, defaultPrefix = BindingConstants.LITERAL)
039 private String page;
040
041 @Inject
042 private ComponentResources resources;
043
044 /**
045 * If provided, this is the activation context for the target page (the information will be encoded into the URL).
046 * If not provided, then the target page will provide its own activation context.
047 */
048 @Parameter
049 private Object[] context;
050
051 void beginRender(MarkupWriter writer)
052 {
053 if (isDisabled()) return;
054
055 Link link = resources.createPageLink(page, resources.isBound("context"), context);
056
057 writeLink(writer, link);
058 }
059
060 void afterRender(MarkupWriter writer)
061 {
062 if (isDisabled()) return;
063
064 writer.end(); // <a>
065 }
066 }