1 11 12 package org.eclipse.jface.internal.databinding.provisional.factories; 13 14 import java.beans.BeanInfo ; 15 import java.beans.Introspector ; 16 import java.beans.PropertyDescriptor ; 17 import java.util.StringTokenizer ; 18 19 import org.eclipse.jface.internal.databinding.internal.observable.NestedObservableList; 20 import org.eclipse.jface.internal.databinding.internal.observable.NestedObservableValue; 21 import org.eclipse.jface.internal.databinding.provisional.BindingException; 22 import org.eclipse.jface.internal.databinding.provisional.DataBindingContext; 23 import org.eclipse.jface.internal.databinding.provisional.description.NestedProperty; 24 import org.eclipse.jface.internal.databinding.provisional.description.Property; 25 import org.eclipse.jface.internal.databinding.provisional.observable.IObservable; 26 import org.eclipse.jface.internal.databinding.provisional.observable.value.IObservableValue; 27 28 35 public class NestedObservableFactory implements IObservableFactory { 36 37 private final DataBindingContext dataBindingContext; 38 39 44 public NestedObservableFactory(DataBindingContext dataBindingContext) { 45 this.dataBindingContext = dataBindingContext; 46 } 47 48 public IObservable createObservable(Object description) { 49 if (description instanceof NestedProperty) { 50 return createNestedObservable((NestedProperty) description, 51 dataBindingContext); 52 } else if (description instanceof Property) { 53 Property propertyDescription = (Property) description; 54 Object o = propertyDescription.getObject(); 55 if (o instanceof IObservableValue) { 56 IObservableValue observableValue = (IObservableValue) o; 57 Class propertyType = propertyDescription.getPropertyType(); 58 if (propertyType == null) { 59 throw new BindingException( 60 "Missing required property type for binding to a property of an IObservableValue."); } 62 Boolean isCollectionProperty = propertyDescription 63 .isCollectionProperty(); 64 if (isCollectionProperty == null) { 65 throw new BindingException( 66 "Missing required property collection information for binding to a property of an IObservableValue."); } 68 Object propertyID = propertyDescription.getPropertyID(); 69 if (isCollectionProperty.booleanValue()) { 70 return new NestedObservableList(dataBindingContext, 71 observableValue, propertyID, propertyType); 72 } 73 return new NestedObservableValue(dataBindingContext, 74 observableValue, propertyID, propertyType); 75 } 76 } 83 return null; 127 } 128 129 private IObservable createNestedObservable(NestedProperty nestedProperty, 130 DataBindingContext bindingContext) { 131 IObservable lastChildObservable = null; 132 Object targetObject = nestedProperty.getObject(); 133 if (nestedProperty.getPrototypeClass() != null) { 134 Class targetClazz = nestedProperty.getPrototypeClass(); 135 StringTokenizer tokenizer = new StringTokenizer ( 136 (String ) nestedProperty.getPropertyID(), "."); while (tokenizer.hasMoreElements()) { 138 String nextDesc = (String ) tokenizer.nextElement(); 139 try { 140 BeanInfo beanInfo = Introspector.getBeanInfo(targetClazz); 141 PropertyDescriptor [] propertyDescriptors = beanInfo 142 .getPropertyDescriptors(); 143 Class discoveredClazz = null; 144 for (int i = 0; i < propertyDescriptors.length; i++) { 145 PropertyDescriptor descriptor = propertyDescriptors[i]; 146 if (descriptor.getName().equals(nextDesc)) { 147 discoveredClazz = descriptor.getPropertyType(); 148 break; 149 } 150 } 151 if (discoveredClazz != null) { 152 targetClazz = discoveredClazz; 153 } else { 154 throw new BindingException( 155 "Error using prototype class to determine binding types."); } 157 } catch (BindingException be) { 158 throw be; 159 } catch (Exception e) { 160 e.printStackTrace(); 161 throw new BindingException( 162 "Exeception using prototype class to determine binding types.", e); } 164 lastChildObservable = bindingContext 165 .createObservable(new Property(targetObject, nextDesc, 166 targetClazz, new Boolean (false))); 167 targetObject = lastChildObservable; 168 } 169 170 } else { 171 String [] properties = (String []) nestedProperty.getPropertyID(); 172 for (int i = 0; i < properties.length; i++) { 173 String nextDesc = properties[i]; 174 Class clazz = nestedProperty.getTypes()[i]; 175 lastChildObservable = bindingContext 176 .createObservable(new Property(targetObject, nextDesc, 177 clazz, new Boolean (false))); 178 targetObject = lastChildObservable; 179 } 180 } 181 return lastChildObservable; 182 } 183 } | Popular Tags |