1 13 package org.eclipse.core.databinding.beans; 14 15 import java.beans.BeanInfo ; 16 import java.beans.IntrospectionException ; 17 import java.beans.Introspector ; 18 import java.beans.PropertyDescriptor ; 19 20 import org.eclipse.core.databinding.BindingException; 21 import org.eclipse.core.databinding.observable.IObservable; 22 import org.eclipse.core.databinding.observable.Realm; 23 import org.eclipse.core.databinding.observable.list.IObservableList; 24 import org.eclipse.core.databinding.observable.map.IObservableMap; 25 import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory; 26 import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables; 27 import org.eclipse.core.databinding.observable.set.IObservableSet; 28 import org.eclipse.core.databinding.observable.value.IObservableValue; 29 import org.eclipse.core.internal.databinding.internal.beans.BeanObservableListDecorator; 30 import org.eclipse.core.internal.databinding.internal.beans.BeanObservableSetDecorator; 31 import org.eclipse.core.internal.databinding.internal.beans.BeanObservableValueDecorator; 32 import org.eclipse.core.internal.databinding.internal.beans.JavaBeanObservableList; 33 import org.eclipse.core.internal.databinding.internal.beans.JavaBeanObservableMap; 34 import org.eclipse.core.internal.databinding.internal.beans.JavaBeanObservableSet; 35 import org.eclipse.core.internal.databinding.internal.beans.JavaBeanObservableValue; 36 37 45 final public class BeansObservables { 46 47 50 public static final boolean DEBUG = true; 51 52 63 public static IObservableValue observeValue(Object bean, String propertyName) { 64 return observeValue(Realm.getDefault(), bean, propertyName); 65 } 66 67 80 public static IObservableValue observeValue(Realm realm, Object bean, 81 String propertyName) { 82 PropertyDescriptor descriptor = getPropertyDescriptor(bean.getClass(), 83 propertyName); 84 return new JavaBeanObservableValue(realm, bean, descriptor, null); 85 } 86 87 100 public static IObservableMap observeMap(IObservableSet domain, 101 Class beanClass, String propertyName) { 102 PropertyDescriptor descriptor = getPropertyDescriptor(beanClass, 103 propertyName); 104 return new JavaBeanObservableMap(domain, descriptor); 105 } 106 107 private static PropertyDescriptor getPropertyDescriptor(Class beanClass, 108 String propertyName) { 109 BeanInfo beanInfo; 110 try { 111 beanInfo = Introspector.getBeanInfo(beanClass); 112 } catch (IntrospectionException e) { 113 return null; 115 } 116 PropertyDescriptor [] propertyDescriptors = beanInfo 117 .getPropertyDescriptors(); 118 for (int i = 0; i < propertyDescriptors.length; i++) { 119 PropertyDescriptor descriptor = propertyDescriptors[i]; 120 if (descriptor.getName().equals(propertyName)) { 121 return descriptor; 122 } 123 } 124 throw new BindingException( 125 "Could not find property with name " + propertyName + " in class " + beanClass); } 127 128 141 public static IObservableMap[] observeMaps(IObservableSet domain, 142 Class beanClass, String [] propertyNames) { 143 IObservableMap[] result = new IObservableMap[propertyNames.length]; 144 for (int i = 0; i < propertyNames.length; i++) { 145 result[i] = observeMap(domain, beanClass, propertyNames[i]); 146 } 147 return result; 148 } 149 150 165 public static IObservableList observeList(Realm realm, Object bean, 166 String propertyName) { 167 return observeList(realm, bean, propertyName, null); 168 } 169 170 193 public static IObservableList observeList(Realm realm, Object bean, 194 String propertyName, Class elementType) { 195 PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean 196 .getClass(), propertyName); 197 elementType = getCollectionElementType(elementType, propertyDescriptor); 198 199 return new JavaBeanObservableList(realm, bean, propertyDescriptor, 200 elementType); 201 } 202 203 216 public static IObservableSet observeSet(Realm realm, Object bean, 217 String propertyName) { 218 return observeSet(realm, bean, propertyName, null); 219 } 220 221 231 public static IObservableFactory valueFactory(final Realm realm, 232 final String propertyName) { 233 return new IObservableFactory() { 234 public IObservable createObservable(Object target) { 235 return observeValue(realm, target, propertyName); 236 } 237 }; 238 } 239 240 251 public static IObservableFactory listFactory(final Realm realm, 252 final String propertyName, final Class elementType) { 253 return new IObservableFactory() { 254 public IObservable createObservable(Object target) { 255 return observeList(realm, target, propertyName, elementType); 256 } 257 }; 258 } 259 260 270 public static IObservableFactory setFactory(final Realm realm, 271 final String propertyName) { 272 return new IObservableFactory() { 273 public IObservable createObservable(Object target) { 274 return observeSet(realm, target, propertyName); 275 } 276 }; 277 } 278 279 294 public static IObservableValue observeDetailValue(Realm realm, 295 IObservableValue master, String propertyName, Class propertyType) { 296 297 IObservableValue value = MasterDetailObservables.detailValue(master, 298 valueFactory(realm, propertyName), propertyType); 299 BeanObservableValueDecorator decorator = new BeanObservableValueDecorator( 300 value, master, getValueTypePropertyDescriptor(master, 301 propertyName)); 302 303 return decorator; 304 } 305 306 321 public static IObservableList observeDetailList(Realm realm, 322 IObservableValue master, String propertyName, Class propertyType) { 323 IObservableList observableList = MasterDetailObservables.detailList( 324 master, listFactory(realm, propertyName, propertyType), 325 propertyType); 326 BeanObservableListDecorator decorator = new BeanObservableListDecorator( 327 observableList, master, getValueTypePropertyDescriptor(master, 328 propertyName)); 329 330 return decorator; 331 } 332 333 348 public static IObservableSet observeDetailSet(Realm realm, 349 IObservableValue master, String propertyName, Class propertyType) { 350 351 IObservableSet observableSet = MasterDetailObservables.detailSet( 352 master, setFactory(realm, propertyName, propertyType), 353 propertyType); 354 BeanObservableSetDecorator decorator = new BeanObservableSetDecorator( 355 observableSet, master, getValueTypePropertyDescriptor(master, 356 propertyName)); 357 358 return decorator; 359 } 360 361 370 public static IObservableSet observeSet(Realm realm, Object bean, 371 String propertyName, Class elementType) { 372 PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean 373 .getClass(), propertyName); 374 elementType = getCollectionElementType(elementType, propertyDescriptor); 375 376 return new JavaBeanObservableSet(realm, bean, propertyDescriptor, 377 elementType); 378 } 379 380 387 public static IObservableFactory setFactory(final Realm realm, 388 final String propertyName, final Class elementType) { 389 return new IObservableFactory() { 390 public IObservable createObservable(Object target) { 391 return observeSet(realm, target, propertyName, elementType); 392 } 393 }; 394 } 395 396 402 private static Class getCollectionElementType(Class elementType, 403 PropertyDescriptor propertyDescriptor) { 404 if (elementType == null) { 405 Class propertyType = propertyDescriptor.getPropertyType(); 406 elementType = propertyType.isArray() ? propertyType 407 .getComponentType() : Object .class; 408 } 409 410 return elementType; 411 } 412 413 418 private static PropertyDescriptor getValueTypePropertyDescriptor( 419 IObservableValue observable, String propertyName) { 420 return (observable.getValueType() != null) ? getPropertyDescriptor( 421 (Class ) observable.getValueType(), propertyName) : null; 422 } 423 } 424 | Popular Tags |