KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > link > DefaultLinkRenderer


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.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  * Default implementation of {@link org.apache.tapestry.link.ILinkRenderer}, which does nothing
27  * special. Can be used as a base class to provide additional handling.
28  *
29  * @author Howard Lewis Ship, David Solis
30  * @since 3.0
31  */

32
33 public class DefaultLinkRenderer implements ILinkRenderer
34 {
35     /**
36      * A shared instance used as a default for any link that doesn't explicitly override.
37      */

38
39     public static final ILinkRenderer SHARED_INSTANCE = new DefaultLinkRenderer();
40
41     public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent linkComponent)
42     {
43         IMarkupWriter wrappedWriter = null;
44
45         if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null)
46             throw new ApplicationRuntimeException(Tapestry
47                     .getMessage("AbstractLinkComponent.no-nesting"), linkComponent, null, null);
48
49         cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, linkComponent);
50
51         boolean hasBody = getHasBody();
52
53         boolean disabled = linkComponent.isDisabled();
54
55         if (!disabled)
56         {
57             ILink l = linkComponent.getLink(cycle);
58
59             if (hasBody)
60                 writer.begin(getElement());
61             else
62                 writer.beginEmpty(getElement());
63
64             writer.attribute(getUrlAttribute(), constructURL(l, linkComponent.getAnchor(), cycle));
65
66             String JavaDoc target = linkComponent.getTarget();
67
68             if (HiveMind.isNonBlank(target))
69                 writer.attribute(getTargetAttribute(), target);
70
71             beforeBodyRender(writer, cycle, linkComponent);
72
73             // Allow the wrapped components a chance to render.
74
// Along the way, they may interact with this component
75
// and cause the name variable to get set.
76

77             wrappedWriter = writer.getNestedWriter();
78         }
79         else
80             wrappedWriter = writer;
81
82         if (hasBody)
83             linkComponent.renderBody(wrappedWriter, cycle);
84
85         if (!disabled)
86         {
87             afterBodyRender(writer, cycle, linkComponent);
88
89             linkComponent.renderAdditionalAttributes(writer, cycle);
90
91             if (hasBody)
92             {
93                 wrappedWriter.close();
94
95                 // Close the <element> tag
96

97                 writer.end();
98             }
99             else
100                 writer.closeTag();
101         }
102
103         cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
104     }
105
106     /**
107      * Converts the EngineServiceLink into a URI or URL. This implementation simply invokes
108      * {@link ILink#getURL(String, boolean)}.
109      */

110
111     protected String JavaDoc constructURL(ILink link, String JavaDoc anchor, IRequestCycle cycle)
112     {
113         return link.getURL(anchor, true);
114     }
115
116     /**
117      * Invoked after the href attribute has been written but before the body of the link is rendered
118      * (but only if the link is not disabled).
119      * <p>
120      * This implementation does nothing.
121      */

122
123     protected void beforeBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link)
124     {
125     }
126
127     /**
128      * Invoked after the body of the link is rendered, but before
129      * {@link ILinkComponent#renderAdditionalAttributes(IMarkupWriter, IRequestCycle)}is invoked
130      * (but only if the link is not disabled).
131      * <p>
132      * This implementation does nothing.
133      */

134
135     protected void afterBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link)
136     {
137     }
138
139     /** @since 3.0 * */
140
141     protected String JavaDoc getElement()
142     {
143         return "a";
144     }
145
146     protected String JavaDoc getUrlAttribute()
147     {
148         return "href";
149     }
150
151     protected String JavaDoc getTargetAttribute()
152     {
153         return "target";
154     }
155
156     protected boolean getHasBody()
157     {
158         return true;
159     }
160 }
Popular Tags