|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ButtonLinkRenderer.java | 83.3% | 90.5% | 100% | 90% |
|
1 | // Copyright 2004, 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // 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.link; | |
16 | ||
17 | import org.apache.hivemind.ApplicationRuntimeException; | |
18 | import org.apache.tapestry.IMarkupWriter; | |
19 | import org.apache.tapestry.IRequestCycle; | |
20 | import org.apache.tapestry.Tapestry; | |
21 | import org.apache.tapestry.components.ILinkComponent; | |
22 | import org.apache.tapestry.engine.ILink; | |
23 | ||
24 | /** | |
25 | * An {@link ILinkRenderer} implementation that generates an HTML button. | |
26 | * This is particularly useful for implementing cancel buttons. | |
27 | * | |
28 | * @author Paul Ferraro | |
29 | * @since 4.0 | |
30 | */ | |
31 | public class ButtonLinkRenderer implements ILinkRenderer | |
32 | { | |
33 | public static final ILinkRenderer SHARED_INSTANCE = new ButtonLinkRenderer(); | |
34 | ||
35 | /** | |
36 | * @see org.apache.tapestry.link.ILinkRenderer#renderLink(org.apache.tapestry.IMarkupWriter, | |
37 | * org.apache.tapestry.IRequestCycle, org.apache.tapestry.components.ILinkComponent) | |
38 | */ | |
39 | 3 | public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent component) |
40 | { | |
41 | 3 | if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null) |
42 | { | |
43 | 0 | String message = Tapestry.getMessage("AbstractLinkComponent.no-nesting"); |
44 | 0 | throw new ApplicationRuntimeException(message, component, null, null); |
45 | } | |
46 | ||
47 | 3 | cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, component); |
48 | ||
49 | 3 | ILink link = component.getLink(cycle); |
50 | ||
51 | 3 | writer.begin("button"); |
52 | 3 | writer.attribute("type", "button"); |
53 | ||
54 | 3 | if (component.isDisabled()) |
55 | { | |
56 | 1 | writer.attribute("disabled", "disabled"); |
57 | } | |
58 | ||
59 | 3 | String url = link.getURL(component.getAnchor(), true); |
60 | 3 | String target = component.getTarget(); |
61 | 3 | String onclick = (target == null) ? getScript(url) : getScript(url, target); |
62 | ||
63 | 3 | writer.attribute("onclick", onclick); |
64 | ||
65 | 3 | component.renderAdditionalAttributes(writer, cycle); |
66 | ||
67 | 3 | IMarkupWriter wrappedWriter = writer.getNestedWriter(); |
68 | ||
69 | 3 | component.renderBody(wrappedWriter, cycle); |
70 | ||
71 | 3 | wrappedWriter.close(); |
72 | ||
73 | 3 | writer.end(); |
74 | ||
75 | 3 | cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME); |
76 | } | |
77 | ||
78 | /** | |
79 | * Generates the onclick event handler that opens the specified url in the current window. | |
80 | * @param url the url generated by this link | |
81 | * @return a JavaScript onclick event handler | |
82 | */ | |
83 | 2 | protected String getScript(String url) |
84 | { | |
85 | 2 | return "window.location='" + url + "'"; |
86 | } | |
87 | ||
88 | /** | |
89 | * Generates the onclick event handler that opens the specified url in the specified window or frame. | |
90 | * @param url the url generated by this link | |
91 | * @param target the name of the target window or frame | |
92 | * @return a JavaScript onclick event handler | |
93 | */ | |
94 | 1 | protected String getScript(String url, String target) |
95 | { | |
96 | 1 | return "window.open('" + url + "','" + target + "')"; |
97 | } | |
98 | } |
|