01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.corelib.components;
16:
17: import java.util.List;
18:
19: import org.apache.tapestry.ClientElement;
20: import org.apache.tapestry.ComponentResources;
21: import org.apache.tapestry.Link;
22: import org.apache.tapestry.MarkupWriter;
23: import org.apache.tapestry.PageRenderSupport;
24: import org.apache.tapestry.annotations.Environmental;
25: import org.apache.tapestry.annotations.Inject;
26: import org.apache.tapestry.annotations.Parameter;
27: import org.apache.tapestry.annotations.SupportsInformalParameters;
28:
29: /**
30: * Generates a render request link to some other page in the application. If an activation context
31: * is supplied (as the context parameter), then the context values will be encoded into the URL. If
32: * no context is supplied, then the target page itself will supply the context via a passivate
33: * event.
34: * <p>
35: * Pages are not required to have an activation context. When a page does have an activation
36: * context, the value typically represents the identity of some object displayed or otherwise
37: * manipulated by the page.
38: */
39: @SupportsInformalParameters
40: public class PageLink implements ClientElement {
41: /** The logical name of the page to link to. */
42: @Parameter(required=true,defaultPrefix="literal")
43: private String _page;
44:
45: @Inject
46: private ComponentResources _resources;
47:
48: @Environmental
49: private PageRenderSupport _support;
50:
51: private String _clientId;
52:
53: /**
54: * If provided, this is the activation context for the target page (the information will be
55: * encoded into the URL). If not provided, then the target page will provide its own activation
56: * context.
57: */
58: @Parameter
59: private List _context;
60:
61: private final Object[] _emptyContext = new Object[0];
62:
63: void beginRender(MarkupWriter writer) {
64: _clientId = _support.allocateClientId(_resources.getId());
65:
66: Object[] activationContext = _context != null ? _context
67: .toArray() : _emptyContext;
68:
69: Link link = _resources.createPageLink(_page, _resources
70: .isBound("context"), activationContext);
71:
72: writer.element("a", "href", link, "id", _clientId);
73:
74: _resources.renderInformalParameters(writer);
75: }
76:
77: void afterRender(MarkupWriter writer) {
78: writer.end(); // <a>
79: }
80:
81: public String getClientId() {
82: return _clientId;
83: }
84:
85: }
|