KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.faces.component.NamingContainer;
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.UIForm;
26 import javax.faces.context.FacesContext;
27 import javax.faces.context.ResponseWriter;
28
29 import org.alfresco.web.ui.common.Utils;
30 import org.alfresco.web.ui.common.component.UIListItem;
31 import org.alfresco.web.ui.common.component.UIMenu;
32 import org.alfresco.web.ui.common.component.UIModeList;
33
34 /**
35  * @author kevinr
36  */

37 public class ModeListRenderer extends BaseRenderer
38 {
39    // ------------------------------------------------------------------------------
40
// Renderer implemenation
41

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

45    public void decode(FacesContext context, UIComponent component)
46    {
47       Map JavaDoc requestMap = context.getExternalContext().getRequestParameterMap();
48       String JavaDoc fieldId = getHiddenFieldName(context, component);
49       String JavaDoc value = (String JavaDoc)requestMap.get(fieldId);
50       
51       // we encoded the value to start with our Id
52
if (value != null && value.startsWith(component.getClientId(context) + NamingContainer.SEPARATOR_CHAR))
53       {
54          // found a new selected value for this ModeList
55
// queue an event to represent the change
56
// TODO: NOTE: The value object is passed in as a String here - is this a problem?
57
// As the 'value' field for a ModeListItem can contain Object...
58
Object JavaDoc selectedValue = value.substring(component.getClientId(context).length() + 1);
59          UIModeList.ModeListItemSelectedEvent event = new UIModeList.ModeListItemSelectedEvent(component, selectedValue);
60          component.queueEvent(event);
61       }
62    }
63
64    /**
65     * @see javax.faces.render.Renderer#encodeBegin(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
66     */

67    public void encodeBegin(FacesContext context, UIComponent component) throws IOException JavaDoc
68    {
69       if (component.isRendered() == false)
70       {
71          return;
72       }
73       
74       UIModeList list = (UIModeList)component;
75       
76       ResponseWriter out = context.getResponseWriter();
77
78       Map JavaDoc attrs = list.getAttributes();
79       
80       if (list.isMenu() == false)
81       {
82          // start outer table container the list items
83
out.write("<table cellspacing=1 cellpadding=0");
84          outputAttribute(out, attrs.get("styleClass"), "class");
85          outputAttribute(out, attrs.get("style"), "style");
86          outputAttribute(out, attrs.get("width"), "width");
87          out.write('>');
88          
89          // horizontal rendering outputs a single row with each item as a column cell
90
if (list.isHorizontal() == true)
91          {
92             out.write("<tr>");
93          }
94          
95          // output title row if present
96
if (list.getLabel() != null)
97          {
98             // each row is an inner table with a single row and 2 columns
99
// first column contains an icon if present, second column contains text
100
if (list.isHorizontal() == false)
101             {
102                out.write("<tr>");
103             }
104             
105             out.write("<td><table cellpadding=0 width=100%");
106             outputAttribute(out, attrs.get("itemSpacing"), "cellspacing");
107             out.write("><tr>");
108             
109             // output icon column
110
if (list.getIconColumnWidth() != 0)
111             {
112                out.write("<td");
113                outputAttribute(out, list.getIconColumnWidth(), "width");
114                out.write("></td>");
115             }
116             
117             // output title label
118
out.write("<td><span");
119             outputAttribute(out, attrs.get("labelStyle"), "style");
120             outputAttribute(out, attrs.get("labelStyleClass"), "class");
121             out.write('>');
122             out.write(Utils.encode(list.getLabel()));
123             out.write("</span></td></tr></table></td>");
124             
125             if (list.isHorizontal() == false)
126             {
127                out.write("</tr>");
128             }
129          }
130       }
131       else
132       {
133          // render as a pop-up menu
134
// TODO: show the image set for the individual item if available?
135
out.write("<table cellspacing=0 cellpadding=0 style='white-space:nowrap'><tr>");
136          String JavaDoc selectedImage = (String JavaDoc)attrs.get("selectedImage");
137          if (selectedImage != null)
138          {
139             out.write("<td");
140             int colWidth = list.getIconColumnWidth();
141             if (colWidth != 0)
142             {
143                out.write(" width=");
144                out.write(Integer.toString(colWidth));
145             }
146             out.write('>');
147             out.write(Utils.buildImageTag(context, selectedImage, null, "absmiddle"));
148             out.write("</td>");
149          }
150          
151          String JavaDoc menuId = UIMenu.getNextMenuId(list, context);
152          out.write("<td><a HREF='#' onclick=\"javascript:_toggleMenu(event, '");
153          out.write(menuId);
154          out.write("');return false;\">");
155          
156          // use default label if available
157
String JavaDoc label = list.getLabel();
158          if (label == null || label.length() == 0)
159          {
160             // else get the child components and walk to find the selected
161
for (Iterator JavaDoc i=list.getChildren().iterator(); i.hasNext(); /**/)
162             {
163                UIComponent child = (UIComponent)i.next();
164                if (child instanceof UIListItem && child.isRendered() == true)
165                {
166                   // found a valid UIListItem child to render
167
UIListItem item = (UIListItem)child;
168                   
169                   // if selected render as the label
170
if (item.getValue().equals(list.getValue()) == true)
171                   {
172                      label = item.getLabel();
173                      break;
174                   }
175                }
176             }
177          }
178          
179          // render the label
180
if (label != null && label.length() != 0)
181          {
182             out.write("<span");
183             outputAttribute(out, attrs.get("labelStyle"), "style");
184             outputAttribute(out, attrs.get("labelStyleClass"), "class");
185             out.write('>');
186             out.write(Utils.encode(label));
187             out.write("</span>");
188          }
189          
190          // output image
191
if (list.getMenuImage() != null)
192          {
193             out.write(Utils.buildImageTag(context, list.getMenuImage(), null, "absmiddle"));
194          }
195          
196          out.write("</a></td></tr></table>");
197          
198          // output the hidden DIV section to contain the menu item table
199
out.write("<div id='");
200          out.write(menuId);
201          out.write("' style=\"position:absolute;display:none;padding-left:2px;\">");
202          
203          // start outer table container the list items
204
out.write("<table cellspacing=1 cellpadding=0");
205          outputAttribute(out, attrs.get("styleClass"), "class");
206          outputAttribute(out, attrs.get("style"), "style");
207          outputAttribute(out, attrs.get("width"), "width");
208          out.write('>');
209       }
210    }
211    
212    /**
213     * @see javax.faces.render.Renderer#encodeChildren(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
214     */

215    public void encodeChildren(FacesContext context, UIComponent component) throws IOException JavaDoc
216    {
217       if (component.isRendered() == false)
218       {
219          return;
220       }
221       
222       UIModeList list = (UIModeList)component;
223       Map JavaDoc attrs = list.getAttributes();
224       
225       ResponseWriter out = context.getResponseWriter();
226       
227       String JavaDoc selectedImage = (String JavaDoc)attrs.get("selectedImage");
228       
229       // get the child components
230
for (Iterator JavaDoc i=list.getChildren().iterator(); i.hasNext(); /**/)
231       {
232          UIComponent child = (UIComponent)i.next();
233          if (child instanceof UIListItem && child.isRendered() == true)
234          {
235             // found a valid UIListItem child to render
236
UIListItem item = (UIListItem)child;
237             
238             // each row is an inner table with a single row and 2 columns
239
// first column contains an icon if present, second column contains text
240
if (list.isHorizontal() == false)
241             {
242                out.write("<tr>");
243             }
244             
245             out.write("<td><table cellpadding=0 width=100%");
246             outputAttribute(out, attrs.get("itemSpacing"), "cellspacing");
247             
248             // if selected value render different style for the item
249
boolean selected = item.getValue().equals(list.getValue());
250             if (selected == true)
251             {
252                outputAttribute(out, attrs.get("selectedStyleClass"), "class");
253                outputAttribute(out, attrs.get("selectedStyle"), "style");
254             }
255             else
256             {
257                outputAttribute(out, attrs.get("itemStyleClass"), "class");
258                outputAttribute(out, attrs.get("itemStyle"), "style");
259             }
260             out.write("><tr>");
261             
262             // output icon column
263
if (list.getIconColumnWidth() != 0)
264             {
265                out.write("<td");
266                outputAttribute(out, list.getIconColumnWidth(), "width");
267                out.write(">");
268                
269                // if the "selectedImage" property is set and this item is selected then show it
270
if (selected == true && selectedImage != null)
271                {
272                   out.write( Utils.buildImageTag(context, selectedImage, item.getTooltip()) );
273                }
274                else
275                {
276                   // else show the image set for the individual item
277
String JavaDoc image = (String JavaDoc)child.getAttributes().get("image");
278                   if (image != null)
279                   {
280                      out.write( Utils.buildImageTag(context, image, item.getTooltip()) );
281                   }
282                }
283                
284                out.write("</td>");
285             }
286             
287             // output item link
288
out.write("<td>");
289             if (!list.isDisabled() && !item.isDisabled())
290             {
291                out.write("<a HREF='#' onclick=\"");
292                // generate javascript to submit the value of the child component
293
String JavaDoc value = list.getClientId(context) + NamingContainer.SEPARATOR_CHAR + (String JavaDoc)child.getAttributes().get("value");
294                out.write(Utils.generateFormSubmit(context, list, getHiddenFieldName(context, list), value));
295                out.write('"');
296             }
297             else
298             {
299                out.write("<span");
300                outputAttribute(out, attrs.get("disabledStyleClass"), "class");
301                outputAttribute(out, attrs.get("disabledStyle"), "style");
302             }
303             
304             // render style for the item link
305
if (item.getValue().equals(list.getValue()))
306             {
307                outputAttribute(out, attrs.get("selectedLinkStyleClass"), "class");
308                outputAttribute(out, attrs.get("selectedLinkStyle"), "style");
309             }
310             else
311             {
312                outputAttribute(out, attrs.get("itemLinkStyleClass"), "class");
313                outputAttribute(out, attrs.get("itemLinkStyle"), "style");
314             }
315             
316             outputAttribute(out, child.getAttributes().get("tooltip"), "title");
317             out.write('>');
318             out.write(Utils.encode(item.getLabel()));
319             if (!list.isDisabled() && !item.isDisabled())
320             {
321                out.write("</a>");
322             }
323             else
324             {
325                out.write("</span>");
326             }
327             out.write("</td></tr></table></td>");
328             
329             if (list.isHorizontal() == false)
330             {
331                out.write("</tr>");
332             }
333          }
334       }
335    }
336
337    /**
338     * @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
339     */

340    public void encodeEnd(FacesContext context, UIComponent component) throws IOException JavaDoc
341    {
342       if (component.isRendered() == false)
343       {
344          return;
345       }
346       
347       ResponseWriter out = context.getResponseWriter();
348       
349       // end outer table
350
UIModeList list = (UIModeList)component;
351       if (list.isHorizontal() == true)
352       {
353          out.write("</tr>");
354       }
355       out.write("</table>");
356       if (list.isMenu() == true)
357       {
358          // close menu hidden div section
359
out.write("</div>");
360       }
361    }
362
363    /**
364     * @see javax.faces.render.Renderer#getRendersChildren()
365     */

366    public boolean getRendersChildren()
367    {
368       return true;
369    }
370    
371    /**
372     * We use a hidden field name based on the parent form component Id and
373     * the string "modelist" to give a hidden field name that can be shared by all
374     * ModeList components within a single UIForm component.
375     *
376     * @return hidden field name
377     */

378    private static String JavaDoc getHiddenFieldName(FacesContext context, UIComponent component)
379    {
380       UIForm form = Utils.getParentForm(context, component);
381       return form.getClientId(context) + NamingContainer.SEPARATOR_CHAR + "modelist";
382    }
383 }
384
Popular Tags