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 |
78
| public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent linkComponent)
|
42 |
| { |
43 |
78
| IMarkupWriter wrappedWriter = null;
|
44 |
| |
45 |
78
| if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null)
|
46 |
1
| throw new ApplicationRuntimeException(LinkMessages.noNesting(), linkComponent, null,
|
47 |
| null); |
48 |
| |
49 |
77
| cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, linkComponent);
|
50 |
| |
51 |
77
| boolean hasBody = getHasBody();
|
52 |
| |
53 |
77
| boolean disabled = linkComponent.isDisabled();
|
54 |
| |
55 |
77
| if (!disabled)
|
56 |
| { |
57 |
74
| if (hasBody)
|
58 |
69
| writer.begin(getElement());
|
59 |
| else |
60 |
5
| writer.beginEmpty(getElement());
|
61 |
| |
62 |
74
| writer.attribute(getUrlAttribute(), constructURL(linkComponent, cycle));
|
63 |
| |
64 |
74
| String target = linkComponent.getTarget();
|
65 |
| |
66 |
74
| if (HiveMind.isNonBlank(target))
|
67 |
3
| writer.attribute(getTargetAttribute(), target);
|
68 |
| |
69 |
74
| beforeBodyRender(writer, cycle, linkComponent);
|
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
74
| wrappedWriter = writer.getNestedWriter();
|
76 |
| } |
77 |
| else |
78 |
3
| wrappedWriter = writer;
|
79 |
| |
80 |
77
| if (hasBody)
|
81 |
71
| linkComponent.renderBody(wrappedWriter, cycle);
|
82 |
| |
83 |
77
| if (!disabled)
|
84 |
| { |
85 |
74
| afterBodyRender(writer, cycle, linkComponent);
|
86 |
| |
87 |
74
| linkComponent.renderAdditionalAttributes(writer, cycle);
|
88 |
| |
89 |
74
| if (hasBody)
|
90 |
| { |
91 |
69
| wrappedWriter.close();
|
92 |
| |
93 |
| |
94 |
| |
95 |
69
| writer.end();
|
96 |
| } |
97 |
| else |
98 |
5
| writer.closeTag();
|
99 |
| } |
100 |
| |
101 |
77
| cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
|
102 |
| } |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
74
| protected String constructURL(ILinkComponent component, IRequestCycle cycle)
|
111 |
| { |
112 |
74
| ILink link = component.getLink(cycle);
|
113 |
| |
114 |
74
| String scheme = component.getScheme();
|
115 |
74
| String anchor = component.getAnchor();
|
116 |
| |
117 |
74
| return link.getURL(scheme, null, 0, anchor, true);
|
118 |
| } |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
72
| protected void beforeBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link)
|
128 |
| { |
129 |
| } |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
72
| protected void afterBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link)
|
140 |
| { |
141 |
| } |
142 |
| |
143 |
| |
144 |
| |
145 |
64
| protected String getElement()
|
146 |
| { |
147 |
64
| return "a";
|
148 |
| } |
149 |
| |
150 |
68
| protected String getUrlAttribute()
|
151 |
| { |
152 |
68
| return "href";
|
153 |
| } |
154 |
| |
155 |
1
| protected String getTargetAttribute()
|
156 |
| { |
157 |
1
| return "target";
|
158 |
| } |
159 |
| |
160 |
69
| protected boolean getHasBody()
|
161 |
| { |
162 |
69
| return true;
|
163 |
| } |
164 |
| } |