Mercurial > projects > dwt2
comparison org.eclipse.core.databinding.beans/src/org/eclipse/core/databinding/beans/PojoObservables.d @ 95:6208d4f6a277
Added trees for databinding.beans and observable
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 21 Apr 2009 10:55:51 +0200 |
parents | |
children | b74ac5dfcc06 |
comparison
equal
deleted
inserted
replaced
94:1d37a7813832 | 95:6208d4f6a277 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2007, 2008 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Matthew Hall - bug 221704 | |
11 *******************************************************************************/ | |
12 | |
13 package org.eclipse.core.databinding.beans; | |
14 | |
15 import java.beans.PropertyChangeEvent; | |
16 import java.beans.PropertyDescriptor; | |
17 | |
18 import org.eclipse.core.databinding.observable.IObservable; | |
19 import org.eclipse.core.databinding.observable.Realm; | |
20 import org.eclipse.core.databinding.observable.list.IObservableList; | |
21 import org.eclipse.core.databinding.observable.map.IObservableMap; | |
22 import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory; | |
23 import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables; | |
24 import org.eclipse.core.databinding.observable.set.IObservableSet; | |
25 import org.eclipse.core.databinding.observable.value.IObservableValue; | |
26 import org.eclipse.core.internal.databinding.beans.BeanObservableListDecorator; | |
27 import org.eclipse.core.internal.databinding.beans.BeanObservableMapDecorator; | |
28 import org.eclipse.core.internal.databinding.beans.BeanObservableSetDecorator; | |
29 import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator; | |
30 import org.eclipse.core.internal.databinding.beans.JavaBeanObservableList; | |
31 import org.eclipse.core.internal.databinding.beans.JavaBeanObservableMap; | |
32 import org.eclipse.core.internal.databinding.beans.JavaBeanObservableSet; | |
33 import org.eclipse.core.internal.databinding.beans.JavaBeanObservableValue; | |
34 import org.eclipse.core.internal.databinding.beans.JavaBeanPropertyObservableMap; | |
35 | |
36 /** | |
37 * A factory for creating observable objects for POJOs (plain old java objects) | |
38 * that conform to idea of an object with getters and setters but does not | |
39 * provide {@link PropertyChangeEvent property change events} on change. This | |
40 * factory is identical to {@link BeansObservables} except for this fact. | |
41 * | |
42 * @since 1.1 | |
43 */ | |
44 final public class PojoObservables { | |
45 | |
46 /** | |
47 * Returns an observable value in the default realm tracking the current | |
48 * value of the named property of the given pojo. | |
49 * | |
50 * @param pojo | |
51 * the object | |
52 * @param propertyName | |
53 * the name of the property | |
54 * @return an observable value tracking the current value of the named | |
55 * property of the given pojo | |
56 */ | |
57 public static IObservableValue observeValue(Object pojo, String propertyName) { | |
58 return observeValue(Realm.getDefault(), pojo, propertyName); | |
59 } | |
60 | |
61 /** | |
62 * Returns an observable value in the given realm tracking the current value | |
63 * of the named property of the given pojo. | |
64 * | |
65 * @param realm | |
66 * the realm | |
67 * @param pojo | |
68 * the object | |
69 * @param propertyName | |
70 * the name of the property | |
71 * @return an observable value tracking the current value of the named | |
72 * property of the given pojo | |
73 */ | |
74 public static IObservableValue observeValue(Realm realm, Object pojo, | |
75 String propertyName) { | |
76 | |
77 PropertyDescriptor descriptor = BeansObservables.getPropertyDescriptor( | |
78 pojo.getClass(), propertyName); | |
79 return new JavaBeanObservableValue(realm, pojo, descriptor, false); | |
80 } | |
81 | |
82 /** | |
83 * Returns an observable map in the default realm tracking the current | |
84 * values of the named property for the pojos in the given set. | |
85 * | |
86 * @param domain | |
87 * the set of pojo objects | |
88 * @param pojoClass | |
89 * the common base type of pojo objects that may be in the set | |
90 * @param propertyName | |
91 * the name of the property | |
92 * @return an observable map tracking the current values of the named | |
93 * property for the pojos in the given domain set | |
94 */ | |
95 public static IObservableMap observeMap(IObservableSet domain, | |
96 Class pojoClass, String propertyName) { | |
97 PropertyDescriptor descriptor = BeansObservables.getPropertyDescriptor( | |
98 pojoClass, propertyName); | |
99 return new JavaBeanObservableMap(domain, descriptor, false); | |
100 } | |
101 | |
102 /** | |
103 * Returns an array of observable maps in the default realm tracking the | |
104 * current values of the named propertys for the pojos in the given set. | |
105 * | |
106 * @param domain | |
107 * the set of objects | |
108 * @param pojoClass | |
109 * the common base type of objects that may be in the set | |
110 * @param propertyNames | |
111 * the array of property names | |
112 * @return an array of observable maps tracking the current values of the | |
113 * named propertys for the pojos in the given domain set | |
114 */ | |
115 public static IObservableMap[] observeMaps(IObservableSet domain, | |
116 Class pojoClass, String[] propertyNames) { | |
117 IObservableMap[] result = new IObservableMap[propertyNames.length]; | |
118 for (int i = 0; i < propertyNames.length; i++) { | |
119 result[i] = observeMap(domain, pojoClass, propertyNames[i]); | |
120 } | |
121 return result; | |
122 } | |
123 | |
124 /** | |
125 * Returns an observable map in the given realm tracking the map-typed named | |
126 * property of the given pojo object. | |
127 * | |
128 * @param realm | |
129 * the realm | |
130 * @param pojo | |
131 * the pojo object | |
132 * @param propertyName | |
133 * the name of the property | |
134 * @return an observable map tracking the map-typed named property of the | |
135 * given pojo object | |
136 */ | |
137 public static IObservableMap observeMap(Realm realm, Object pojo, | |
138 String propertyName) { | |
139 PropertyDescriptor descriptor = BeansObservables.getPropertyDescriptor( | |
140 pojo.getClass(), propertyName); | |
141 return new JavaBeanPropertyObservableMap(realm, pojo, descriptor, false); | |
142 } | |
143 | |
144 /** | |
145 * Returns an observable list in the given realm tracking the | |
146 * collection-typed named property of the given pojo object. The returned | |
147 * list is mutable. | |
148 * | |
149 * @param realm | |
150 * the realm | |
151 * @param pojo | |
152 * the object | |
153 * @param propertyName | |
154 * the name of the collection-typed property | |
155 * @return an observable list tracking the collection-typed named property | |
156 * of the given pojo object | |
157 * @see #observeList(Realm, Object, String, Class) | |
158 */ | |
159 public static IObservableList observeList(Realm realm, Object pojo, | |
160 String propertyName) { | |
161 return observeList(realm, pojo, propertyName, null); | |
162 } | |
163 | |
164 /** | |
165 * Returns an observable list in the given realm tracking the | |
166 * collection-typed named property of the given bean object. The returned | |
167 * list is mutable. When an item is added or removed the setter is invoked | |
168 * for the list on the parent bean to provide notification to other | |
169 * listeners via <code>PropertyChangeEvents</code>. This is done to | |
170 * provide the same behavior as is expected from arrays as specified in the | |
171 * bean spec in section 7.2. | |
172 * | |
173 * @param realm | |
174 * the realm | |
175 * @param pojo | |
176 * the bean object | |
177 * @param propertyName | |
178 * the name of the property | |
179 * @param elementType | |
180 * type of the elements in the list. If <code>null</code> and | |
181 * the property is an array the type will be inferred. If | |
182 * <code>null</code> and the property type cannot be inferred | |
183 * element type will be <code>null</code>. | |
184 * @return an observable list tracking the collection-typed named property | |
185 * of the given bean object | |
186 */ | |
187 public static IObservableList observeList(Realm realm, Object pojo, | |
188 String propertyName, Class elementType) { | |
189 PropertyDescriptor propertyDescriptor = BeansObservables | |
190 .getPropertyDescriptor(pojo.getClass(), propertyName); | |
191 elementType = BeansObservables.getCollectionElementType(elementType, | |
192 propertyDescriptor); | |
193 | |
194 return new JavaBeanObservableList(realm, pojo, propertyDescriptor, | |
195 elementType, false); | |
196 } | |
197 | |
198 /** | |
199 * Returns an observable set in the given realm tracking the | |
200 * collection-typed named property of the given pojo object. | |
201 * | |
202 * @param realm | |
203 * the realm | |
204 * @param pojo | |
205 * the pojo object | |
206 * @param propertyName | |
207 * the name of the property | |
208 * @return an observable set tracking the collection-typed named property of | |
209 * the given pojo object | |
210 */ | |
211 public static IObservableSet observeSet(Realm realm, Object pojo, | |
212 String propertyName) { | |
213 return observeSet(realm, pojo, propertyName, null); | |
214 } | |
215 | |
216 /** | |
217 * @param realm | |
218 * @param pojo | |
219 * @param propertyName | |
220 * @param elementType | |
221 * can be <code>null</code> | |
222 * @return an observable set that tracks the current value of the named | |
223 * property for given pojo object | |
224 */ | |
225 public static IObservableSet observeSet(Realm realm, Object pojo, | |
226 String propertyName, Class elementType) { | |
227 PropertyDescriptor propertyDescriptor = BeansObservables | |
228 .getPropertyDescriptor(pojo.getClass(), propertyName); | |
229 elementType = BeansObservables.getCollectionElementType(elementType, | |
230 propertyDescriptor); | |
231 | |
232 return new JavaBeanObservableSet(realm, pojo, propertyDescriptor, | |
233 elementType, false); | |
234 } | |
235 | |
236 /** | |
237 * Returns a factory for creating obervable values tracking the given | |
238 * property of a particular pojo object | |
239 * | |
240 * @param realm | |
241 * the realm to use | |
242 * @param propertyName | |
243 * the name of the property | |
244 * @return an observable value factory | |
245 */ | |
246 public static IObservableFactory valueFactory(final Realm realm, | |
247 final String propertyName) { | |
248 return new class() IObservableFactory { | |
249 public IObservable createObservable(Object target) { | |
250 return observeValue(realm, target, propertyName); | |
251 } | |
252 }; | |
253 } | |
254 | |
255 /** | |
256 * Returns a factory for creating obervable lists tracking the given | |
257 * property of a particular pojo object | |
258 * | |
259 * @param realm | |
260 * the realm to use | |
261 * @param propertyName | |
262 * the name of the property | |
263 * @param elementType | |
264 * @return an observable list factory | |
265 */ | |
266 public static IObservableFactory listFactory(final Realm realm, | |
267 final String propertyName, final Class elementType) { | |
268 return new class() IObservableFactory { | |
269 public IObservable createObservable(Object target) { | |
270 return observeList(realm, target, propertyName, elementType); | |
271 } | |
272 }; | |
273 } | |
274 | |
275 /** | |
276 * Returns a factory for creating obervable sets tracking the given property | |
277 * of a particular pojo object | |
278 * | |
279 * @param realm | |
280 * the realm to use | |
281 * @param propertyName | |
282 * the name of the property | |
283 * @return an observable set factory | |
284 */ | |
285 public static IObservableFactory setFactory(final Realm realm, | |
286 final String propertyName) { | |
287 return new class() IObservableFactory { | |
288 public IObservable createObservable(Object target) { | |
289 return observeSet(realm, target, propertyName); | |
290 } | |
291 }; | |
292 } | |
293 | |
294 /** | |
295 * @param realm | |
296 * @param propertyName | |
297 * @param elementType | |
298 * can be <code>null</code> | |
299 * @return an observable set factory for creating observable sets | |
300 */ | |
301 public static IObservableFactory setFactory(final Realm realm, | |
302 final String propertyName, final Class elementType) { | |
303 return new class() IObservableFactory { | |
304 public IObservable createObservable(Object target) { | |
305 return observeSet(realm, target, propertyName, elementType); | |
306 } | |
307 }; | |
308 } | |
309 | |
310 /** | |
311 * Returns a factory for creating an observable map. The factory, when | |
312 * provided with a pojo object, will create an {@link IObservableMap} in the | |
313 * given realm that tracks the map-typed named property for the specified | |
314 * pojo. | |
315 * | |
316 * @param realm | |
317 * the realm assigned to observables created by the returned | |
318 * factory. | |
319 * @param propertyName | |
320 * the name of the property | |
321 * @return a factory for creating {@link IObservableMap} objects. | |
322 */ | |
323 public static IObservableFactory mapPropertyFactory(final Realm realm, | |
324 final String propertyName) { | |
325 return new class() IObservableFactory { | |
326 public IObservable createObservable(Object target) { | |
327 return observeMap(realm, target, propertyName); | |
328 } | |
329 }; | |
330 } | |
331 | |
332 /** | |
333 * Helper method for | |
334 * <code>MasterDetailObservables.detailValue(master, valueFactory(realm, | |
335 propertyName), propertyType)</code> | |
336 * | |
337 * @param realm | |
338 * @param master | |
339 * @param propertyName | |
340 * @param propertyType | |
341 * can be <code>null</code> | |
342 * @return an observable value that tracks the current value of the named | |
343 * property for the current value of the master observable value | |
344 * | |
345 * @see MasterDetailObservables | |
346 */ | |
347 public static IObservableValue observeDetailValue(Realm realm, | |
348 IObservableValue master, String propertyName, Class propertyType) { | |
349 | |
350 IObservableValue value = MasterDetailObservables.detailValue(master, | |
351 valueFactory(realm, propertyName), propertyType); | |
352 BeanObservableValueDecorator decorator = new BeanObservableValueDecorator( | |
353 value, master, BeansObservables.getValueTypePropertyDescriptor( | |
354 master, propertyName)); | |
355 | |
356 return decorator; | |
357 } | |
358 | |
359 /** | |
360 * Helper method for | |
361 * <code>MasterDetailObservables.detailList(master, listFactory(realm, | |
362 propertyName, propertyType), propertyType)</code> | |
363 * | |
364 * @param realm | |
365 * @param master | |
366 * @param propertyName | |
367 * @param propertyType | |
368 * can be <code>null</code> | |
369 * @return an observable list that tracks the named property for the current | |
370 * value of the master observable value | |
371 * | |
372 * @see MasterDetailObservables | |
373 */ | |
374 public static IObservableList observeDetailList(Realm realm, | |
375 IObservableValue master, String propertyName, Class propertyType) { | |
376 IObservableList observableList = MasterDetailObservables.detailList( | |
377 master, listFactory(realm, propertyName, propertyType), | |
378 propertyType); | |
379 BeanObservableListDecorator decorator = new BeanObservableListDecorator( | |
380 observableList, master, BeansObservables | |
381 .getValueTypePropertyDescriptor(master, propertyName)); | |
382 | |
383 return decorator; | |
384 } | |
385 | |
386 /** | |
387 * Helper method for | |
388 * <code>MasterDetailObservables.detailSet(master, setFactory(realm, | |
389 propertyName), propertyType)</code> | |
390 * | |
391 * @param realm | |
392 * @param master | |
393 * @param propertyName | |
394 * @param propertyType | |
395 * can be <code>null</code> | |
396 * @return an observable set that tracks the named property for the current | |
397 * value of the master observable value | |
398 * | |
399 * @see MasterDetailObservables | |
400 */ | |
401 public static IObservableSet observeDetailSet(Realm realm, | |
402 IObservableValue master, String propertyName, Class propertyType) { | |
403 | |
404 IObservableSet observableSet = MasterDetailObservables.detailSet( | |
405 master, setFactory(realm, propertyName, propertyType), | |
406 propertyType); | |
407 BeanObservableSetDecorator decorator = new BeanObservableSetDecorator( | |
408 observableSet, master, BeansObservables | |
409 .getValueTypePropertyDescriptor(master, propertyName)); | |
410 | |
411 return decorator; | |
412 } | |
413 | |
414 /** | |
415 * Helper method for | |
416 * <code>MasterDetailObservables.detailMap(master, mapFactory(realm, propertyName))</code> | |
417 * | |
418 * @param realm | |
419 * @param master | |
420 * @param propertyName | |
421 * @return an observable map that tracks the map-type named property for the | |
422 * current value of the master observable value. | |
423 */ | |
424 public static IObservableMap observeDetailMap(Realm realm, | |
425 IObservableValue master, String propertyName) { | |
426 IObservableMap observableMap = MasterDetailObservables.detailMap( | |
427 master, mapPropertyFactory(realm, propertyName)); | |
428 BeanObservableMapDecorator decorator = new BeanObservableMapDecorator( | |
429 observableMap, master, BeansObservables | |
430 .getValueTypePropertyDescriptor(master, propertyName)); | |
431 return decorator; | |
432 } | |
433 } |