1 24 package org.riotfamily.forms.factory; 25 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.riotfamily.common.beans.propertyeditors.IntArrayPropertyEditor; 33 import org.riotfamily.forms.BeanEditor; 34 import org.riotfamily.forms.ContainerElement; 35 import org.riotfamily.forms.Editor; 36 import org.riotfamily.forms.Element; 37 import org.riotfamily.forms.ElementFactory; 38 import org.riotfamily.forms.Form; 39 import org.springframework.beans.BeanUtils; 40 import org.springframework.beans.BeanWrapper; 41 import org.springframework.beans.BeanWrapperImpl; 42 import org.springframework.beans.MutablePropertyValues; 43 import org.springframework.beans.PropertyValue; 44 import org.springframework.beans.PropertyValues; 45 import org.springframework.beans.factory.BeanFactory; 46 import org.springframework.beans.factory.BeanFactoryAware; 47 import org.springframework.beans.factory.config.RuntimeBeanReference; 48 49 50 56 public class ConfigurableElementFactory implements ContainerElementFactory, 57 EditorElementFactory { 58 59 private Log log = LogFactory.getLog(ConfigurableElementFactory.class); 60 61 62 private Class elementClass; 63 64 65 private String bind; 66 67 68 private Class beanClass; 69 70 71 private PropertyValues propertyValues; 72 73 74 private List childFactories = new LinkedList (); 75 76 77 private BeanFactory beanFactory; 78 79 82 public ConfigurableElementFactory(Class elementClass) { 83 this.elementClass = elementClass; 84 } 85 86 public Class getElementClass() { 87 return this.elementClass; 88 } 89 90 93 public void setBeanFactory(BeanFactory beanFactory) { 94 this.beanFactory = beanFactory; 95 } 96 97 100 public void setPropertyValues(PropertyValues propertyValues) { 101 this.propertyValues = propertyValues; 102 } 103 104 111 public void setBeanClass(Class beanClass) { 112 if (beanClass != null && !BeanEditor.class 113 .isAssignableFrom(elementClass)) { 114 115 throw new FormDefinitionException("Element class must implement " 116 + BeanEditor.class.getName()); 117 } 118 this.beanClass = beanClass; 119 } 120 121 124 public Class getBeanClass() { 125 return beanClass; 126 } 127 128 135 public void setBind(String bind) { 136 if (bind != null && !Editor.class.isAssignableFrom(elementClass)) { 137 throw new FormDefinitionException("Element class must implement " 138 + Editor.class.getName()); 139 } 140 this.bind = bind; 141 } 142 143 150 public String getBind() { 151 return bind; 152 } 153 154 162 public void setChildFactories(List childFactories) { 163 if (childFactories != null && !ContainerElement.class 164 .isAssignableFrom(elementClass)) { 165 166 throw new FormDefinitionException("Element class must implement " 167 + ContainerElement.class.getName()); 168 } 169 this.childFactories = childFactories; 170 } 171 172 176 public void addChildFactory(ElementFactory factory) { 177 if (!ContainerElement.class.isAssignableFrom(elementClass)) { 178 throw new FormDefinitionException("Element class must implement " 179 + ContainerElement.class.getName()); 180 } 181 childFactories.add(factory); 182 } 183 184 188 public List getChildFactories() { 189 return childFactories; 190 } 191 192 196 public Element createElement(Element parent, Form form) { 197 log.debug("Creating element " + elementClass); 198 Element element = (Element) BeanUtils.instantiateClass(elementClass); 199 element.setParent(parent); 200 if (beanClass != null) { 201 BeanEditor bee = (BeanEditor) element; 202 bee.setBeanClass(beanClass); 203 } 204 if (element instanceof BeanFactoryAware) { 205 BeanFactoryAware bfa = (BeanFactoryAware) element; 206 bfa.setBeanFactory(beanFactory); 207 } 208 populateElement(element); 209 210 if (element instanceof Editor) { 211 if (bind != null) { 212 log.debug("Bind: " + bind); 213 BeanEditor beanEditor = findEditor(parent, form); 214 Editor editor = (Editor) element; 215 beanEditor.bind(editor, bind); 216 } 217 } 218 219 if (element instanceof ContainerElement) { 220 createChildElements((ContainerElement) element, form); 221 } 222 223 return element; 224 } 225 226 232 protected void populateElement(Element element) { 233 if (propertyValues == null) { 234 return; 235 } 236 BeanWrapper beanWrapper = new BeanWrapperImpl(element); 237 beanWrapper.registerCustomEditor(int[].class, new IntArrayPropertyEditor()); 238 MutablePropertyValues pvs = new MutablePropertyValues(); 239 PropertyValue[] pvArray = propertyValues.getPropertyValues(); 240 for (int i = 0; i < pvArray.length; i++) { 241 PropertyValue pv = pvArray[i]; 242 Object resolvedValue = resolveValueIfNecessary(pv.getValue()); 243 pvs.addPropertyValue(pvArray[i].getName(), resolvedValue); 244 } 245 beanWrapper.setPropertyValues(pvs); 246 } 247 248 254 protected Object resolveValueIfNecessary(Object value) { 255 if (value instanceof RuntimeBeanReference) { 256 RuntimeBeanReference ref = (RuntimeBeanReference) value; 257 return beanFactory.getBean(ref.getBeanName()); 258 } 259 return value; 260 } 261 262 protected void createChildElements(ContainerElement parent, Form form) { 263 if (childFactories != null) { 264 Iterator it = childFactories.iterator(); 265 while (it.hasNext()) { 266 ElementFactory factory = (ElementFactory) it.next(); 267 Element child = factory.createElement(parent, form); 268 parent.addElement(child); 269 } 270 } 271 } 272 273 protected BeanEditor findEditor(Element parent, Form form) { 274 while (parent != null) { 275 log.debug("Checking " + parent); 276 if (parent instanceof BeanEditor) { 277 log.debug("Found."); 278 return (BeanEditor) parent; 279 } 280 parent = parent.getParent(); 281 } 282 return form; 283 } 284 285 } 286 | Popular Tags |