1 24 package org.riotfamily.forms.element.select; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import java.util.Set ; 31 32 import org.riotfamily.forms.ErrorUtils; 33 import org.riotfamily.forms.event.JavaScriptEvent; 34 import org.riotfamily.forms.request.FormRequest; 35 import org.springframework.beans.BeanUtils; 36 37 41 public abstract class AbstractMultiSelectElement 42 extends AbstractSelectElement { 43 44 private ArrayList selectedValues = new ArrayList (); 45 46 private Class collectionClass; 47 48 private Integer maxSelection; 49 50 57 public void setCollectionClass(Class collectionClass) { 58 this.collectionClass = collectionClass; 59 } 60 61 public void setMaxSelection(Integer maxSelection) { 62 this.maxSelection = maxSelection; 63 } 64 65 protected void afterBindingSet() { 66 if (collectionClass == null) { 67 Class type = getEditorBinding().getPropertyType(); 68 if (type.isInterface()) { 69 if (Set .class.isAssignableFrom(type)) { 70 collectionClass = HashSet .class; 71 } 72 else { 73 collectionClass = ArrayList .class; 74 } 75 } 76 else { 77 collectionClass = type; 78 } 79 } 80 } 81 82 public final void setValue(Object value) { 83 selectedValues = new ArrayList (); 84 if (value != null) { 85 Collection collection = (Collection ) value; 86 selectedValues.addAll(collection); 87 } 88 } 89 90 public Object getValue() { 91 Collection collection = null; 92 if (getEditorBinding() != null) { 93 collection = (Collection ) getEditorBinding().getValue(); 94 } 95 if (collection == null) { 96 collection = (Collection ) BeanUtils.instantiateClass(collectionClass); 97 } 98 else { 99 collection.clear(); 100 } 101 collection.addAll(selectedValues); 102 return collection; 103 } 104 105 protected Collection getSelectedValues() { 106 return selectedValues; 107 } 108 109 protected boolean hasSelection() { 110 return !selectedValues.isEmpty(); 111 } 112 113 public boolean isSelected(Option option) { 114 return selectedValues != null && 115 selectedValues.contains(option.getValue()); 116 } 117 118 121 public void processRequest(FormRequest request) { 122 updateSelection(request.getParameterValues(getParamName())); 123 } 124 125 private void updateSelection(String [] indexes) { 126 List options = getOptions(); 127 selectedValues = new ArrayList (); 128 if (indexes != null) { 129 for (int i = 0; i < indexes.length; i++) { 130 int index = Integer.parseInt(indexes[i]); 131 if (index != -1) { 132 selectedValues.add(((Option) options.get(index)).getValue()); 133 } 134 } 135 } 136 validate(); 137 } 138 139 protected void validate() { 140 super.validate(); 141 if (maxSelection != null && selectedValues.size() > maxSelection.intValue()) { 142 ErrorUtils.reject(this, "tooManyValuesSelected", maxSelection); 143 } 144 } 145 146 public void handleJavaScriptEvent(JavaScriptEvent event) { 147 if (event.getType() == JavaScriptEvent.ON_CHANGE) { 148 Collection oldValue = selectedValues; 149 updateSelection(event.getValues()); 150 fireChangeEvent(selectedValues, oldValue); 151 } 152 } 153 154 } | Popular Tags |