|
Lines 10-23
Link Here
|
| 10 |
* Contributors: |
10 |
* Contributors: |
| 11 |
* IBM - Initial API and implementation |
11 |
* IBM - Initial API and implementation |
| 12 |
* Trevor S. Kaufman - Bug 215131 - added mapFactory |
12 |
* Trevor S. Kaufman - Bug 215131 - added mapFactory |
| 13 |
* |
13 |
* Tom Schindl<tom.schindl@bestsolution.at> |
| 14 |
* </copyright> |
14 |
* </copyright> |
| 15 |
* |
15 |
* |
| 16 |
* $Id: EMFObservables.java,v 1.3 2008/04/22 13:36:00 emerks Exp $ |
16 |
* $Id: EMFObservables.java,v 1.3 2008/04/22 13:36:00 emerks Exp $ |
| 17 |
*/ |
17 |
*/ |
| 18 |
package org.eclipse.emf.databinding; |
18 |
package org.eclipse.emf.databinding; |
| 19 |
|
19 |
|
| 20 |
|
|
|
| 21 |
import org.eclipse.core.databinding.observable.IObservable; |
20 |
import org.eclipse.core.databinding.observable.IObservable; |
| 22 |
import org.eclipse.core.databinding.observable.Realm; |
21 |
import org.eclipse.core.databinding.observable.Realm; |
| 23 |
import org.eclipse.core.databinding.observable.list.IObservableList; |
22 |
import org.eclipse.core.databinding.observable.list.IObservableList; |
|
Lines 26-194
Link Here
|
| 26 |
import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables; |
25 |
import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables; |
| 27 |
import org.eclipse.core.databinding.observable.set.IObservableSet; |
26 |
import org.eclipse.core.databinding.observable.set.IObservableSet; |
| 28 |
import org.eclipse.core.databinding.observable.value.IObservableValue; |
27 |
import org.eclipse.core.databinding.observable.value.IObservableValue; |
|
|
28 |
import org.eclipse.emf.common.notify.NotifyingList; |
| 29 |
import org.eclipse.emf.databinding.internal.EWritableList; |
| 29 |
import org.eclipse.emf.ecore.EObject; |
30 |
import org.eclipse.emf.ecore.EObject; |
| 30 |
import org.eclipse.emf.ecore.EStructuralFeature; |
31 |
import org.eclipse.emf.ecore.EStructuralFeature; |
|
|
32 |
import org.eclipse.emf.ecore.resource.Resource; |
| 31 |
|
33 |
|
| 32 |
/** |
34 |
/** |
| 33 |
* PROVISIONAL |
35 |
* <p> |
| 34 |
* This API is subject to arbitrary change, including renaming or removal. |
36 |
* <b>PROVISIONAL This API is subject to arbitrary change, including renaming or |
|
|
37 |
* removal.</b> |
| 38 |
* </p> |
| 39 |
* A factory for creating observable objects of Java objects that conform to the |
| 40 |
* <a href="http://java.sun.com/products/javabeans/docs/spec.html">JavaBean |
| 41 |
* specification</a> for bound properties. |
| 42 |
* |
| 43 |
* @since 1.0 |
| 35 |
*/ |
44 |
*/ |
| 36 |
public class EMFObservables |
45 |
public class EMFObservables { |
| 37 |
{ |
46 |
/** |
| 38 |
/** |
47 |
* |
| 39 |
* Returns an observable value for the given feature of the object. |
48 |
*/ |
| 40 |
* @param eObject the object to observe. |
49 |
public static final boolean DEBUG = false; |
| 41 |
* @param eStructuralFeature the feature of the object to observe. |
50 |
|
| 42 |
* @return an observable value for the given feature of the object. |
51 |
/** |
| 43 |
*/ |
52 |
* Returns an observable value for the given feature of the object. |
| 44 |
public static IObservableValue observeValue(EObject eObject, EStructuralFeature eStructuralFeature) |
53 |
* |
| 45 |
{ |
54 |
* @param eObject |
| 46 |
return new EObjectObservableValue(eObject, eStructuralFeature); |
55 |
* the object to observe. |
| 47 |
} |
56 |
* @param eStructuralFeature |
| 48 |
|
57 |
* the feature of the object to observe. |
| 49 |
/** |
58 |
* @return an observable value for the given feature of the object. |
| 50 |
* Returns an observable value for the given feature of the object. |
59 |
*/ |
| 51 |
* @param realm the realm in which to observe. |
60 |
public static IObservableValue observeValue(EObject eObject, |
| 52 |
* @param eObject the object to observe. |
61 |
EStructuralFeature eStructuralFeature) { |
| 53 |
* @param eStructuralFeature the feature of the object to observe. |
62 |
return new EObjectObservableValue(eObject, eStructuralFeature); |
| 54 |
* @return an observable value for the given feature of the object. |
63 |
} |
| 55 |
*/ |
64 |
|
| 56 |
public static IObservableValue observeValue(Realm realm, EObject eObject, EStructuralFeature eStructuralFeature) |
65 |
/** |
| 57 |
{ |
66 |
* Returns an observable value for the given feature of the object. |
| 58 |
return new EObjectObservableValue(realm, eObject, eStructuralFeature); |
67 |
* |
| 59 |
} |
68 |
* @param realm |
| 60 |
|
69 |
* the realm in which to observe. |
| 61 |
/** |
70 |
* @param eObject |
| 62 |
* Returns an observable list for the given multi-valued feature of the object. |
71 |
* the object to observe. |
| 63 |
* @param eObject the object to observe. |
72 |
* @param eStructuralFeature |
| 64 |
* @param eStructuralFeature the feature of the object to observe. |
73 |
* the feature of the object to observe. |
| 65 |
* @return an observable list for the given multi-valued feature of the object. |
74 |
* @return an observable value for the given feature of the object. |
| 66 |
*/ |
75 |
*/ |
| 67 |
public static IObservableList observeList(EObject eObject, EStructuralFeature eStructuralFeature) |
76 |
public static IObservableValue observeValue(Realm realm, EObject eObject, |
| 68 |
{ |
77 |
EStructuralFeature eStructuralFeature) { |
| 69 |
return new EObjectObservableList(eObject, eStructuralFeature); |
78 |
return new EObjectObservableValue(realm, eObject, eStructuralFeature); |
| 70 |
} |
79 |
} |
| 71 |
|
80 |
|
| 72 |
/** |
81 |
/** |
| 73 |
* Returns an observable list for the given multi-valued feature of the object. |
82 |
* Returns an observable list for the given multi-valued feature of the |
| 74 |
* @param realm the realm in which to observe. |
83 |
* object. |
| 75 |
* @param eObject the object to observe. |
84 |
* |
| 76 |
* @param eStructuralFeature the feature of the object to observe. |
85 |
* @param eObject |
| 77 |
* @return an observable list for the given multi-valued feature of the object. |
86 |
* the object to observe. |
| 78 |
*/ |
87 |
* @param eStructuralFeature |
| 79 |
public static IObservableList observeList(Realm realm, EObject eObject, EStructuralFeature eStructuralFeature) |
88 |
* the feature of the object to observe. |
| 80 |
{ |
89 |
* @return an observable list for the given multi-valued feature of the |
| 81 |
return new EObjectObservableList(realm, eObject, eStructuralFeature); |
90 |
* object. |
| 82 |
} |
91 |
*/ |
| 83 |
|
92 |
public static IObservableList observeList(EObject eObject, |
| 84 |
/** |
93 |
EStructuralFeature eStructuralFeature) { |
| 85 |
* Returns an observable map in the default realm |
94 |
return new EObjectObservableList(eObject, eStructuralFeature); |
| 86 |
* tracking the current value of the given feature for each object in the given set. |
95 |
} |
| 87 |
* @param objects the objects to track. |
96 |
|
| 88 |
* @param eStructuralFeature the feature for which to track the value. |
97 |
/** |
| 89 |
* @return an observable map tracking the current value of the given feature for each object in the given set. |
98 |
* Returns an observable list for the given multi-valued feature of the |
| 90 |
*/ |
99 |
* object. |
| 91 |
public static IObservableMap observeMap(IObservableSet objects, EStructuralFeature eStructuralFeature) |
100 |
* |
| 92 |
{ |
101 |
* @param realm |
| 93 |
return new EObjectObservableMap(objects, eStructuralFeature); |
102 |
* the realm in which to observe. |
| 94 |
} |
103 |
* @param eObject |
| 95 |
|
104 |
* the object to observe. |
| 96 |
/** |
105 |
* @param eStructuralFeature |
| 97 |
* Returns an array of observable maps in the default realm |
106 |
* the feature of the object to observe. |
| 98 |
* tracking the current value of the given features for each object in the given set. |
107 |
* @return an observable list for the given multi-valued feature of the |
| 99 |
* @param objects the objects to track. |
108 |
* object. |
| 100 |
* @param eStructuralFeatures the features for which to track the value. |
109 |
*/ |
| 101 |
* @return an array of observable maps tracking the current value of the given features for each object in the given set. |
110 |
public static IObservableList observeList(Realm realm, EObject eObject, |
| 102 |
*/ |
111 |
EStructuralFeature eStructuralFeature) { |
| 103 |
public static IObservableMap[] observeMaps(IObservableSet objects, EStructuralFeature[] eStructuralFeatures) |
112 |
return new EObjectObservableList(realm, eObject, eStructuralFeature); |
| 104 |
{ |
113 |
} |
| 105 |
IObservableMap[] result = new IObservableMap [eStructuralFeatures.length]; |
114 |
|
| 106 |
for (int i = 0; i < eStructuralFeatures.length; i++) |
115 |
/** |
| 107 |
{ |
116 |
* Returns an observable map in the default realm tracking the current value |
| 108 |
result[i] = observeMap(objects, eStructuralFeatures[i]); |
117 |
* of the given feature for each object in the given set. |
| 109 |
} |
118 |
* |
| 110 |
return result; |
119 |
* @param objects |
| 111 |
} |
120 |
* the objects to track. |
| 112 |
|
121 |
* @param eStructuralFeature |
| 113 |
/** |
122 |
* the feature for which to track the value. |
| 114 |
* Returns an observable value that tracks the current value of the feature of the current value of the master observable value. |
123 |
* @return an observable map tracking the current value of the given feature |
| 115 |
* @param realm the realm in which to observe. |
124 |
* for each object in the given set. |
| 116 |
* @param value the master observable value. |
125 |
*/ |
| 117 |
* @param eStructuralFeature the feature for which to track the value. |
126 |
public static IObservableMap observeMap(IObservableSet objects, |
| 118 |
* @return an observable value that tracks the current value of the named property for the current value of the master observable value |
127 |
EStructuralFeature eStructuralFeature) { |
| 119 |
* @see MasterDetailObservables#detailValue(IObservableValue, IObservableFactory, Object) |
128 |
return new EObjectObservableMap(objects, eStructuralFeature); |
| 120 |
*/ |
129 |
} |
| 121 |
public static IObservableValue observeDetailValue(Realm realm, IObservableValue value, EStructuralFeature eStructuralFeature) |
130 |
|
| 122 |
{ |
131 |
/** |
| 123 |
return MasterDetailObservables.detailValue(value, valueFactory(realm, eStructuralFeature), eStructuralFeature); |
132 |
* Returns an array of observable maps in the default realm tracking the |
| 124 |
} |
133 |
* current value of the given features for each object in the given set. |
| 125 |
|
134 |
* |
| 126 |
/** |
135 |
* @param objects |
| 127 |
* Returns a factory for creating observable values |
136 |
* the objects to track. |
| 128 |
* tracking the value of the given feature of a particular {@link EObject object}. |
137 |
* @param eStructuralFeatures |
| 129 |
* @param realm the realm in which to observe. |
138 |
* the features for which to track the value. |
| 130 |
* @param eStructuralFeature the feature for which to track the value. |
139 |
* @return an array of observable maps tracking the current value of the |
| 131 |
* @return an observable factory. |
140 |
* given features for each object in the given set. |
| 132 |
*/ |
141 |
*/ |
| 133 |
public static IObservableFactory valueFactory(final Realm realm, final EStructuralFeature eStructuralFeature) |
142 |
public static IObservableMap[] observeMaps(IObservableSet objects, |
| 134 |
{ |
143 |
EStructuralFeature[] eStructuralFeatures) { |
| 135 |
return |
144 |
IObservableMap[] result = new IObservableMap[eStructuralFeatures.length]; |
| 136 |
new IObservableFactory() |
145 |
for (int i = 0; i < eStructuralFeatures.length; i++) { |
| 137 |
{ |
146 |
result[i] = observeMap(objects, eStructuralFeatures[i]); |
| 138 |
public IObservable createObservable(Object target) |
147 |
} |
| 139 |
{ |
148 |
return result; |
| 140 |
return observeValue(realm, (EObject)target, eStructuralFeature); |
149 |
} |
| 141 |
} |
150 |
|
| 142 |
}; |
151 |
/** |
| 143 |
} |
152 |
* Returns an observable value that tracks the current value of the feature |
| 144 |
|
153 |
* of the current value of the master observable value. |
| 145 |
/** |
154 |
* |
| 146 |
* Returns an observable list that tracks the current value of the feature of the current value of the master observable value. |
155 |
* @param realm |
| 147 |
* @param realm the realm in which to observe. |
156 |
* the realm in which to observe. |
| 148 |
* @param value the master observable value. |
157 |
* @param value |
| 149 |
* @param eStructuralFeature the feature for which to track the value. |
158 |
* the master observable value. |
| 150 |
* @return an observable value that tracks the current value of the named property for the current value of the master observable value |
159 |
* @param eStructuralFeature |
| 151 |
* @see MasterDetailObservables#detailList(IObservableValue, IObservableFactory, Object) |
160 |
* the feature for which to track the value. |
| 152 |
*/ |
161 |
* @return an observable value that tracks the current value of the named |
| 153 |
public static IObservableList observeDetailList(Realm realm, IObservableValue value, EStructuralFeature eStructuralFeature) |
162 |
* property for the current value of the master observable value |
| 154 |
{ |
163 |
* @see MasterDetailObservables#detailValue(IObservableValue, |
| 155 |
return MasterDetailObservables.detailList(value, listFactory(realm, eStructuralFeature), eStructuralFeature); |
164 |
* IObservableFactory, Object) |
| 156 |
} |
165 |
*/ |
| 157 |
|
166 |
public static IObservableValue observeDetailValue(Realm realm, |
| 158 |
/** |
167 |
IObservableValue value, EStructuralFeature eStructuralFeature) { |
| 159 |
* Returns a factory for creating observable lists |
168 |
return MasterDetailObservables.detailValue(value, valueFactory(realm, |
| 160 |
* tracking the value of the given feature of a particular {@link EObject object}. |
169 |
eStructuralFeature), eStructuralFeature); |
| 161 |
* @param realm the realm in which to observe. |
170 |
} |
| 162 |
* @param eStructuralFeature the feature for which to track the value. |
171 |
|
| 163 |
* @return an observable factory. |
172 |
/** |
| 164 |
*/ |
173 |
* Returns a factory for creating observable values tracking the value of |
| 165 |
public static IObservableFactory listFactory(final Realm realm, final EStructuralFeature eStructuralFeature) |
174 |
* the given feature of a particular {@link EObject object}. |
| 166 |
{ |
175 |
* |
| 167 |
return |
176 |
* @param realm |
| 168 |
new IObservableFactory() |
177 |
* the realm in which to observe. |
| 169 |
{ |
178 |
* @param eStructuralFeature |
| 170 |
public IObservable createObservable(Object target) |
179 |
* the feature for which to track the value. |
| 171 |
{ |
180 |
* @return an observable factory. |
| 172 |
return observeList(realm, (EObject)target, eStructuralFeature); |
181 |
*/ |
| 173 |
} |
182 |
public static IObservableFactory valueFactory(final Realm realm, |
| 174 |
}; |
183 |
final EStructuralFeature eStructuralFeature) { |
| 175 |
} |
184 |
return new IObservableFactory() { |
| 176 |
|
185 |
public IObservable createObservable(Object target) { |
| 177 |
/** |
186 |
return observeValue(realm, (EObject) target, eStructuralFeature); |
| 178 |
* Returns a factory for creating observable maps |
187 |
} |
| 179 |
* tracking the value of the given feature of a particular {@link EObject object}. |
188 |
}; |
| 180 |
* @param eStructuralFeature the feature for which to track the value. |
189 |
} |
| 181 |
* @return an observable factory. |
190 |
|
| 182 |
*/ |
191 |
/** |
| 183 |
public static IObservableFactory mapFactory(final EStructuralFeature eStructuralFeature) |
192 |
* Returns an observable list that tracks the current value of the feature |
| 184 |
{ |
193 |
* of the current value of the master observable value. |
| 185 |
return |
194 |
* |
| 186 |
new IObservableFactory() |
195 |
* @param realm |
| 187 |
{ |
196 |
* the realm in which to observe. |
| 188 |
public IObservable createObservable(Object target) |
197 |
* @param value |
| 189 |
{ |
198 |
* the master observable value. |
| 190 |
return observeMap((IObservableSet)target, eStructuralFeature); |
199 |
* @param eStructuralFeature |
| 191 |
} |
200 |
* the feature for which to track the value. |
| 192 |
}; |
201 |
* @return an observable value that tracks the current value of the named |
| 193 |
} |
202 |
* property for the current value of the master observable value |
|
|
203 |
* @see MasterDetailObservables#detailList(IObservableValue, |
| 204 |
* IObservableFactory, Object) |
| 205 |
*/ |
| 206 |
public static IObservableList observeDetailList(Realm realm, |
| 207 |
IObservableValue value, EStructuralFeature eStructuralFeature) { |
| 208 |
return MasterDetailObservables.detailList(value, listFactory(realm, |
| 209 |
eStructuralFeature), eStructuralFeature); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Returns a factory for creating observable lists tracking the value of the |
| 214 |
* given feature of a particular {@link EObject object}. |
| 215 |
* |
| 216 |
* @param realm |
| 217 |
* the realm in which to observe. |
| 218 |
* @param eStructuralFeature |
| 219 |
* the feature for which to track the value. |
| 220 |
* @return an observable factory. |
| 221 |
*/ |
| 222 |
public static IObservableFactory listFactory(final Realm realm, |
| 223 |
final EStructuralFeature eStructuralFeature) { |
| 224 |
return new IObservableFactory() { |
| 225 |
public IObservable createObservable(Object target) { |
| 226 |
return observeList(realm, (EObject) target, eStructuralFeature); |
| 227 |
} |
| 228 |
}; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Returns a factory for creating observable maps tracking the value of the |
| 233 |
* given feature of a particular {@link EObject object}. |
| 234 |
* |
| 235 |
* @param eStructuralFeature |
| 236 |
* the feature for which to track the value. |
| 237 |
* @return an observable factory. |
| 238 |
*/ |
| 239 |
public static IObservableFactory mapFactory( |
| 240 |
final EStructuralFeature eStructuralFeature) { |
| 241 |
return new IObservableFactory() { |
| 242 |
public IObservable createObservable(Object target) { |
| 243 |
return observeMap((IObservableSet) target, eStructuralFeature); |
| 244 |
} |
| 245 |
}; |
| 246 |
} |
| 247 |
|
| 248 |
public static IObservableList observeResourceContents(Resource resource) { |
| 249 |
return new EWritableList<EObject>((NotifyingList<EObject>) resource.getContents()); |
| 250 |
} |
| 194 |
} |
251 |
} |