1 17 package org.alfresco.web.ui.repo.renderer; 18 19 import java.io.IOException ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import javax.faces.component.UIComponent; 24 import javax.faces.context.FacesContext; 25 import javax.faces.context.ResponseWriter; 26 27 import org.alfresco.service.cmr.repository.NodeRef; 28 import org.alfresco.service.cmr.repository.NodeService; 29 import org.alfresco.web.app.Application; 30 import org.alfresco.web.bean.repository.Repository; 31 import org.alfresco.web.ui.common.Utils; 32 import org.alfresco.web.ui.common.renderer.BaseRenderer; 33 import org.alfresco.web.ui.repo.component.UIMultiValueEditor; 34 import org.alfresco.web.ui.repo.component.UIMultiValueEditor.MultiValueEditorEvent; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 44 public class MultiValueListEditorRenderer extends BaseRenderer 45 { 46 private static Log logger = LogFactory.getLog(MultiValueListEditorRenderer.class); 47 48 49 private final static String MSG_REMOVE = "remove"; 50 private final static String MSG_SELECT_BUTTON = "select_button"; 51 private final static String MSG_ADD_TO_LIST_BUTTON = "add_to_list_button"; 52 53 private boolean highlightedRow; 54 55 58 61 public void decode(FacesContext context, UIComponent component) 62 { 63 Map requestMap = context.getExternalContext().getRequestParameterMap(); 64 Map valuesMap = context.getExternalContext().getRequestParameterValuesMap(); 65 String fieldId = getHiddenFieldName(component); 66 String value = (String )requestMap.get(fieldId); 67 68 int action = UIMultiValueEditor.ACTION_NONE; 69 int removeIndex = -1; 70 if (value != null && value.length() != 0) 71 { 72 int sepIdx = value.indexOf(UIMultiValueEditor.ACTION_SEPARATOR); 74 if (sepIdx != -1) 75 { 76 action = Integer.parseInt(value.substring(0, sepIdx)); 77 removeIndex = Integer.parseInt(value.substring(sepIdx+1)); 78 } 79 else 80 { 81 action = Integer.parseInt(value); 82 } 83 } 84 85 if (action != UIMultiValueEditor.ACTION_NONE) 86 { 87 MultiValueEditorEvent event = new MultiValueEditorEvent(component, action, removeIndex); 88 component.queueEvent(event); 89 } 90 91 super.decode(context, component); 92 } 93 94 97 public void encodeBegin(FacesContext context, UIComponent component) throws IOException 98 { 99 if (component.isRendered() == false) 100 { 101 return; 102 } 103 104 this.highlightedRow = false; 106 107 if (component instanceof UIMultiValueEditor) 108 { 109 ResponseWriter out = context.getResponseWriter(); 110 Map attrs = component.getAttributes(); 111 String clientId = component.getClientId(context); 112 UIMultiValueEditor editor = (UIMultiValueEditor)component; 113 114 out.write("<table border='0' cellspacing='4' cellpadding='4' class='selector'"); 116 this.outputAttribute(out, attrs.get("style"), "style"); 117 this.outputAttribute(out, attrs.get("styleClass"), "styleClass"); 118 out.write(">"); 119 120 out.write("<tr><td>"); 122 out.write("1. "); 123 out.write(editor.getSelectItemMsg()); 124 out.write("</td></tr>"); 125 126 if (editor.getAddingNewItem()) 127 { 128 out.write("<tr><td style='padding-left:8px'>"); 129 } 130 else 131 { 132 out.write("<tr><td style='padding-left:8px;'><input type='submit' value='"); 133 out.write(Application.getMessage(context, MSG_SELECT_BUTTON)); 134 out.write("' onclick=\""); 135 out.write(generateFormSubmit(context, component, Integer.toString(UIMultiValueEditor.ACTION_SELECT))); 136 out.write("\"/></td></tr>"); 137 } 138 } 139 } 140 141 144 public void encodeEnd(FacesContext context, UIComponent component) throws IOException 145 { 146 if (component instanceof UIMultiValueEditor) 147 { 148 ResponseWriter out = context.getResponseWriter(); 149 UIMultiValueEditor editor = (UIMultiValueEditor)component; 150 151 NodeService nodeService = Repository.getServiceRegistry(context).getNodeService(); 153 154 if (editor.getAddingNewItem()) 155 { 156 out.write("</td></tr>"); 157 } 158 159 out.write("<tr><td>2. <input type='submit'"); 161 if (editor.getAddingNewItem() == false && editor.getLastItemAdded() != null || 162 editor.getLastItemAdded() == null) 163 { 164 out.write(" disabled='true'"); 165 } 166 out.write(" value='"); 167 out.write(Application.getMessage(context, MSG_ADD_TO_LIST_BUTTON)); 168 out.write("' onclick=\""); 169 out.write(generateFormSubmit(context, component, Integer.toString(UIMultiValueEditor.ACTION_ADD))); 170 out.write("\"/></td></tr>"); 171 172 out.write("<tr><td style='padding-top:8px'>"); 173 out.write(editor.getSelectedItemsMsg()); 174 out.write("</td></tr>"); 175 176 out.write("<tr><td><table cellspacing='0' cellpadding='2' border='0' class='selectedItems'>"); 178 out.write("<tr><td colspan='2' class='selectedItemsHeader'>"); 179 out.write(Application.getMessage(context, "name")); 180 out.write("</td></tr>"); 181 182 List currentItems = (List )editor.getValue(); 183 if (currentItems != null && currentItems.size() > 0) 184 { 185 for (int x = 0; x < currentItems.size(); x++) 186 { 187 Object obj = currentItems.get(x); 188 if (obj != null) 189 { 190 if (obj instanceof NodeRef) 191 { 192 if (nodeService.exists((NodeRef)obj)) 193 { 194 renderExistingItem(context, component, out, nodeService, x, obj); 195 } 196 else 197 { 198 currentItems.remove(x); 200 } 201 } 202 else 203 { 204 renderExistingItem(context, component, out, nodeService, x, obj); 205 } 206 } 207 } 208 } 209 else 210 { 211 out.write("<tr><td class='selectedItemsRow'>"); 212 out.write(editor.getNoSelectedItemsMsg()); 213 out.write("</td></tr>"); 214 } 215 216 out.write("</table></td></tr></table>"); 218 } 219 } 220 221 232 protected void renderExistingItem(FacesContext context, UIComponent component, ResponseWriter out, 233 NodeService nodeService, int index, Object value) throws IOException 234 { 235 out.write("<tr><td class='"); 236 if (this.highlightedRow) 237 { 238 out.write("selectedItemsRowAlt"); 239 } 240 else 241 { 242 out.write("selectedItemsRow"); 243 } 244 out.write("'>"); 245 246 if (value instanceof NodeRef) 247 { 248 out.write(Repository.getNameForNode(nodeService, (NodeRef)value)); 249 } 250 else 251 { 252 out.write(value.toString()); 253 } 254 255 out.write(" "); 256 out.write("</td><td class='"); 257 if (this.highlightedRow) 258 { 259 out.write("selectedItemsRowAlt"); 260 } 261 else 262 { 263 out.write("selectedItemsRow"); 264 } 265 out.write("'><a HREF='#' title='"); 266 out.write(Application.getMessage(context, MSG_REMOVE)); 267 out.write("' onclick=\""); 268 out.write(generateFormSubmit(context, component, UIMultiValueEditor.ACTION_REMOVE + UIMultiValueEditor.ACTION_SEPARATOR + index)); 269 out.write("\"><img SRC='"); 270 out.write(context.getExternalContext().getRequestContextPath()); 271 out.write("/images/icons/delete.gif' border='0' width='13' height='16'/></a>"); 272 273 this.highlightedRow = !this.highlightedRow; 274 } 275 276 281 private String getHiddenFieldName(UIComponent component) 282 { 283 return component.getClientId(FacesContext.getCurrentInstance()); 284 } 285 286 295 private String generateFormSubmit(FacesContext context, UIComponent component, String action) 296 { 297 return Utils.generateFormSubmit(context, component, getHiddenFieldName(component), action); 298 } 299 } 300 | Popular Tags |