1 11 package org.eclipse.jface.internal.databinding.provisional.beans; 12 13 import java.beans.BeanInfo ; 14 import java.beans.IntrospectionException ; 15 import java.beans.Introspector ; 16 import java.beans.PropertyDescriptor ; 17 import java.util.Collection ; 18 19 import org.eclipse.jface.internal.databinding.internal.beans.JavaBeanObservableList; 20 import org.eclipse.jface.internal.databinding.internal.beans.JavaBeanObservableMultiMapping; 21 import org.eclipse.jface.internal.databinding.internal.beans.JavaBeanObservableValue; 22 import org.eclipse.jface.internal.databinding.provisional.BindingException; 23 import org.eclipse.jface.internal.databinding.provisional.DataBindingContext; 24 import org.eclipse.jface.internal.databinding.provisional.description.Property; 25 import org.eclipse.jface.internal.databinding.provisional.description.TableModelDescription; 26 import org.eclipse.jface.internal.databinding.provisional.factories.IObservableFactory; 27 import org.eclipse.jface.internal.databinding.provisional.observable.IObservable; 28 import org.eclipse.jface.internal.databinding.provisional.observable.list.IObservableList; 29 30 50 final public class BeanObservableFactory implements IObservableFactory { 51 52 private final DataBindingContext dataBindingContext; 53 54 private final Class [] includedTypes; 55 56 private final Class [] excludedTypes; 57 58 67 public BeanObservableFactory(DataBindingContext dataBindingContext, 68 Class [] includedTypes, Class [] excludedTypes) { 69 this.dataBindingContext = dataBindingContext; 70 this.includedTypes = includedTypes; 71 this.excludedTypes = excludedTypes; 72 } 73 74 public IObservable createObservable(Object description) { 75 if (description instanceof Property) { 76 Property property = (Property) description; 77 if (property.getObject() != null) { 78 Object object = property.getObject(); 79 Class objectClass = object.getClass(); 80 if (includedTypes != null) { 81 boolean included = false; 82 for (int i = 0; i < includedTypes.length; i++) { 83 if (includedTypes[i].isAssignableFrom(objectClass)) { 84 included = true; 85 break; 86 } 87 } 88 if (!included) { 89 return null; 92 } 93 } 94 if (excludedTypes != null) { 95 for (int i = 0; i < excludedTypes.length; i++) { 96 if (excludedTypes[i].isAssignableFrom(objectClass)) { 97 return null; 100 } 101 } 102 } 103 BeanInfo beanInfo; 104 try { 105 beanInfo = Introspector.getBeanInfo(objectClass); 106 } catch (IntrospectionException e) { 107 return null; 109 } 110 PropertyDescriptor [] propertyDescriptors = beanInfo 111 .getPropertyDescriptors(); 112 for (int i = 0; i < propertyDescriptors.length; i++) { 113 PropertyDescriptor descriptor = propertyDescriptors[i]; 114 if (descriptor.getName().equals(property.getPropertyID())) { 115 if (descriptor.getPropertyType().isArray() 116 || Collection .class.isAssignableFrom(descriptor 117 .getPropertyType())) { 118 Class elementType = descriptor.getPropertyType() 125 .isArray() ? descriptor.getPropertyType() 126 .getComponentType() : property 127 .getPropertyType(); 128 if (elementType == null) { 129 elementType = Object .class; 137 } 138 return new JavaBeanObservableList(object, 139 descriptor, elementType); 140 } 141 return new JavaBeanObservableValue(object, descriptor); 142 } 143 } 144 } 145 } else if (description instanceof TableModelDescription) { 156 TableModelDescription tableModelDescription = (TableModelDescription) description; 157 IObservable collectionObservable = tableModelDescription.getObservableList(); 158 if (collectionObservable == null) { 159 collectionObservable = dataBindingContext 160 .createObservable(tableModelDescription 161 .getCollectionProperty()); 162 } 163 if (collectionObservable == null) { 164 return null; 165 } 166 IObservableList readableList; 167 if (collectionObservable instanceof IObservableList) { 168 readableList = (IObservableList) collectionObservable; 169 } else { 170 throw new BindingException( 171 "collection inside a TableModelDescription needs to be IObservableList"); } 173 Object [] columnIDs = tableModelDescription.getColumnIDs(); 174 PropertyDescriptor [] propertyDescriptors = new PropertyDescriptor [columnIDs.length]; 175 Class elementType = (Class ) readableList.getElementType(); 176 for (int i = 0; i < columnIDs.length; i++) { 177 propertyDescriptors[i] = getPropertyDescriptor(elementType, 178 (String ) columnIDs[i]); 179 } 180 return new JavaBeanObservableMultiMapping(readableList, 181 propertyDescriptors); 182 } 183 return null; 184 } 185 186 private PropertyDescriptor getPropertyDescriptor(Class elementType, 187 String propertyName) { 188 BeanInfo beanInfo; 189 try { 190 beanInfo = Introspector.getBeanInfo(elementType); 191 } catch (IntrospectionException ex) { 192 throw new BindingException("Cannot introspect " + elementType, ex); } 194 PropertyDescriptor [] propertyDescriptors = beanInfo 195 .getPropertyDescriptors(); 196 for (int i = 0; i < propertyDescriptors.length; i++) { 197 if (propertyDescriptors[i].getName().equals(propertyName)) { 198 return propertyDescriptors[i]; 199 } 200 } 201 return null; 202 } 203 } 204 | Popular Tags |