1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.contrib.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 | |
import org.apache.tapestry.link.ILinkRenderer; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 0 | public class ButtonLinkRenderer implements ILinkRenderer |
33 | |
{ |
34 | 0 | public static final ILinkRenderer SHARED_INSTANCE = new ButtonLinkRenderer(); |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent component) |
41 | |
{ |
42 | 0 | if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null) |
43 | |
{ |
44 | 0 | String message = Tapestry.getMessage("AbstractLinkComponent.no-nesting"); |
45 | 0 | throw new ApplicationRuntimeException(message, component, null, null); |
46 | |
} |
47 | |
|
48 | 0 | cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, component); |
49 | |
|
50 | 0 | ILink link = component.getLink(cycle); |
51 | |
|
52 | 0 | writer.begin("button"); |
53 | 0 | writer.attribute("type", "button"); |
54 | |
|
55 | 0 | if (component.isDisabled()) |
56 | |
{ |
57 | 0 | writer.attribute("disabled", "disabled"); |
58 | |
} |
59 | |
|
60 | 0 | if (!cycle.isRewinding()) { |
61 | 0 | String url = link.getURL(component.getAnchor(), true); |
62 | 0 | String target = component.getTarget(); |
63 | 0 | String onclick = (target == null) ? getScript(url) : getScript(url, target); |
64 | |
|
65 | 0 | writer.attribute("onclick", onclick); |
66 | |
} |
67 | |
|
68 | 0 | component.renderAdditionalAttributes(writer, cycle); |
69 | |
|
70 | 0 | IMarkupWriter wrappedWriter = writer.getNestedWriter(); |
71 | |
|
72 | 0 | component.renderBody(wrappedWriter, cycle); |
73 | |
|
74 | 0 | wrappedWriter.close(); |
75 | |
|
76 | 0 | writer.end(); |
77 | |
|
78 | 0 | cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME); |
79 | 0 | } |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
protected String getScript(String url) |
87 | |
{ |
88 | 0 | return "window.location='" + url + "'"; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
protected String getScript(String url, String target) |
98 | |
{ |
99 | 0 | return "window.open('" + url + "','" + target + "')"; |
100 | |
} |
101 | |
} |