KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > factories > NestedObservableFactory


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.factories;
13
14 import java.beans.BeanInfo JavaDoc;
15 import java.beans.Introspector JavaDoc;
16 import java.beans.PropertyDescriptor JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
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 /**
29  *
30  * TODO Javadoc
31  *
32  * @since 1.0
33  *
34  */

35 public class NestedObservableFactory implements IObservableFactory {
36
37     private final DataBindingContext dataBindingContext;
38
39     /**
40      * @param dataBindingContext
41      * TODO
42      *
43      */

44     public NestedObservableFactory(DataBindingContext dataBindingContext) {
45         this.dataBindingContext = dataBindingContext;
46     }
47
48     public IObservable createObservable(Object JavaDoc 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 JavaDoc o = propertyDescription.getObject();
55             if (o instanceof IObservableValue) {
56                 IObservableValue observableValue = (IObservableValue) o;
57                 Class JavaDoc propertyType = propertyDescription.getPropertyType();
58                 if (propertyType == null) {
59                     throw new BindingException(
60                             "Missing required property type for binding to a property of an IObservableValue."); //$NON-NLS-1$
61
}
62                 Boolean JavaDoc 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."); //$NON-NLS-1$
67
}
68                 Object JavaDoc 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             // else if (o instanceof List) {
77
// return new ListObservableCollection(
78
// (List) o,
79
// propertyDescription.getPropertyType() == null ? Object.class
80
// : propertyDescription.getPropertyType());
81
// }
82
}
83         // else if (description instanceof TreeModelDescription) {
84
// TreeModelDescription treeModelDescription = (TreeModelDescription)
85
// description;
86
// if (treeModelDescription.getRoot() != null) {
87
// if (treeModelDescription.getRoot() instanceof IObservable) {
88
// if (treeModelDescription.getRoot() instanceof IObservableTree)
89
// return (IObservableTree) treeModelDescription
90
// .getRoot();
91
// // Nest the TreeModelDescription's root
92
// return new NestedObservableTree(
93
// bindingContext,
94
// treeModelDescription);
95
// } else if (treeModelDescription.getRoot() instanceof Property) {
96
// // Create an Observable for the
97
// // TreeModelDescription's root first
98
// TreeModelDescription newDescription = new TreeModelDescription(
99
// bindingContext
100
// .createObservable(treeModelDescription
101
// .getRoot()));
102
// Class[] types = treeModelDescription.getTypes();
103
// for (int i = 0; i < types.length; i++) {
104
// String[] props = treeModelDescription
105
// .getChildrenProperties(types[i]);
106
// for (int j = 0; j < props.length; j++)
107
// newDescription.addChildrenProperty(
108
// types[i], props[j]);
109
// }
110
// return bindingContext
111
// .createObservable(newDescription);
112
// }
113
// }
114
// return null;
115
// } else if (description instanceof TableModelDescription) {
116
// TableModelDescription tableModelDescription = (TableModelDescription)
117
// description;
118
// Object master =
119
// tableModelDescription.getCollectionProperty().getObject();
120
// if(master instanceof IObservableValue) {
121
// return new
122
// NestedObservableCellProvider(bindingContext,(IObservableValue)
123
// master,tableModelDescription);
124
// }
125
// }
126
return null;
127     }
128
129     private IObservable createNestedObservable(NestedProperty nestedProperty,
130             DataBindingContext bindingContext) {
131         IObservable lastChildObservable = null;
132         Object JavaDoc targetObject = nestedProperty.getObject();
133         if (nestedProperty.getPrototypeClass() != null) {
134             Class JavaDoc targetClazz = nestedProperty.getPrototypeClass();
135             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(
136                     (String JavaDoc) nestedProperty.getPropertyID(), "."); //$NON-NLS-1$
137
while (tokenizer.hasMoreElements()) {
138                 String JavaDoc nextDesc = (String JavaDoc) tokenizer.nextElement();
139                 try {
140                     BeanInfo JavaDoc beanInfo = Introspector.getBeanInfo(targetClazz);
141                     PropertyDescriptor JavaDoc[] propertyDescriptors = beanInfo
142                             .getPropertyDescriptors();
143                     Class JavaDoc discoveredClazz = null;
144                     for (int i = 0; i < propertyDescriptors.length; i++) {
145                         PropertyDescriptor JavaDoc 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."); //$NON-NLS-1$
156
}
157                 } catch (BindingException be) {
158                     throw be;
159                 } catch (Exception JavaDoc e) {
160                     e.printStackTrace();
161                     throw new BindingException(
162                             "Exeception using prototype class to determine binding types.", e); //$NON-NLS-1$
163
}
164                 lastChildObservable = bindingContext
165                         .createObservable(new Property(targetObject, nextDesc,
166                                 targetClazz, new Boolean JavaDoc(false)));
167                 targetObject = lastChildObservable;
168             }
169
170         } else {
171             String JavaDoc[] properties = (String JavaDoc[]) nestedProperty.getPropertyID();
172             for (int i = 0; i < properties.length; i++) {
173                 String JavaDoc nextDesc = properties[i];
174                 Class JavaDoc clazz = nestedProperty.getTypes()[i];
175                 lastChildObservable = bindingContext
176                         .createObservable(new Property(targetObject, nextDesc,
177                                 clazz, new Boolean JavaDoc(false)));
178                 targetObject = lastChildObservable;
179             }
180         }
181         return lastChildObservable;
182     }
183 }
Popular Tags