1 17 package org.alfresco.web.ui.common.renderer; 18 19 import java.io.IOException ; 20 import java.util.Iterator ; 21 import java.util.Map ; 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 37 public class ModeListRenderer extends BaseRenderer 38 { 39 42 45 public void decode(FacesContext context, UIComponent component) 46 { 47 Map requestMap = context.getExternalContext().getRequestParameterMap(); 48 String fieldId = getHiddenFieldName(context, component); 49 String value = (String )requestMap.get(fieldId); 50 51 if (value != null && value.startsWith(component.getClientId(context) + NamingContainer.SEPARATOR_CHAR)) 53 { 54 Object 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 67 public void encodeBegin(FacesContext context, UIComponent component) throws IOException 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 attrs = list.getAttributes(); 79 80 if (list.isMenu() == false) 81 { 82 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 if (list.isHorizontal() == true) 91 { 92 out.write("<tr>"); 93 } 94 95 if (list.getLabel() != null) 97 { 98 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 if (list.getIconColumnWidth() != 0) 111 { 112 out.write("<td"); 113 outputAttribute(out, list.getIconColumnWidth(), "width"); 114 out.write("></td>"); 115 } 116 117 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 out.write("<table cellspacing=0 cellpadding=0 style='white-space:nowrap'><tr>"); 136 String selectedImage = (String )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 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 String label = list.getLabel(); 158 if (label == null || label.length() == 0) 159 { 160 for (Iterator i=list.getChildren().iterator(); i.hasNext(); ) 162 { 163 UIComponent child = (UIComponent)i.next(); 164 if (child instanceof UIListItem && child.isRendered() == true) 165 { 166 UIListItem item = (UIListItem)child; 168 169 if (item.getValue().equals(list.getValue()) == true) 171 { 172 label = item.getLabel(); 173 break; 174 } 175 } 176 } 177 } 178 179 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 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 out.write("<div id='"); 200 out.write(menuId); 201 out.write("' style=\"position:absolute;display:none;padding-left:2px;\">"); 202 203 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 215 public void encodeChildren(FacesContext context, UIComponent component) throws IOException 216 { 217 if (component.isRendered() == false) 218 { 219 return; 220 } 221 222 UIModeList list = (UIModeList)component; 223 Map attrs = list.getAttributes(); 224 225 ResponseWriter out = context.getResponseWriter(); 226 227 String selectedImage = (String )attrs.get("selectedImage"); 228 229 for (Iterator i=list.getChildren().iterator(); i.hasNext(); ) 231 { 232 UIComponent child = (UIComponent)i.next(); 233 if (child instanceof UIListItem && child.isRendered() == true) 234 { 235 UIListItem item = (UIListItem)child; 237 238 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 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 if (list.getIconColumnWidth() != 0) 264 { 265 out.write("<td"); 266 outputAttribute(out, list.getIconColumnWidth(), "width"); 267 out.write(">"); 268 269 if (selected == true && selectedImage != null) 271 { 272 out.write( Utils.buildImageTag(context, selectedImage, item.getTooltip()) ); 273 } 274 else 275 { 276 String image = (String )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 out.write("<td>"); 289 if (!list.isDisabled() && !item.isDisabled()) 290 { 291 out.write("<a HREF='#' onclick=\""); 292 String value = list.getClientId(context) + NamingContainer.SEPARATOR_CHAR + (String )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 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 340 public void encodeEnd(FacesContext context, UIComponent component) throws IOException 341 { 342 if (component.isRendered() == false) 343 { 344 return; 345 } 346 347 ResponseWriter out = context.getResponseWriter(); 348 349 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 out.write("</div>"); 360 } 361 } 362 363 366 public boolean getRendersChildren() 367 { 368 return true; 369 } 370 371 378 private static String 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 |