KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > viewers > ViewersBindingFactory


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.jface.internal.databinding.provisional.viewers;
13
14 import java.beans.PropertyDescriptor JavaDoc;
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 /**
30  * @since 3.2
31  *
32  */

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         // List-based
41
if (targetObservable instanceof IObservableList
42                 && targetObservable instanceof IObservableCollectionWithLabels) {
43             IObservableMultiMappingWithDomain model = null;
44             if (modelObservable instanceof IObservableList) {
45                 //FIXME Needs Test Case
46

47                 IObservableList observableList = (IObservableList) modelObservable;
48                 Class JavaDoc objectDef;
49                 Object JavaDoc elementType = observableList.getElementType();
50                 if (elementType instanceof Class JavaDoc) {
51                     objectDef = (Class JavaDoc) elementType;
52                 } else {
53                     throw new UnsupportedOperationException JavaDoc("Automatic creation of toString() IObservableMultiMappingWithDomain is not supported with non Class element types."); //$NON-NLS-1$
54
}
55                 
56                 PropertyDescriptor JavaDoc propertyDescriptor = null;
57                 try {
58                     propertyDescriptor = new PropertyDescriptor JavaDoc("string", objectDef.getMethod("toString", null), null); //$NON-NLS-1$ //$NON-NLS-2$
59
} catch (Throwable JavaDoc t) {
60                     throw new RuntimeException JavaDoc("Automatic creation of toString() IObservableMultiMappingWithDomain failed.", t); //$NON-NLS-1$
61
}
62
63                 PropertyDescriptor JavaDoc[] propertyDescriptors = new PropertyDescriptor JavaDoc[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()); //$NON-NLS-1$
92
} 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()); //$NON-NLS-1$
112
}
113         return null;
114     }
115
116 }
117
Popular Tags