KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > common > renderer > ActionLinkRenderer


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.ui.common.renderer;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.faces.component.UIComponent;
24 import javax.faces.context.FacesContext;
25 import javax.faces.event.ActionEvent;
26
27 import org.alfresco.web.ui.common.Utils;
28 import org.alfresco.web.ui.common.component.UIActionLink;
29 import org.alfresco.web.ui.common.component.UIMenu;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * @author kevinr
35  */

36 public class ActionLinkRenderer extends BaseRenderer
37 {
38    private static Log logger = LogFactory.getLog(ActionLinkRenderer.class);
39    
40    // ------------------------------------------------------------------------------
41
// Renderer implementation
42

43    /**
44     * @see javax.faces.render.Renderer#decode(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
45     */

46    public void decode(FacesContext context, UIComponent component)
47    {
48       Map JavaDoc requestMap = context.getExternalContext().getRequestParameterMap();
49       String JavaDoc fieldId = Utils.getActionHiddenFieldName(context, component);
50       String JavaDoc value = (String JavaDoc)requestMap.get(fieldId);
51       // we are clicked if the hidden field contained our client id
52
if (value != null && value.equals(component.getClientId(context)))
53       {
54          // get all the params for this actionlink, see if any values have been set
55
// on the request which match our params and set them into the component
56
UIActionLink link = (UIActionLink)component;
57          Map JavaDoc<String JavaDoc, String JavaDoc> destParams = link.getParameterMap();
58          destParams.clear();
59          Map JavaDoc<String JavaDoc, String JavaDoc> actionParams = getParameterMap(link);
60          if (actionParams != null)
61          {
62             for (String JavaDoc name : actionParams.keySet())
63             {
64                String JavaDoc paramValue = (String JavaDoc)requestMap.get(name);
65                destParams.put(name, paramValue);
66             }
67          }
68          
69          ActionEvent event = new ActionEvent(component);
70          component.queueEvent(event);
71       }
72    }
73    
74    /**
75     * @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
76     */

77    public void encodeEnd(FacesContext context, UIComponent component) throws IOException JavaDoc
78    {
79       // always check for this flag - as per the spec
80
if (component.isRendered() == true)
81       {
82          Writer JavaDoc out = context.getResponseWriter();
83          
84          UIActionLink link = (UIActionLink)component;
85          
86          if (isInMenu(link) == true)
87          {
88             // render as menu item
89
out.write( renderMenuAction(context, link) );
90          }
91          else
92          {
93             // render as action link
94
out.write( renderActionLink(context, link) );
95          }
96       }
97    }
98    
99    /**
100     * Render ActionLink as plain link and image
101     *
102     * @param context
103     * @param link
104     *
105     * @return action link HTML
106     */

107    private String JavaDoc renderActionLink(FacesContext context, UIActionLink link)
108    {
109       Map JavaDoc attrs = link.getAttributes();
110       StringBuilder JavaDoc linkBuf = new StringBuilder JavaDoc(256);
111       
112       if (link.getHref() == null)
113       {
114          linkBuf.append("<a HREF='#' onclick=\"");
115          
116          // if we have an overriden onclick add that
117
if (link.getOnclick() != null)
118          {
119             linkBuf.append(link.getOnclick());
120          }
121          else
122          {
123             // generate JavaScript to set a hidden form field and submit
124
// a form which request attributes that we can decode
125
linkBuf.append(Utils.generateFormSubmit(context, link, Utils.getActionHiddenFieldName(context, link), link.getClientId(context), getParameterMap(link)));
126          }
127          
128          linkBuf.append('"');
129       }
130       else
131       {
132          String JavaDoc href = link.getHref();
133          if (href.startsWith("http") == false && href.startsWith("file") == false)
134          {
135             href = context.getExternalContext().getRequestContextPath() + href;
136          }
137          linkBuf.append("<a HREF=\"")
138                 .append(href)
139                 .append('"');
140          
141          // output href 'target' attribute if supplied
142
if (link.getTarget() != null)
143          {
144             linkBuf.append(" target=\"")
145                    .append(link.getTarget())
146                    .append("\"");
147          }
148       }
149       
150       if (attrs.get("style") != null)
151       {
152          linkBuf.append(" style=\"")
153                 .append(attrs.get("style"))
154                 .append('"');
155       }
156       if (attrs.get("styleClass") != null)
157       {
158          linkBuf.append(" class=")
159                 .append(attrs.get("styleClass"));
160       }
161       if (link.getTooltip() != null)
162       {
163          linkBuf.append(" title=\"")
164                 .append(Utils.encode(link.getTooltip()))
165                 .append('"');
166       }
167       linkBuf.append('>');
168       
169       StringBuilder JavaDoc buf = new StringBuilder JavaDoc(350);
170       if (link.getImage() != null)
171       {
172          int padding = link.getPadding();
173          if (padding != 0)
174          {
175             // TODO: make this width value a property!
176
buf.append("<table cellspacing=0 cellpadding=0><tr><td width=16>");
177          }
178          
179          if (link.getShowLink() == false)
180          {
181             buf.append(linkBuf.toString());
182          }
183          
184          // TODO: allow configuring of alignment attribute
185
buf.append(Utils.buildImageTag(context, link.getImage(), (String JavaDoc)link.getValue(), "absmiddle"));
186          
187          if (link.getShowLink() == false)
188          {
189             buf.append("</a>");
190          }
191          else
192          {
193             if (padding != 0)
194             {
195                buf.append("</td><td style=\"padding:")
196                   .append(padding)
197                   .append("px\">");
198             }
199             else
200             {
201                // TODO: add horizontal spacing as component property
202
buf.append("<span style='padding-left:2px");
203                
204                // text next to an image may need alignment
205
if (attrs.get("verticalAlign") != null)
206                {
207                   buf.append(";vertical-align:")
208                      .append(attrs.get("verticalAlign"));
209                }
210                
211                buf.append("'>");
212             }
213             
214             buf.append(linkBuf.toString());
215             buf.append(Utils.encode(link.getValue().toString()));
216             buf.append("</a>");
217             
218             if (padding == 0)
219             {
220                buf.append("</span>");
221             }
222          }
223          
224          if (padding != 0)
225          {
226             buf.append("</td></tr></table>");
227          }
228       }
229       else
230       {
231          buf.append(linkBuf.toString());
232          buf.append(Utils.encode(link.getValue().toString()));
233          buf.append("</a>");
234       }
235       
236       return buf.toString();
237    }
238    
239    /**
240     * Render ActionLink as menu image and item link
241     *
242     * @param context
243     * @param link
244     *
245     * @return action link HTML
246     */

247    private String JavaDoc renderMenuAction(FacesContext context, UIActionLink link)
248    {
249       StringBuilder JavaDoc buf = new StringBuilder JavaDoc(256);
250       
251       buf.append("<tr><td>");
252       
253       // render image cell first for a menu
254
if (link.getImage() != null)
255       {
256          buf.append(Utils.buildImageTag(context, link.getImage(), (String JavaDoc)link.getValue()));
257       }
258       
259       buf.append("</td><td");
260       int padding = link.getPadding();
261       if (padding != 0)
262       {
263          buf.append(" style=\"padding:")
264             .append(padding)
265             .append("px\"");
266       }
267       buf.append(">");
268       
269       // render text link cell for the menu
270
if (link.getHref() == null)
271       {
272          buf.append("<a HREF='#' onclick=\"");
273          buf.append(Utils.generateFormSubmit(context, link, Utils.getActionHiddenFieldName(context, link), link.getClientId(context), getParameterMap(link)));
274          buf.append('"');
275       }
276       else
277       {
278          String JavaDoc href = link.getHref();
279          if (href.startsWith("http") == false)
280          {
281             href = context.getExternalContext().getRequestContextPath() + href;
282          }
283          buf.append("<a HREF=\"")
284             .append(href)
285             .append('"');
286          
287          // output href 'target' attribute if supplied
288
if (link.getTarget() != null)
289          {
290             buf.append(" target=\"")
291                .append(link.getTarget())
292                .append("\"");
293          }
294       }
295       
296       Map JavaDoc attrs = link.getAttributes();
297       if (attrs.get("style") != null)
298       {
299          buf.append(" style=\"")
300             .append(attrs.get("style"))
301             .append('"');
302       }
303       if (attrs.get("styleClass") != null)
304       {
305          buf.append(" class=")
306             .append(attrs.get("styleClass"));
307       }
308       buf.append('>');
309       buf.append(Utils.encode(link.getValue().toString()));
310       buf.append("</a>");
311       
312       buf.append("</td></tr>");
313       
314       return buf.toString();
315    }
316    
317    
318    // ------------------------------------------------------------------------------
319
// Private helpers
320

321    /**
322     * Return true if the action link is present within a UIMenu component container
323     *
324     * @param link The ActionLink to test
325     *
326     * @return true if the action link is present within a UIMenu component
327     */

328    private static boolean isInMenu(UIActionLink link)
329    {
330       UIComponent parent = link.getParent();
331       while (parent != null)
332       {
333          if (parent instanceof UIMenu)
334          {
335             break;
336          }
337          parent = parent.getParent();
338       }
339       return (parent != null);
340    }
341 }
342
Popular Tags