1 24 package org.riotfamily.forms; 25 26 import java.beans.PropertyEditor ; 27 import java.text.SimpleDateFormat ; 28 import java.util.Date ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.riotfamily.common.beans.ObjectWrapper; 36 import org.riotfamily.common.beans.propertyeditors.SqlDateEditor; 37 import org.springframework.beans.InvalidPropertyException; 38 import org.springframework.beans.PropertyEditorRegistrar; 39 import org.springframework.beans.PropertyEditorRegistrySupport; 40 import org.springframework.beans.propertyeditors.CustomDateEditor; 41 42 43 48 public class EditorBinder extends PropertyEditorRegistrySupport { 49 50 private Log log = LogFactory.getLog(EditorBinder.class); 51 52 53 private List bindings = new LinkedList (); 54 55 private ObjectWrapper objectWrapper; 56 57 private boolean editingExistingBean; 58 59 private EditorBinding parent; 60 61 public EditorBinder(ObjectWrapper objectWrapper) { 62 registerDefaultEditors(); 63 registerCustomEditor(java.sql.Date .class,new SqlDateEditor()); 64 registerCustomEditor(Date .class, new CustomDateEditor( 65 new SimpleDateFormat ("yyyy-MM-dd"), false)); 66 67 this.objectWrapper = objectWrapper; 68 } 69 70 public Class getBeanClass() { 71 return objectWrapper.getObjectClass(); 72 } 73 74 public Object getBackingObject() { 75 return objectWrapper.getObject(); 76 } 77 78 public void setBackingObject(Object backingObject) { 79 editingExistingBean = backingObject != null; 80 if (backingObject != null) { 81 objectWrapper.setObject(backingObject); 82 } 83 } 84 85 public boolean isEditingExistingBean() { 86 return editingExistingBean; 87 } 88 89 public Object getPropertyValue(String property) { 90 return objectWrapper.getPropertyValue(property); 91 } 92 93 public void setPropertyValue(String property, Object value) { 94 objectWrapper.setPropertyValue(property, value); 95 } 96 97 public Class getPropertyType(String property) { 98 return objectWrapper.getPropertyType(property); 99 } 100 101 107 public void bind(Editor editor, String property) { 108 log.debug("Binding " + editor + " to property " + property); 109 EditorBinding eb = new EditorBindingImpl(editor, property); 110 bindings.add(eb); 111 editor.setEditorBinding(eb); 112 } 113 114 117 public Editor getEditor(String property) { 118 if (property != null) { 119 int i = property.indexOf('.'); 120 if (i != -1) { 121 String nested = property.substring(i + 1); 122 property = property.substring(0, i); 123 Editor editor = findEditorByProperty(property); 124 if (editor instanceof BeanEditor) { 125 BeanEditor be = (BeanEditor) editor; 126 return be.getEditor(nested); 127 } 128 else { 129 throw new InvalidPropertyException(getBeanClass(), 130 property, "Nested editor must implement the " + 131 "BeanEditor interface"); 132 } 133 } 134 return findEditorByProperty(property); 135 } 136 return null; 137 } 138 139 protected Editor findEditorByProperty(String property) { 140 Iterator it = bindings.iterator(); 141 while (it.hasNext()) { 142 EditorBinding binding = (EditorBinding) it.next(); 143 if (property.equals(binding.getProperty())) { 144 return binding.getEditor(); 145 } 146 } 147 throw new InvalidPropertyException(getBeanClass(), property, 148 "No editor bound to property"); 149 } 150 151 155 public String [] getBoundProperties() { 156 String [] props = new String [bindings.size()]; 157 Iterator it = bindings.iterator(); 158 for (int i = 0; it.hasNext(); i++) { 159 EditorBinding binding = (EditorBinding) it.next(); 160 props[i] = binding.getProperty(); 161 } 162 return props; 163 } 164 165 public void registerPropertyEditors(PropertyEditorRegistrar[] registrars) { 166 if (registrars != null) { 167 for (int i = 0; i < registrars.length; i++) { 168 registrars[i].registerCustomEditors(this); 169 } 170 } 171 } 172 173 179 public void initEditors() { 180 Iterator it = bindings.iterator(); 181 while (it.hasNext()) { 182 EditorBinding binding = (EditorBinding) it.next(); 183 Editor editor = binding.getEditor(); 184 Object value = null; 185 if (editingExistingBean) { 186 value = getPropertyValue(binding.getProperty()); 187 } 188 editor.setValue(value); 189 } 190 } 191 192 199 public Object populateBackingObject() { 200 Iterator it = bindings.iterator(); 201 while (it.hasNext()) { 202 EditorBinding binding = (EditorBinding) it.next(); 203 Object oldValue = binding.getValue(); 204 Object newValue = binding.getEditor().getValue(); 205 206 210 if (newValue != oldValue) { 211 setPropertyValue(binding.getProperty(), newValue); 212 } 213 } 214 return getBackingObject(); 215 } 216 217 public EditorBinding getParent() { 218 return this.parent; 219 } 220 221 public void setParent(EditorBinding parent) { 222 this.parent = parent; 223 } 224 225 public PropertyEditor getPropertyEditor(Class type, String propertyPath) { 226 PropertyEditor pe = findCustomEditor(type, propertyPath); 227 if (pe == null) { 228 pe = getDefaultEditor(type); 229 } 230 return pe; 231 } 232 233 private class EditorBindingImpl implements EditorBinding { 234 235 private Editor editor; 236 237 private String property; 238 239 public EditorBindingImpl(Editor editor, String property) { 240 this.editor = editor; 241 this.property = property; 242 } 243 244 public EditorBinder getEditorBinder() { 245 return EditorBinder.this; 246 } 247 248 public Editor getEditor() { 249 return editor; 250 } 251 252 public String getProperty() { 253 return property; 254 } 255 256 public Object getValue() { 257 return getPropertyValue(property); 258 } 259 260 public Class getBeanClass() { 261 return EditorBinder.this.getBeanClass(); 262 } 263 264 public String getPropertyPath() { 265 if (getParent() != null) { 266 return getParent().getPropertyPath() 267 + '.' + getProperty(); 268 } 269 return getProperty(); 270 } 271 272 public Class getPropertyType() { 273 return EditorBinder.this.getPropertyType(property); 274 } 275 276 public PropertyEditor getPropertyEditor() { 277 return EditorBinder.this.getPropertyEditor( 278 getPropertyType(), getPropertyPath()); 279 } 280 281 } 282 283 } | Popular Tags |