KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > EditorBinder


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms;
25
26 import java.beans.PropertyEditor JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
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 /**
44  * This class is used to bind a form element to the property of a bean. The
45  * form element must implement the {@link org.riotfamily.forms.Editor}
46  * interface.
47  */

48 public class EditorBinder extends PropertyEditorRegistrySupport {
49
50     private Log log = LogFactory.getLog(EditorBinder.class);
51
52     /** List of {@link EditorBinding editor bindings} */
53     private List JavaDoc bindings = new LinkedList JavaDoc();
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 JavaDoc.class,new SqlDateEditor());
64         registerCustomEditor(Date JavaDoc.class, new CustomDateEditor(
65                 new SimpleDateFormat JavaDoc("yyyy-MM-dd"), false));
66
67         this.objectWrapper = objectWrapper;
68     }
69
70     public Class JavaDoc getBeanClass() {
71         return objectWrapper.getObjectClass();
72     }
73
74     public Object JavaDoc getBackingObject() {
75         return objectWrapper.getObject();
76     }
77
78     public void setBackingObject(Object JavaDoc 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 JavaDoc getPropertyValue(String JavaDoc property) {
90         return objectWrapper.getPropertyValue(property);
91     }
92
93     public void setPropertyValue(String JavaDoc property, Object JavaDoc value) {
94         objectWrapper.setPropertyValue(property, value);
95     }
96
97     public Class JavaDoc getPropertyType(String JavaDoc property) {
98         return objectWrapper.getPropertyType(property);
99     }
100
101     /**
102      * Binds the given editor to the property with the specified name.
103      *
104      * @param editor the editor to bind
105      * @param property the name of the property the editor is to be bound to
106      */

107     public void bind(Editor editor, String JavaDoc 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     /**
115      * Returns the editor that is bound to the given property.
116      */

117     public Editor getEditor(String JavaDoc property) {
118         if (property != null) {
119             int i = property.indexOf('.');
120             if (i != -1) {
121                 String JavaDoc 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 JavaDoc property) {
140         Iterator JavaDoc 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     /**
152      * Returns the names of all properties an editor is bound to.
153      * @since 6.4
154      */

155     public String JavaDoc[] getBoundProperties() {
156         String JavaDoc[] props = new String JavaDoc[bindings.size()];
157         Iterator JavaDoc 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     /**
174      * Initializes each editor with the property value it is bound to or
175      * <code>null<code> if the backingObject is not set.
176      *
177      * @see Editor#setValue(Object)
178      */

179     public void initEditors() {
180         Iterator JavaDoc it = bindings.iterator();
181         while (it.hasNext()) {
182             EditorBinding binding = (EditorBinding) it.next();
183             Editor editor = binding.getEditor();
184             Object JavaDoc value = null;
185             if (editingExistingBean) {
186                 value = getPropertyValue(binding.getProperty());
187             }
188             editor.setValue(value);
189         }
190     }
191
192     /**
193      * Sets the properties of the backingObject to the values provided by the
194      * corresponding editor. If the backingObject is <code>null</code> a new
195      * instance is created.
196      *
197      * @return the populated backingObject
198      */

199     public Object JavaDoc populateBackingObject() {
200         Iterator JavaDoc it = bindings.iterator();
201         while (it.hasNext()) {
202             EditorBinding binding = (EditorBinding) it.next();
203             Object JavaDoc oldValue = binding.getValue();
204             Object JavaDoc newValue = binding.getEditor().getValue();
205
206             // Only set the property if it has been changed. This prevents
207
// BeanWrapperImpl to perform type conversions which would fail
208
// for collections managed by Hibernate.
209

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 JavaDoc getPropertyEditor(Class JavaDoc type, String JavaDoc propertyPath) {
226         PropertyEditor JavaDoc 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 JavaDoc property;
238         
239         public EditorBindingImpl(Editor editor, String JavaDoc 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 JavaDoc getProperty() {
253             return property;
254         }
255         
256         public Object JavaDoc getValue() {
257             return getPropertyValue(property);
258         }
259         
260         public Class JavaDoc getBeanClass() {
261             return EditorBinder.this.getBeanClass();
262         }
263         
264         public String JavaDoc getPropertyPath() {
265             if (getParent() != null) {
266                 return getParent().getPropertyPath()
267                         + '.' + getProperty();
268             }
269             return getProperty();
270         }
271         
272         public Class JavaDoc getPropertyType() {
273             return EditorBinder.this.getPropertyType(property);
274         }
275         
276         public PropertyEditor JavaDoc getPropertyEditor() {
277             return EditorBinder.this.getPropertyEditor(
278                     getPropertyType(), getPropertyPath());
279         }
280         
281     }
282
283 }
Popular Tags