1 18 package org.alfresco.web.ui.repo.component; 19 20 import java.util.ArrayList ; 21 import java.util.List ; 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.el.ValueBinding; 28 import javax.faces.event.AbortProcessingException; 29 import javax.faces.event.ActionEvent; 30 import javax.faces.event.FacesEvent; 31 32 import org.alfresco.web.app.Application; 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 45 public class UIMultiValueEditor extends UIInput 46 { 47 private static final Log logger = LogFactory.getLog(UIMultiValueEditor.class); 48 private static final String MSG_SELECTED_ITEMS = "selected_items"; 49 private static final String MSG_NO_SELECTED_ITEMS = "no_selected_items"; 50 private static final String MSG_SELECT_ITEM = "select_an_item"; 51 52 53 public final static String ACTION_SEPARATOR = ";"; 54 public final static int ACTION_NONE = -1; 55 public final static int ACTION_REMOVE = 0; 56 public final static int ACTION_SELECT = 1; 57 public final static int ACTION_ADD = 2; 58 59 private Boolean addingNewItem = Boolean.FALSE; 60 private Boolean readOnly; 61 private Object lastItemAdded; 62 private String selectItemMsg; 63 private String selectedItemsMsg; 64 private String noSelectedItemsMsg; 65 66 69 72 public UIMultiValueEditor() 73 { 74 setRendererType("org.alfresco.faces.List"); 75 } 76 77 80 public String getFamily() 81 { 82 return "org.alfresco.faces.MultiValueEditor"; 83 } 84 85 88 public void restoreState(FacesContext context, Object state) 89 { 90 Object values[] = (Object [])state; 91 super.restoreState(context, values[0]); 93 this.lastItemAdded = values[1]; 94 this.readOnly = (Boolean )values[2]; 95 this.addingNewItem = (Boolean )values[3]; 96 this.selectItemMsg = (String )values[4]; 97 this.selectedItemsMsg = (String )values[5]; 98 this.noSelectedItemsMsg = (String )values[6]; 99 } 100 101 104 public Object saveState(FacesContext context) 105 { 106 Object values[] = new Object [7]; 107 values[0] = super.saveState(context); 109 values[1] = this.lastItemAdded; 110 values[2] = this.readOnly; 111 values[3] = this.addingNewItem; 112 values[4] = this.selectItemMsg; 113 values[5] = this.selectedItemsMsg; 114 values[6] = this.noSelectedItemsMsg; 115 return (values); 116 } 117 118 123 public Object getLastItemAdded() 124 { 125 ValueBinding vb = getValueBinding("lastItemAdded"); 126 if (vb != null) 127 { 128 this.lastItemAdded = vb.getValue(getFacesContext()); 129 } 130 131 return this.lastItemAdded; 132 } 133 134 139 public void setLastItemAdded(Object lastItemAdded) 140 { 141 this.lastItemAdded = lastItemAdded; 142 } 143 144 150 public String getSelectedItemsMsg() 151 { 152 ValueBinding vb = getValueBinding("selectedItemsMsg"); 153 if (vb != null) 154 { 155 this.selectedItemsMsg = (String )vb.getValue(getFacesContext()); 156 } 157 158 if (this.selectedItemsMsg == null) 159 { 160 this.selectedItemsMsg = Application.getMessage(getFacesContext(), MSG_SELECTED_ITEMS); 161 } 162 163 return this.selectedItemsMsg; 164 } 165 166 171 public void setSelectedItemsMsg(String selectedItemsMsg) 172 { 173 this.selectedItemsMsg = selectedItemsMsg; 174 } 175 176 182 public String getNoSelectedItemsMsg() 183 { 184 ValueBinding vb = getValueBinding("noSelectedItemsMsg"); 185 if (vb != null) 186 { 187 this.noSelectedItemsMsg = (String )vb.getValue(getFacesContext()); 188 } 189 190 if (this.noSelectedItemsMsg == null) 191 { 192 this.noSelectedItemsMsg = Application.getMessage(getFacesContext(), MSG_NO_SELECTED_ITEMS); 193 } 194 195 return this.noSelectedItemsMsg; 196 } 197 198 203 public void setNoSelectedItemsMsg(String noSelectedItemsMsg) 204 { 205 this.noSelectedItemsMsg = noSelectedItemsMsg; 206 } 207 208 214 public String getSelectItemMsg() 215 { 216 ValueBinding vb = getValueBinding("selectItemMsg"); 217 if (vb != null) 218 { 219 this.selectItemMsg = (String )vb.getValue(getFacesContext()); 220 } 221 222 if (this.selectItemMsg == null) 223 { 224 this.selectItemMsg = Application.getMessage(getFacesContext(), MSG_SELECT_ITEM); 225 } 226 227 return this.selectItemMsg; 228 } 229 230 235 public void setSelectItemMsg(String selectItemMsg) 236 { 237 this.selectItemMsg = selectItemMsg; 238 } 239 240 245 public boolean getReadOnly() 246 { 247 ValueBinding vb = getValueBinding("readOnly"); 248 if (vb != null) 249 { 250 this.readOnly = (Boolean )vb.getValue(getFacesContext()); 251 } 252 253 if (this.readOnly == null) 254 { 255 this.readOnly = Boolean.FALSE; 256 } 257 258 return this.readOnly.booleanValue(); 259 } 260 261 266 public void setReadOnly(boolean readOnly) 267 { 268 this.readOnly = Boolean.valueOf(readOnly); 269 } 270 271 276 public boolean getAddingNewItem() 277 { 278 return this.addingNewItem.booleanValue(); 279 } 280 281 284 public void broadcast(FacesEvent event) throws AbortProcessingException 285 { 286 if (event instanceof MultiValueEditorEvent) 287 { 288 MultiValueEditorEvent assocEvent = (MultiValueEditorEvent)event; 289 List items = (List )getValue(); 290 291 switch (assocEvent.Action) 292 { 293 case ACTION_SELECT: 294 { 295 this.addingNewItem = Boolean.TRUE; 296 break; 297 } 298 case ACTION_ADD: 299 { 300 if (items == null) 301 { 302 items = new ArrayList (); 303 setSubmittedValue(items); 304 } 305 306 items.add(getLastItemAdded()); 307 this.addingNewItem = Boolean.FALSE; 308 309 ValueBinding vb = getValueBinding("lastItemAdded"); 312 if (vb != null) 313 { 314 vb.setValue(FacesContext.getCurrentInstance(), null); 315 } 316 317 break; 318 } 319 case ACTION_REMOVE: 320 { 321 items.remove(assocEvent.RemoveIndex); 322 break; 323 } 324 } 325 } 326 else 327 { 328 super.broadcast(event); 329 } 330 } 331 332 333 336 public boolean getRendersChildren() 337 { 338 340 return !this.addingNewItem.booleanValue(); 341 } 342 343 346 349 public static class MultiValueEditorEvent extends ActionEvent 350 { 351 public int Action; 352 public int RemoveIndex; 353 354 public MultiValueEditorEvent(UIComponent component, int action, int removeIndex) 355 { 356 super(component); 357 this.Action = action; 358 this.RemoveIndex = removeIndex; 359 } 360 } 361 } 362 | Popular Tags |