KickJava   Java API By Example, From Geeks To Geeks.

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


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     public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent component)
40     {
41         if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null)
42         {
43             String JavaDoc message = Tapestry.getMessage("AbstractLinkComponent.no-nesting");
44             throw new ApplicationRuntimeException(message, component, null, null);
45         }
46
47         cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, component);
48
49         ILink link = component.getLink(cycle);
50
51         writer.begin("button");
52         writer.attribute("type", "button");
53
54         if (component.isDisabled())
55         {
56             writer.attribute("disabled", "disabled");
57         }
58
59         String JavaDoc url = link.getURL(component.getAnchor(), true);
60         String JavaDoc target = component.getTarget();
61         String JavaDoc onclick = (target == null) ? getScript(url) : getScript(url, target);
62
63         writer.attribute("onclick", onclick);
64
65         component.renderAdditionalAttributes(writer, cycle);
66
67         IMarkupWriter wrappedWriter = writer.getNestedWriter();
68
69         component.renderBody(wrappedWriter, cycle);
70
71         wrappedWriter.close();
72
73         writer.end();
74
75         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     protected String JavaDoc getScript(String JavaDoc url)
84     {
85         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     protected String JavaDoc getScript(String JavaDoc url, String JavaDoc target)
95     {
96         return "window.open('" + url + "','" + target + "')";
97     }
98 }
Popular Tags