|
Lines 8-24
Link Here
|
| 8 |
* Contributors: |
8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
9 |
* IBM Corporation - initial API and implementation |
| 10 |
* Brad Reynolds - bugs 164268, 171616, 147515 |
10 |
* Brad Reynolds - bugs 164268, 171616, 147515 |
| 11 |
* Matthew Hall - bug 221704, 234686 |
11 |
* Matthew Hall - bugs 221704, 234686, 194734 |
| 12 |
* Thomas Kratz - bug 213787 |
12 |
* Thomas Kratz - bug 213787 |
| 13 |
*******************************************************************************/ |
13 |
*******************************************************************************/ |
| 14 |
package org.eclipse.core.databinding.beans; |
14 |
package org.eclipse.core.databinding.beans; |
| 15 |
|
15 |
|
| 16 |
import java.beans.BeanInfo; |
|
|
| 17 |
import java.beans.IntrospectionException; |
| 18 |
import java.beans.Introspector; |
| 19 |
import java.beans.PropertyDescriptor; |
16 |
import java.beans.PropertyDescriptor; |
| 20 |
|
17 |
|
| 21 |
import org.eclipse.core.databinding.BindingException; |
|
|
| 22 |
import org.eclipse.core.databinding.observable.IObservable; |
18 |
import org.eclipse.core.databinding.observable.IObservable; |
| 23 |
import org.eclipse.core.databinding.observable.Realm; |
19 |
import org.eclipse.core.databinding.observable.Realm; |
| 24 |
import org.eclipse.core.databinding.observable.list.IObservableList; |
20 |
import org.eclipse.core.databinding.observable.list.IObservableList; |
|
Lines 27-41
Link Here
|
| 27 |
import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables; |
23 |
import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables; |
| 28 |
import org.eclipse.core.databinding.observable.set.IObservableSet; |
24 |
import org.eclipse.core.databinding.observable.set.IObservableSet; |
| 29 |
import org.eclipse.core.databinding.observable.value.IObservableValue; |
25 |
import org.eclipse.core.databinding.observable.value.IObservableValue; |
|
|
26 |
import org.eclipse.core.databinding.property.IListProperty; |
| 27 |
import org.eclipse.core.databinding.property.IMapProperty; |
| 28 |
import org.eclipse.core.databinding.property.ISetProperty; |
| 29 |
import org.eclipse.core.databinding.property.IValueProperty; |
| 30 |
import org.eclipse.core.databinding.property.PropertyObservables; |
| 30 |
import org.eclipse.core.internal.databinding.beans.BeanObservableListDecorator; |
31 |
import org.eclipse.core.internal.databinding.beans.BeanObservableListDecorator; |
| 31 |
import org.eclipse.core.internal.databinding.beans.BeanObservableMapDecorator; |
32 |
import org.eclipse.core.internal.databinding.beans.BeanObservableMapDecorator; |
| 32 |
import org.eclipse.core.internal.databinding.beans.BeanObservableSetDecorator; |
33 |
import org.eclipse.core.internal.databinding.beans.BeanObservableSetDecorator; |
| 33 |
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator; |
34 |
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator; |
| 34 |
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableList; |
35 |
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper; |
| 35 |
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableMap; |
|
|
| 36 |
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableSet; |
| 37 |
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableValue; |
| 38 |
import org.eclipse.core.internal.databinding.beans.JavaBeanPropertyObservableMap; |
| 39 |
import org.eclipse.core.runtime.Assert; |
36 |
import org.eclipse.core.runtime.Assert; |
| 40 |
|
37 |
|
| 41 |
/** |
38 |
/** |
|
Lines 83-91
Link Here
|
| 83 |
*/ |
80 |
*/ |
| 84 |
public static IObservableValue observeValue(Realm realm, Object bean, |
81 |
public static IObservableValue observeValue(Realm realm, Object bean, |
| 85 |
String propertyName) { |
82 |
String propertyName) { |
| 86 |
PropertyDescriptor descriptor = getPropertyDescriptor(bean.getClass(), |
83 |
IValueProperty property = BeanProperties.valueProperty(bean.getClass(), |
| 87 |
propertyName); |
84 |
propertyName); |
| 88 |
return new JavaBeanObservableValue(realm, bean, descriptor); |
85 |
PropertyDescriptor propertyDescriptor = ((IBeanProperty) property) |
|
|
86 |
.getPropertyDescriptor(); |
| 87 |
return new BeanObservableValueDecorator(PropertyObservables |
| 88 |
.observeValue(realm, bean, property), propertyDescriptor); |
| 89 |
} |
89 |
} |
| 90 |
|
90 |
|
| 91 |
/** |
91 |
/** |
|
Lines 103-111
Link Here
|
| 103 |
*/ |
103 |
*/ |
| 104 |
public static IObservableMap observeMap(IObservableSet domain, |
104 |
public static IObservableMap observeMap(IObservableSet domain, |
| 105 |
Class beanClass, String propertyName) { |
105 |
Class beanClass, String propertyName) { |
| 106 |
PropertyDescriptor descriptor = getPropertyDescriptor(beanClass, |
106 |
IValueProperty property = BeanProperties.valueProperty(beanClass, |
| 107 |
propertyName); |
107 |
propertyName); |
| 108 |
return new JavaBeanObservableMap(domain, descriptor); |
108 |
PropertyDescriptor propertyDescriptor = ((IBeanProperty) property) |
|
|
109 |
.getPropertyDescriptor(); |
| 110 |
return new BeanObservableMapDecorator(PropertyObservables.observeMap( |
| 111 |
domain, property), propertyDescriptor); |
| 109 |
} |
112 |
} |
| 110 |
|
113 |
|
| 111 |
/** |
114 |
/** |
|
Lines 124-132
Link Here
|
| 124 |
*/ |
127 |
*/ |
| 125 |
public static IObservableMap observeMap(Realm realm, Object bean, |
128 |
public static IObservableMap observeMap(Realm realm, Object bean, |
| 126 |
String propertyName) { |
129 |
String propertyName) { |
| 127 |
PropertyDescriptor descriptor = getPropertyDescriptor(bean.getClass(), |
130 |
IMapProperty property = BeanProperties.mapProperty(bean.getClass(), |
| 128 |
propertyName); |
131 |
propertyName); |
| 129 |
return new JavaBeanPropertyObservableMap(realm, bean, descriptor); |
132 |
PropertyDescriptor propertyDescriptor = ((IBeanProperty) property) |
|
|
133 |
.getPropertyDescriptor(); |
| 134 |
return new BeanObservableMapDecorator(PropertyObservables.observeMap( |
| 135 |
realm, bean, property), propertyDescriptor); |
| 130 |
} |
136 |
} |
| 131 |
|
137 |
|
| 132 |
/** |
138 |
/** |
|
Lines 145-171
Link Here
|
| 145 |
return observeMap(Realm.getDefault(), bean, propertyName); |
151 |
return observeMap(Realm.getDefault(), bean, propertyName); |
| 146 |
} |
152 |
} |
| 147 |
|
153 |
|
| 148 |
/*package*/ static PropertyDescriptor getPropertyDescriptor(Class beanClass, |
|
|
| 149 |
String propertyName) { |
| 150 |
BeanInfo beanInfo; |
| 151 |
try { |
| 152 |
beanInfo = Introspector.getBeanInfo(beanClass); |
| 153 |
} catch (IntrospectionException e) { |
| 154 |
// cannot introspect, give up |
| 155 |
return null; |
| 156 |
} |
| 157 |
PropertyDescriptor[] propertyDescriptors = beanInfo |
| 158 |
.getPropertyDescriptors(); |
| 159 |
for (int i = 0; i < propertyDescriptors.length; i++) { |
| 160 |
PropertyDescriptor descriptor = propertyDescriptors[i]; |
| 161 |
if (descriptor.getName().equals(propertyName)) { |
| 162 |
return descriptor; |
| 163 |
} |
| 164 |
} |
| 165 |
throw new BindingException( |
| 166 |
"Could not find property with name " + propertyName + " in class " + beanClass); //$NON-NLS-1$ //$NON-NLS-2$ |
| 167 |
} |
| 168 |
|
| 169 |
/** |
154 |
/** |
| 170 |
* Returns an array of observable maps in the default realm tracking the |
155 |
* Returns an array of observable maps in the default realm tracking the |
| 171 |
* current values of the named propertys for the beans in the given set. |
156 |
* current values of the named propertys for the beans in the given set. |
|
Lines 251-262
Link Here
|
| 251 |
*/ |
236 |
*/ |
| 252 |
public static IObservableList observeList(Realm realm, Object bean, |
237 |
public static IObservableList observeList(Realm realm, Object bean, |
| 253 |
String propertyName, Class elementType) { |
238 |
String propertyName, Class elementType) { |
| 254 |
PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean |
239 |
IListProperty property = BeanProperties.listProperty(bean.getClass(), |
| 255 |
.getClass(), propertyName); |
240 |
propertyName, elementType); |
| 256 |
elementType = getCollectionElementType(elementType, propertyDescriptor); |
241 |
PropertyDescriptor propertyDescriptor = ((IBeanProperty) property) |
|
|
242 |
.getPropertyDescriptor(); |
| 257 |
|
243 |
|
| 258 |
return new JavaBeanObservableList(realm, bean, propertyDescriptor, |
244 |
return new BeanObservableListDecorator(PropertyObservables.observeList( |
| 259 |
elementType); |
245 |
realm, bean, property), propertyDescriptor); |
| 260 |
} |
246 |
} |
| 261 |
|
247 |
|
| 262 |
/** |
248 |
/** |
|
Lines 439-449
Link Here
|
| 439 |
|
425 |
|
| 440 |
IObservableValue value = MasterDetailObservables.detailValue(master, |
426 |
IObservableValue value = MasterDetailObservables.detailValue(master, |
| 441 |
valueFactory(realm, propertyName), propertyType); |
427 |
valueFactory(realm, propertyName), propertyType); |
| 442 |
BeanObservableValueDecorator decorator = new BeanObservableValueDecorator( |
428 |
return new BeanObservableValueDecorator(value, BeanPropertyHelper |
| 443 |
value, master, getValueTypePropertyDescriptor(master, |
429 |
.getValueTypePropertyDescriptor(master, propertyName)); |
| 444 |
propertyName)); |
|
|
| 445 |
|
| 446 |
return decorator; |
| 447 |
} |
430 |
} |
| 448 |
|
431 |
|
| 449 |
/** |
432 |
/** |
|
Lines 492-506
Link Here
|
| 492 |
* @since 1.1 |
475 |
* @since 1.1 |
| 493 |
*/ |
476 |
*/ |
| 494 |
public static IObservableValue observeDetailValue(Realm realm, |
477 |
public static IObservableValue observeDetailValue(Realm realm, |
| 495 |
IObservableValue master, Class masterType, String propertyName, Class propertyType) { |
478 |
IObservableValue master, Class masterType, String propertyName, |
|
|
479 |
Class propertyType) { |
| 496 |
Assert.isNotNull(masterType, "masterType cannot be null"); //$NON-NLS-1$ |
480 |
Assert.isNotNull(masterType, "masterType cannot be null"); //$NON-NLS-1$ |
| 497 |
IObservableValue value = MasterDetailObservables.detailValue(master, |
481 |
IObservableValue value = MasterDetailObservables.detailValue(master, |
| 498 |
valueFactory(realm, propertyName), propertyType); |
482 |
valueFactory(realm, propertyName), propertyType); |
| 499 |
BeanObservableValueDecorator decorator = new BeanObservableValueDecorator( |
483 |
return new BeanObservableValueDecorator(value, BeanPropertyHelper |
| 500 |
value, master, getPropertyDescriptor(masterType, |
484 |
.getPropertyDescriptor(masterType, propertyName)); |
| 501 |
propertyName)); |
|
|
| 502 |
|
| 503 |
return decorator; |
| 504 |
} |
485 |
} |
| 505 |
|
486 |
|
| 506 |
/** |
487 |
/** |
|
Lines 551-561
Link Here
|
| 551 |
IObservableList observableList = MasterDetailObservables.detailList( |
532 |
IObservableList observableList = MasterDetailObservables.detailList( |
| 552 |
master, listFactory(realm, propertyName, propertyType), |
533 |
master, listFactory(realm, propertyName, propertyType), |
| 553 |
propertyType); |
534 |
propertyType); |
| 554 |
BeanObservableListDecorator decorator = new BeanObservableListDecorator( |
535 |
return new BeanObservableListDecorator(observableList, |
| 555 |
observableList, master, getValueTypePropertyDescriptor(master, |
536 |
BeanPropertyHelper.getValueTypePropertyDescriptor(master, |
| 556 |
propertyName)); |
537 |
propertyName)); |
| 557 |
|
|
|
| 558 |
return decorator; |
| 559 |
} |
538 |
} |
| 560 |
|
539 |
|
| 561 |
/** |
540 |
/** |
|
Lines 599-609
Link Here
|
| 599 |
IObservableSet observableSet = MasterDetailObservables.detailSet( |
578 |
IObservableSet observableSet = MasterDetailObservables.detailSet( |
| 600 |
master, setFactory(realm, propertyName, propertyType), |
579 |
master, setFactory(realm, propertyName, propertyType), |
| 601 |
propertyType); |
580 |
propertyType); |
| 602 |
BeanObservableSetDecorator decorator = new BeanObservableSetDecorator( |
581 |
return new BeanObservableSetDecorator(observableSet, BeanPropertyHelper |
| 603 |
observableSet, master, getValueTypePropertyDescriptor(master, |
582 |
.getValueTypePropertyDescriptor(master, propertyName)); |
| 604 |
propertyName)); |
|
|
| 605 |
|
| 606 |
return decorator; |
| 607 |
} |
583 |
} |
| 608 |
|
584 |
|
| 609 |
/** |
585 |
/** |
|
Lines 642-651
Link Here
|
| 642 |
IObservableValue master, String propertyName) { |
618 |
IObservableValue master, String propertyName) { |
| 643 |
IObservableMap observableMap = MasterDetailObservables.detailMap( |
619 |
IObservableMap observableMap = MasterDetailObservables.detailMap( |
| 644 |
master, mapPropertyFactory(realm, propertyName)); |
620 |
master, mapPropertyFactory(realm, propertyName)); |
| 645 |
BeanObservableMapDecorator decorator = new BeanObservableMapDecorator( |
621 |
return new BeanObservableMapDecorator(observableMap, BeanPropertyHelper |
| 646 |
observableMap, master, getValueTypePropertyDescriptor(master, |
622 |
.getValueTypePropertyDescriptor(master, propertyName)); |
| 647 |
propertyName)); |
|
|
| 648 |
return decorator; |
| 649 |
} |
623 |
} |
| 650 |
|
624 |
|
| 651 |
/** |
625 |
/** |
|
Lines 688-699
Link Here
|
| 688 |
*/ |
662 |
*/ |
| 689 |
public static IObservableSet observeSet(Realm realm, Object bean, |
663 |
public static IObservableSet observeSet(Realm realm, Object bean, |
| 690 |
String propertyName, Class elementType) { |
664 |
String propertyName, Class elementType) { |
| 691 |
PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean |
665 |
ISetProperty property = BeanProperties.setProperty(bean.getClass(), |
| 692 |
.getClass(), propertyName); |
666 |
propertyName, elementType); |
| 693 |
elementType = getCollectionElementType(elementType, propertyDescriptor); |
667 |
PropertyDescriptor propertyDescriptor = ((IBeanProperty) property) |
|
|
668 |
.getPropertyDescriptor(); |
| 694 |
|
669 |
|
| 695 |
return new JavaBeanObservableSet(realm, bean, propertyDescriptor, |
670 |
return new BeanObservableSetDecorator(PropertyObservables.observeSet( |
| 696 |
elementType); |
671 |
realm, bean, property), propertyDescriptor); |
| 697 |
} |
672 |
} |
| 698 |
|
673 |
|
| 699 |
/** |
674 |
/** |
|
Lines 828-859
Link Here
|
| 828 |
public static IObservableFactory mapPropertyFactory(String propertyName) { |
803 |
public static IObservableFactory mapPropertyFactory(String propertyName) { |
| 829 |
return mapPropertyFactory(Realm.getDefault(), propertyName); |
804 |
return mapPropertyFactory(Realm.getDefault(), propertyName); |
| 830 |
} |
805 |
} |
| 831 |
|
|
|
| 832 |
/** |
| 833 |
* @param elementType |
| 834 |
* can be <code>null</code> |
| 835 |
* @param propertyDescriptor |
| 836 |
* @return type of the items in a collection/array property |
| 837 |
*/ |
| 838 |
/*package*/ static Class getCollectionElementType(Class elementType, |
| 839 |
PropertyDescriptor propertyDescriptor) { |
| 840 |
if (elementType == null) { |
| 841 |
Class propertyType = propertyDescriptor.getPropertyType(); |
| 842 |
elementType = propertyType.isArray() ? propertyType |
| 843 |
.getComponentType() : Object.class; |
| 844 |
} |
| 845 |
|
| 846 |
return elementType; |
| 847 |
} |
| 848 |
|
| 849 |
/** |
| 850 |
* @param observable |
| 851 |
* @param propertyName |
| 852 |
* @return property descriptor or <code>null</code> |
| 853 |
*/ |
| 854 |
/* package*/ static PropertyDescriptor getValueTypePropertyDescriptor( |
| 855 |
IObservableValue observable, String propertyName) { |
| 856 |
return (observable.getValueType() != null) ? getPropertyDescriptor( |
| 857 |
(Class) observable.getValueType(), propertyName) : null; |
| 858 |
} |
| 859 |
} |
806 |
} |