KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.tapestry.AbstractComponent;
24 import org.apache.tapestry.IMarkupWriter;
25 import org.apache.tapestry.IRequestCycle;
26 import org.apache.tapestry.PageRenderSupport;
27 import org.apache.tapestry.TapestryUtils;
28 import org.apache.tapestry.components.ILinkComponent;
29 import org.apache.tapestry.components.LinkEventType;
30 import org.apache.tapestry.engine.IEngineService;
31 import org.apache.tapestry.engine.ILink;
32
33 /**
34  * Base class for implementations of {@link ILinkComponent}. Includes a disabled attribute (that
35  * should be bound to a disabled parameter), an anchor attribute, and a renderer attribute (that
36  * should be bound to a renderer parameter). A default, shared instance of
37  * {@link org.apache.tapestry.link.DefaultLinkRenderer}is used when no specific renderer is
38  * provided.
39  *
40  * @author Howard Lewis Ship
41  */

42
43 public abstract class AbstractLinkComponent extends AbstractComponent implements ILinkComponent
44 {
45     private Map JavaDoc _eventHandlers;
46
47     public abstract boolean isDisabled();
48
49     /**
50      * Adds an event handler (typically, from a wrapped component such as a
51      * {@link org.apache.tapestry.html.Rollover}).
52      */

53
54     public void addEventHandler(LinkEventType eventType, String JavaDoc functionName)
55     {
56         Object JavaDoc currentValue;
57
58         if (_eventHandlers == null)
59             _eventHandlers = new HashMap JavaDoc();
60
61         currentValue = _eventHandlers.get(eventType);
62
63         // The first value is added as a String
64

65         if (currentValue == null)
66         {
67             _eventHandlers.put(eventType, functionName);
68             return;
69         }
70
71         // When adding the second value, convert to a List
72

73         if (currentValue instanceof String JavaDoc)
74         {
75             List JavaDoc list = new ArrayList JavaDoc();
76             list.add(currentValue);
77             list.add(functionName);
78
79             _eventHandlers.put(eventType, list);
80             return;
81         }
82
83         // For the third and up, add the new function to the List
84

85         List JavaDoc list = (List JavaDoc) currentValue;
86         list.add(functionName);
87     }
88
89     /**
90      * Renders the link by delegating to an instance of {@link ILinkRenderer}.
91      */

92
93     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
94     {
95         getRenderer().renderLink(writer, cycle, this);
96     }
97
98     protected void cleanupAfterRender(IRequestCycle cycle)
99     {
100         _eventHandlers = null;
101
102         super.cleanupAfterRender(cycle);
103     }
104
105     protected void writeEventHandlers(IMarkupWriter writer, IRequestCycle cycle)
106     {
107         String JavaDoc name = null;
108
109         if (_eventHandlers == null)
110             return;
111
112         PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
113
114         Iterator JavaDoc i = _eventHandlers.entrySet().iterator();
115
116         while (i.hasNext())
117         {
118             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
119             LinkEventType type = (LinkEventType) entry.getKey();
120
121             name = writeEventHandler(
122                     writer,
123                     pageRenderSupport,
124                     name,
125                     type.getAttributeName(),
126                     entry.getValue());
127         }
128
129     }
130
131     protected String JavaDoc writeEventHandler(IMarkupWriter writer, PageRenderSupport pageRenderSupport,
132             String JavaDoc name, String JavaDoc attributeName, Object JavaDoc value)
133     {
134         String JavaDoc wrapperFunctionName;
135
136         if (value instanceof String JavaDoc)
137         {
138             wrapperFunctionName = (String JavaDoc) value;
139         }
140         else
141         {
142             String JavaDoc finalName = name == null ? pageRenderSupport.getUniqueString("Link") : name;
143
144             wrapperFunctionName = attributeName + "_" + finalName;
145
146             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
147
148             buffer.append("function ");
149             buffer.append(wrapperFunctionName);
150             buffer.append(" ()\n{\n");
151
152             Iterator JavaDoc i = ((List JavaDoc) value).iterator();
153             while (i.hasNext())
154             {
155                 String JavaDoc functionName = (String JavaDoc) i.next();
156                 buffer.append(" ");
157                 buffer.append(functionName);
158                 buffer.append("();\n");
159             }
160
161             buffer.append("}\n\n");
162
163             pageRenderSupport.addBodyScript(buffer.toString());
164         }
165
166         writer.attribute(attributeName, "javascript:" + wrapperFunctionName + "();");
167
168         return name;
169     }
170
171     /** @since 3.0 * */
172
173     public abstract ILinkRenderer getRenderer();
174
175     public abstract void setRenderer(ILinkRenderer renderer);
176
177     public void renderAdditionalAttributes(IMarkupWriter writer, IRequestCycle cycle)
178     {
179         writeEventHandlers(writer, cycle);
180
181         // Generate additional attributes from informal parameters.
182

183         renderInformalParameters(writer, cycle);
184     }
185
186     /**
187      * Utility method for subclasses; Gets the named service from the engine and invokes
188      * {@link IEngineService#getLink(IRequestCycle, org.apache.tapestry.IComponent, Object[])}on
189      * it.
190      *
191      * @since 3.0
192      * @deprecated To be removed in 4.1; links may now have the necessary engine service injected.
193      */

194
195     protected ILink getLink(IRequestCycle cycle, String JavaDoc serviceName, Object JavaDoc parameter)
196     {
197         IEngineService service = cycle.getEngine().getService(serviceName);
198
199         return service.getLink(cycle, parameter);
200     }
201
202     public abstract String JavaDoc getAnchor();
203
204     public ILink getLink(IRequestCycle cycle)
205     {
206         return null;
207     }
208
209     /**
210      * Sets the renderer parameter property to its default value
211      * {@link DefaultLinkRenderer#SHARED_INSTANCE}.
212      *
213      * @since 3.0
214      */

215     protected void finishLoad()
216     {
217         setRenderer(DefaultLinkRenderer.SHARED_INSTANCE);
218     }
219
220 }
Popular Tags