1 11 12 package org.eclipse.jface.internal.databinding.provisional.viewers; 13 14 import java.beans.PropertyDescriptor ; 15 16 import org.eclipse.jface.internal.databinding.internal.beans.JavaBeanObservableMultiMapping; 17 import org.eclipse.jface.internal.databinding.internal.viewers.MultiMappingAndListBinding; 18 import org.eclipse.jface.internal.databinding.internal.viewers.MultiMappingAndSetBinding; 19 import org.eclipse.jface.internal.databinding.provisional.BindSpec; 20 import org.eclipse.jface.internal.databinding.provisional.Binding; 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.factories.IBindingFactory; 24 import org.eclipse.jface.internal.databinding.provisional.observable.IObservable; 25 import org.eclipse.jface.internal.databinding.provisional.observable.list.IObservableList; 26 import org.eclipse.jface.internal.databinding.provisional.observable.mapping.IObservableMultiMappingWithDomain; 27 import org.eclipse.jface.internal.databinding.provisional.observable.set.IObservableSet; 28 29 33 public class ViewersBindingFactory implements IBindingFactory { 34 35 public Binding createBinding(DataBindingContext dataBindingContext, 36 IObservable targetObservable, IObservable modelObservable, 37 BindSpec bindSpec) { 38 if (bindSpec == null) 39 bindSpec = new BindSpec(null, null, null, null); 40 if (targetObservable instanceof IObservableList 42 && targetObservable instanceof IObservableCollectionWithLabels) { 43 IObservableMultiMappingWithDomain model = null; 44 if (modelObservable instanceof IObservableList) { 45 47 IObservableList observableList = (IObservableList) modelObservable; 48 Class objectDef; 49 Object elementType = observableList.getElementType(); 50 if (elementType instanceof Class ) { 51 objectDef = (Class ) elementType; 52 } else { 53 throw new UnsupportedOperationException ("Automatic creation of toString() IObservableMultiMappingWithDomain is not supported with non Class element types."); } 55 56 PropertyDescriptor propertyDescriptor = null; 57 try { 58 propertyDescriptor = new PropertyDescriptor ("string", objectDef.getMethod("toString", null), null); } catch (Throwable t) { 60 throw new RuntimeException ("Automatic creation of toString() IObservableMultiMappingWithDomain failed.", t); } 62 63 PropertyDescriptor [] propertyDescriptors = new PropertyDescriptor [1]; 64 propertyDescriptors[0] = propertyDescriptor; 65 model = new JavaBeanObservableMultiMapping(observableList, 66 propertyDescriptors); 67 } else if (modelObservable instanceof IObservableMultiMappingWithDomain) { 68 model = (IObservableMultiMappingWithDomain) modelObservable; 69 } 70 71 if (model != null) { 72 if (model.getDomain() instanceof IObservableList) { 73 IObservableList modelList = (IObservableList) model 74 .getDomain(); 75 IObservableCollectionWithLabels target = (IObservableCollectionWithLabels) targetObservable; 76 77 IObservableList targetList = (IObservableList) targetObservable; 78 dataBindingContext.fillBindSpecDefaults(dataBindingContext, 79 bindSpec, targetList.getElementType(), modelList 80 .getElementType()); 81 82 target.setModelToTargetConverters(bindSpec.getModelToTargetConverters()); 83 84 MultiMappingAndListBinding binding = new MultiMappingAndListBinding( 85 dataBindingContext, targetList, target, modelList, 86 model, bindSpec); 87 return binding; 88 } 89 } 90 throw new BindingException( 91 "incompatible updatables: target is observable list with labels, model is " + modelObservable.getClass().getName()); } else if (targetObservable instanceof IObservableSet 93 && targetObservable instanceof IObservableCollectionWithLabels) { 94 if (modelObservable instanceof IObservableMultiMappingWithDomain) { 95 IObservableMultiMappingWithDomain model = (IObservableMultiMappingWithDomain) modelObservable; 96 if (model.getDomain() instanceof IObservableSet) { 97 IObservableSet modelSet = (IObservableSet) model 98 .getDomain(); 99 IObservableCollectionWithLabels target = (IObservableCollectionWithLabels) targetObservable; 100 IObservableSet targetSet = (IObservableSet) targetObservable; 101 dataBindingContext.fillBindSpecDefaults(dataBindingContext, 102 bindSpec, targetSet.getElementType(), modelSet 103 .getElementType()); 104 MultiMappingAndSetBinding binding = new MultiMappingAndSetBinding( 105 dataBindingContext, targetSet, target, modelSet, 106 model, bindSpec); 107 return binding; 108 } 109 } 110 throw new BindingException( 111 "incompatible updatables: target is observable set with labels, model is " + modelObservable.getClass().getName()); } 113 return null; 114 } 115 116 } 117 | Popular Tags |