1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.link; |
16 |
| |
17 |
| import org.apache.hivemind.ApplicationRuntimeException; |
18 |
| import org.apache.hivemind.HiveMind; |
19 |
| import org.apache.tapestry.IMarkupWriter; |
20 |
| import org.apache.tapestry.IRequestCycle; |
21 |
| import org.apache.tapestry.Tapestry; |
22 |
| import org.apache.tapestry.components.ILinkComponent; |
23 |
| import org.apache.tapestry.engine.ILink; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| public class DefaultLinkRenderer implements ILinkRenderer |
34 |
| { |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| public static final ILinkRenderer SHARED_INSTANCE = new DefaultLinkRenderer(); |
40 |
| |
41 |
158
| public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent linkComponent)
|
42 |
| { |
43 |
158
| IMarkupWriter wrappedWriter = null;
|
44 |
| |
45 |
158
| if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null)
|
46 |
2
| throw new ApplicationRuntimeException(LinkMessages.noNesting(), linkComponent, null,
|
47 |
| null); |
48 |
| |
49 |
156
| cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, linkComponent);
|
50 |
| |
51 |
156
| boolean hasBody = getHasBody();
|
52 |
| |
53 |
156
| boolean disabled = linkComponent.isDisabled() || cycle.isRewinding();
|
54 |
| |
55 |
156
| if (!disabled)
|
56 |
| { |
57 |
142
| if (hasBody)
|
58 |
134
| writer.begin(getElement());
|
59 |
| else |
60 |
8
| writer.beginEmpty(getElement());
|
61 |
| |
62 |
142
| writer.attribute(getUrlAttribute(), constructURL(linkComponent, cycle));
|
63 |
| |
64 |
142
| String target = linkComponent.getTarget();
|
65 |
| |
66 |
142
| if (HiveMind.isNonBlank(target))
|
67 |
6
| writer.attribute(getTargetAttribute(), target);
|
68 |
| |
69 |
142
| beforeBodyRender(writer, cycle, linkComponent);
|
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
142
| wrappedWriter = writer.getNestedWriter();
|
76 |
| } |
77 |
| else |
78 |
14
| wrappedWriter = writer;
|
79 |
| |
80 |
156
| if (hasBody)
|
81 |
144
| linkComponent.renderBody(wrappedWriter, cycle);
|
82 |
| |
83 |
156
| if (!disabled)
|
84 |
| { |
85 |
142
| afterBodyRender(writer, cycle, linkComponent);
|
86 |
| |
87 |
142
| linkComponent.renderAdditionalAttributes(writer, cycle);
|
88 |
| |
89 |
142
| if (hasBody)
|
90 |
| { |
91 |
134
| wrappedWriter.close();
|
92 |
| |
93 |
| |
94 |
| |
95 |
134
| writer.end();
|
96 |
| } |
97 |
| else |
98 |
8
| writer.closeTag();
|
99 |
| } |
100 |
| |
101 |
156
| cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
|
102 |
| } |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
142
| protected String constructURL(ILinkComponent component, IRequestCycle cycle)
|
111 |
| { |
112 |
142
| ILink link = component.getLink(cycle);
|
113 |
| |
114 |
142
| String scheme = component.getScheme();
|
115 |
142
| Integer port = component.getPort();
|
116 |
142
| int portI = (port == null) ? 0 : port.intValue();
|
117 |
142
| String anchor = component.getAnchor();
|
118 |
| |
119 |
142
| return link.getURL(scheme, null, portI, anchor, true);
|
120 |
| } |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
138
| protected void beforeBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link)
|
130 |
| { |
131 |
| } |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
138
| protected void afterBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link)
|
142 |
| { |
143 |
| } |
144 |
| |
145 |
| |
146 |
| |
147 |
128
| protected String getElement()
|
148 |
| { |
149 |
128
| return "a";
|
150 |
| } |
151 |
| |
152 |
134
| protected String getUrlAttribute()
|
153 |
| { |
154 |
134
| return "href";
|
155 |
| } |
156 |
| |
157 |
2
| protected String getTargetAttribute()
|
158 |
| { |
159 |
2
| return "target";
|
160 |
| } |
161 |
| |
162 |
140
| protected boolean getHasBody()
|
163 |
| { |
164 |
140
| return true;
|
165 |
| } |
166 |
| } |