01: // Copyright 2006, 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 static org.apache.tapestry.TapestryConstants.ACTION_EVENT;
18:
19: import java.util.List;
20:
21: import org.apache.tapestry.ClientElement;
22: import org.apache.tapestry.ComponentResources;
23: import org.apache.tapestry.Link;
24: import org.apache.tapestry.MarkupWriter;
25: import org.apache.tapestry.PageRenderSupport;
26: import org.apache.tapestry.annotations.Environmental;
27: import org.apache.tapestry.annotations.Inject;
28: import org.apache.tapestry.annotations.Parameter;
29: import org.apache.tapestry.annotations.SupportsInformalParameters;
30:
31: /**
32: * Component that triggers an action on the server with a subsequent full page refresh.
33: */
34: @SupportsInformalParameters
35: public class ActionLink implements ClientElement {
36: /**
37: * The context for the link (optional parameter). This list of values will be converted into
38: * strings and included in the URI. The strings will be coerced back to whatever their values
39: * are and made available to event handler methods.
40: */
41: @Parameter
42: private List<?> _context;
43:
44: @Inject
45: private ComponentResources _resources;
46:
47: @Environmental
48: private PageRenderSupport _support;
49:
50: /**
51: * If true, then then no link element is rendered (and no informal parameters as well). The body
52: * is, however, still rendered.
53: */
54: @Parameter("false")
55: private boolean _disabled;
56:
57: private String _clientId;
58:
59: void beginRender(MarkupWriter writer) {
60: if (_disabled)
61: return;
62:
63: _clientId = _support.allocateClientId(_resources.getId());
64:
65: Object[] contextArray = _context == null ? new Object[0]
66: : _context.toArray();
67:
68: Link link = _resources.createActionLink(ACTION_EVENT, false,
69: contextArray);
70:
71: writer.element("a", "href", link, "id", _clientId);
72:
73: _resources.renderInformalParameters(writer);
74: }
75:
76: void afterRender(MarkupWriter writer) {
77: if (_disabled)
78: return;
79:
80: writer.end(); // <a>
81: }
82:
83: public String getClientId() {
84: return _clientId;
85: }
86: }
|