1 17 package org.alfresco.web.ui.common.renderer; 18 19 import java.io.IOException ; 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import javax.faces.component.UIComponent; 25 import javax.faces.component.UIInput; 26 import javax.faces.context.FacesContext; 27 import javax.faces.context.ResponseWriter; 28 29 import org.alfresco.config.Config; 30 import org.alfresco.config.ConfigElement; 31 import org.alfresco.config.ConfigService; 32 import org.alfresco.web.app.Application; 33 import org.alfresco.web.ui.common.Utils; 34 import org.alfresco.web.ui.common.component.UIImagePicker; 35 import org.alfresco.web.ui.common.component.UIListItem; 36 import org.alfresco.web.ui.common.component.UIListItems; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 46 public class ImagePickerRadioRenderer extends BaseRenderer 47 { 48 private static Log logger = LogFactory.getLog(ImagePickerRadioRenderer.class); 49 50 private int columns; 51 private int position; 52 private boolean open; 53 54 57 60 public void decode(FacesContext context, UIComponent component) 61 { 62 if (Utils.isComponentDisabledOrReadOnly(component)) 63 { 64 return; 65 } 66 67 String clientId = component.getClientId(context); 68 Map paramsMap = context.getExternalContext().getRequestParameterMap(); 69 70 String submittedValue = (String )paramsMap.get(clientId); 71 72 if (logger.isDebugEnabled()) 73 logger.debug("Submitted value = " + submittedValue); 74 75 ((UIInput)component).setSubmittedValue(submittedValue); 76 } 77 78 81 public void encodeBegin(FacesContext context, UIComponent component) throws IOException 82 { 83 if (component.isRendered() == false) 84 { 85 return; 86 } 87 88 this.columns = 1; 90 this.position = 0; 91 this.open = false; 92 93 ResponseWriter out = context.getResponseWriter(); 94 95 UIImagePicker imagePicker = (UIImagePicker)component; 96 97 Map attrs = imagePicker.getAttributes(); 98 out.write("<table cellpadding='0'"); 99 outputAttribute(out, attrs.get("spacing"), "cellspacing"); 100 outputAttribute(out, attrs.get("styleClass"), "class"); 101 outputAttribute(out, attrs.get("style"), "style"); 102 out.write(">\n"); 103 } 104 105 108 @SuppressWarnings ("unchecked") 109 public void encodeChildren(FacesContext context, UIComponent component) throws IOException 110 { 111 if (component.isRendered() == false) 112 { 113 return; 114 } 115 116 UIImagePicker imagePicker = (UIImagePicker)component; 117 Map attrs = imagePicker.getAttributes(); 118 119 Integer cols = (Integer )attrs.get("columns"); 120 if (cols != null && cols instanceof Integer ) 121 { 122 this.columns = cols.intValue(); 123 } 124 125 String onclick = (String )attrs.get("onclick"); 127 128 ResponseWriter out = context.getResponseWriter(); 129 130 String configSection = (String )attrs.get("configSection"); 133 134 if (configSection != null && configSection.length() > 0) 135 { 136 ConfigService cfgService = Application.getConfigService(context); 139 Config cfg = cfgService.getConfig(configSection); 140 if (cfg != null) 141 { 142 ConfigElement iconsCfg = cfg.getConfigElement("icons"); 143 if (iconsCfg != null) 144 { 145 for (ConfigElement icon : iconsCfg.getChildren()) 146 { 147 String iconName = icon.getAttribute("name"); 148 String iconPath = icon.getAttribute("path"); 149 150 if (iconName != null && iconPath != null) 151 { 152 UIListItem item = new UIListItem(); 153 item.setValue(iconName); 154 item.getAttributes().put("image", iconPath); 155 renderItem(context, out, imagePicker, item, onclick); 156 } 157 } 158 } 159 } 160 } 161 else 162 { 163 for (Iterator i = imagePicker.getChildren().iterator(); i.hasNext(); ) 165 { 166 UIComponent child = (UIComponent)i.next(); 167 if (child instanceof UIListItems) 168 { 169 Object listItems = ((UIListItems)child).getValue(); 172 if (listItems instanceof Collection ) 173 { 174 Iterator iter = ((Collection )listItems).iterator(); 175 while (iter.hasNext()) 176 { 177 UIListItem item = (UIListItem)iter.next(); 178 renderItem(context, out, imagePicker, item, onclick); 179 } 180 } 181 } 182 else if (child instanceof UIListItem && child.isRendered() == true) 183 { 184 UIListItem item = (UIListItem)child; 186 renderItem(context, out, imagePicker, item, onclick); 187 } 188 } 189 } 190 191 if (open) 193 { 194 out.write("</tr>\n"); 195 } 196 } 197 198 201 public void encodeEnd(FacesContext context, UIComponent component) throws IOException 202 { 203 if (component.isRendered() == false) 204 { 205 return; 206 } 207 208 ResponseWriter out = context.getResponseWriter(); 209 out.write("</table>"); 210 } 211 212 215 public boolean getRendersChildren() 216 { 217 return true; 218 } 219 220 229 private void renderItem(FacesContext context, ResponseWriter out, 230 UIImagePicker imagePicker, UIListItem item, String onclick) 231 throws IOException 232 { 233 String tooltip = (String )item.getAttributes().get("tooltip"); 234 235 if ((this.position % this.columns) == 0) 237 { 238 if (this.open) 240 { 241 out.write("</tr>\n"); 242 this.open = false; 243 } 244 245 out.write("<tr>"); 246 247 this.open = true; 249 } 250 251 out.write("<td>"); 253 254 out.write("<input type='radio' name='"); 255 out.write(imagePicker.getClientId(context)); 256 out.write("' id='"); 257 out.write(imagePicker.getClientId(context)); 258 out.write("' value='"); 259 out.write(item.getValue().toString()); 262 out.write("'"); 263 264 Object currentValue = imagePicker.getSubmittedValue(); 266 if (currentValue == null) 267 { 268 currentValue = imagePicker.getValue(); 269 } 270 271 Object itemValue = item.getValue(); 272 if (itemValue != null && itemValue.equals(currentValue)) 273 { 274 out.write(" checked='true'"); 275 } 276 277 if (tooltip != null) 278 { 279 out.write(" title='"); 280 out.write(Utils.encode(tooltip)); 281 out.write("'"); 282 } 283 284 if (onclick != null) 285 { 286 out.write(" onclick='"); 287 out.write(onclick); 288 out.write("'"); 289 } 290 291 296 out.write(">"); 297 out.write("</td><td align='center'>"); 298 299 String image = (String )item.getAttributes().get("image"); 301 if (image == null) 302 { 303 throw new IllegalStateException ("All child items must specify an image"); 304 } 305 306 out.write(Utils.buildImageTag(context, image, tooltip)); 307 308 String label = (String )item.getAttributes().get("label"); 309 if (label != null && label.length() > 0) 310 { 311 out.write("<br/>"); 312 out.write(Utils.encode(label)); 313 } 314 315 out.write("</td>"); 316 317 this.position++; 319 } 320 } 321 | Popular Tags |