comparison org.eclipse.core.databinding/src/org/eclipse/core/databinding/observable/Observables.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 383ce7bd736b
comparison
equal deleted inserted replaced
76:f05e6e8b2f2d 78:0a55d2d5a946
1 /*******************************************************************************
2 * Copyright (c) 2006-2008 Cerner 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 * Brad Reynolds - initial API and implementation
10 * Matt Carter - bug 212518 (constantObservableValue)
11 * Matthew Hall - bugs 208332, 212518, 219909, 184830
12 * Marko Topolnik - bug 184830
13 ******************************************************************************/
14
15 module org.eclipse.core.databinding.observable.Observables;
16
17 import java.lang.all;
18
19 import java.util.List;
20 import java.util.Set;
21
22 import org.eclipse.core.databinding.observable.list.IListChangeListener;
23 import org.eclipse.core.databinding.observable.list.IObservableList;
24 import org.eclipse.core.databinding.observable.list.ObservableList;
25 import org.eclipse.core.databinding.observable.map.IObservableMap;
26 import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory;
27 import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables;
28 import org.eclipse.core.databinding.observable.set.IObservableSet;
29 import org.eclipse.core.databinding.observable.set.ISetChangeListener;
30 import org.eclipse.core.databinding.observable.set.ObservableSet;
31 import org.eclipse.core.databinding.observable.value.IObservableValue;
32 import org.eclipse.core.internal.databinding.observable.ConstantObservableValue;
33 import org.eclipse.core.internal.databinding.observable.EmptyObservableList;
34 import org.eclipse.core.internal.databinding.observable.EmptyObservableSet;
35 import org.eclipse.core.internal.databinding.observable.MapEntryObservableValue;
36 import org.eclipse.core.internal.databinding.observable.ProxyObservableList;
37 import org.eclipse.core.internal.databinding.observable.ProxyObservableSet;
38 import org.eclipse.core.internal.databinding.observable.StalenessObservableValue;
39 import org.eclipse.core.internal.databinding.observable.UnmodifiableObservableList;
40 import org.eclipse.core.internal.databinding.observable.UnmodifiableObservableSet;
41 import org.eclipse.core.internal.databinding.observable.UnmodifiableObservableValue;
42 import org.eclipse.core.runtime.Assert;
43
44 /**
45 * Contains static methods to operate on or return
46 * {@link IObservable Observables}.
47 *
48 * @since 1.0
49 */
50 public class Observables {
51 /**
52 * Returns an unmodifiable observable value backed by the given observable
53 * value.
54 *
55 * @param value
56 * the value to wrap in an unmodifiable value
57 * @return an unmodifiable observable value backed by the given observable
58 * value
59 * @since 1.1
60 */
61 public static IObservableValue unmodifiableObservableValue(
62 IObservableValue value) {
63 Assert.isNotNull(value, "Argument 'value' cannot be null"); //$NON-NLS-1$
64 return new UnmodifiableObservableValue(value);
65 }
66
67 /**
68 * Returns an observable value with the given constant value.
69 *
70 * @param realm
71 * the observable's realm
72 * @param value
73 * the observable's constant value
74 * @param valueType
75 * the observable's value type
76 * @return an immutable observable value with the given constant value
77 * @since 1.1
78 */
79 public static IObservableValue constantObservableValue(Realm realm,
80 Object value, Object valueType) {
81 return new ConstantObservableValue(realm, value, valueType);
82 }
83
84 /**
85 * Returns an observable value with the given constant value.
86 *
87 * @param realm
88 * the observable's realm
89 * @param value
90 * the observable's constant value
91 * @return an immutable observable value with the given constant value
92 * @since 1.1
93 */
94 public static IObservableValue constantObservableValue(Realm realm,
95 Object value) {
96 return constantObservableValue(realm, value, null);
97 }
98
99 /**
100 * Returns an observable value with the given constant value.
101 *
102 * @param value
103 * the observable's constant value
104 * @param valueType
105 * the observable's value type
106 * @return an immutable observable value with the given constant value
107 * @since 1.1
108 */
109 public static IObservableValue constantObservableValue(Object value,
110 Object valueType) {
111 return constantObservableValue(Realm.getDefault(), value, valueType);
112 }
113
114 /**
115 * Returns an observable value with the given constant value.
116 *
117 * @param value
118 * the observable's constant value
119 * @return an immutable observable value with the given constant value
120 * @since 1.1
121 */
122 public static IObservableValue constantObservableValue(Object value) {
123 return constantObservableValue(Realm.getDefault(), value, null);
124 }
125
126 /**
127 * Returns an unmodifiable observable list backed by the given observable
128 * list.
129 *
130 * @param list
131 * the list to wrap in an unmodifiable list
132 * @return an unmodifiable observable list backed by the given observable
133 * list
134 */
135 public static IObservableList unmodifiableObservableList(
136 IObservableList list) {
137 if (list is null) {
138 throw new IllegalArgumentException("List parameter cannot be null."); //$NON-NLS-1$
139 }
140
141 return new UnmodifiableObservableList(list);
142 }
143
144 /**
145 * Returns an unmodifiable observable set backed by the given observable
146 * set.
147 *
148 * @param set
149 * the set to wrap in an unmodifiable set
150 * @return an unmodifiable observable set backed by the given observable set
151 * @since 1.1
152 */
153 public static IObservableSet unmodifiableObservableSet(IObservableSet set) {
154 if (set is null) {
155 throw new IllegalArgumentException("Set parameter cannot be null"); //$NON-NLS-1$
156 }
157
158 return new UnmodifiableObservableSet(set);
159 }
160
161 /**
162 * Returns an empty observable list. The returned list continues to work
163 * after it has been disposed of and can be disposed of multiple times.
164 *
165 * @return an empty observable list.
166 */
167 public static IObservableList emptyObservableList() {
168 return emptyObservableList(Realm.getDefault(), null);
169 }
170
171 /**
172 * Returns an empty observable list of the given element type. The returned
173 * list continues to work after it has been disposed of and can be disposed
174 * of multiple times.
175 *
176 * @param elementType
177 * the element type of the returned list
178 * @return an empty observable list
179 * @since 1.1
180 */
181 public static IObservableList emptyObservableList(Object elementType) {
182 return emptyObservableList(Realm.getDefault(), elementType);
183 }
184
185 /**
186 * Returns an empty observable list belonging to the given realm. The
187 * returned list continues to work after it has been disposed of and can be
188 * disposed of multiple times.
189 *
190 * @param realm
191 * the realm of the returned list
192 * @return an empty observable list.
193 */
194 public static IObservableList emptyObservableList(Realm realm) {
195 return emptyObservableList(realm, null);
196 }
197
198 /**
199 * Returns an empty observable list of the given element type and belonging
200 * to the given realm. The returned list continues to work after it has been
201 * disposed of and can be disposed of multiple times.
202 *
203 * @param realm
204 * the realm of the returned list
205 * @param elementType
206 * the element type of the returned list
207 * @return an empty observable list
208 * @since 1.1
209 */
210 public static IObservableList emptyObservableList(Realm realm,
211 Object elementType) {
212 return new EmptyObservableList(realm, elementType);
213 }
214
215 /**
216 * Returns an empty observable set. The returned set continues to work after
217 * it has been disposed of and can be disposed of multiple times.
218 *
219 * @return an empty observable set.
220 */
221 public static IObservableSet emptyObservableSet() {
222 return emptyObservableSet(Realm.getDefault(), null);
223 }
224
225 /**
226 * Returns an empty observable set of the given element type. The returned
227 * set continues to work after it has been disposed of and can be disposed
228 * of multiple times.
229 *
230 * @param elementType
231 * the element type of the returned set
232 * @return an empty observable set
233 * @since 1.1
234 */
235 public static IObservableSet emptyObservableSet(Object elementType) {
236 return emptyObservableSet(Realm.getDefault(), elementType);
237 }
238
239 /**
240 * Returns an empty observable set belonging to the given realm. The
241 * returned set continues to work after it has been disposed of and can be
242 * disposed of multiple times.
243 *
244 * @param realm
245 * the realm of the returned set
246 * @return an empty observable set.
247 */
248 public static IObservableSet emptyObservableSet(Realm realm) {
249 return emptyObservableSet(realm, null);
250 }
251
252 /**
253 * Returns an empty observable set of the given element type and belonging
254 * to the given realm. The returned set continues to work after it has been
255 * disposed of and can be disposed of multiple times.
256 *
257 * @param realm
258 * the realm of the returned set
259 * @param elementType
260 * the element type of the returned set
261 * @return an empty observable set
262 * @since 1.1
263 */
264 public static IObservableSet emptyObservableSet(Realm realm,
265 Object elementType) {
266 return new EmptyObservableSet(realm, elementType);
267 }
268
269 /**
270 * Returns an observable set backed by the given set.
271 *
272 * @param set
273 * the set to wrap in an IObservableSet
274 * @return an observable set backed by the given set
275 */
276 public static IObservableSet staticObservableSet(Set set) {
277 return staticObservableSet(Realm.getDefault(), set, Object.classinfo);
278 }
279
280 /**
281 * Returns an observable set of the given element type, backed by the given
282 * set.
283 *
284 * @param set
285 * the set to wrap in an IObservableSet
286 * @param elementType
287 * the element type of the returned set
288 * @return Returns an observable set backed by the given unchanging set
289 * @since 1.1
290 */
291 public static IObservableSet staticObservableSet(Set set, Object elementType) {
292 return staticObservableSet(Realm.getDefault(), set, elementType);
293 }
294
295 /**
296 * Returns an observable set belonging to the given realm, backed by the
297 * given set.
298 *
299 * @param realm
300 * the realm of the returned set
301 * @param set
302 * the set to wrap in an IObservableSet
303 * @return an observable set backed by the given unchanging set
304 */
305 public static IObservableSet staticObservableSet(Realm realm, Set set) {
306 return staticObservableSet(realm, set, Object.classinfo);
307 }
308
309 /**
310 * Returns an observable set of the given element type and belonging to the
311 * given realm, backed by the given set.
312 *
313 * @param realm
314 * the realm of the returned set
315 * @param set
316 * the set to wrap in an IObservableSet
317 * @param elementType
318 * the element type of the returned set
319 * @return an observable set backed by the given set
320 * @since 1.1
321 */
322 public static IObservableSet staticObservableSet(Realm realm, Set set,
323 Object elementType) {
324 return new class(realm, set, elementType) ObservableSet {
325 public void addChangeListener(IChangeListener listener) {
326 }
327
328 public void addStaleListener(IStaleListener listener) {
329 }
330
331 public void addSetChangeListener(ISetChangeListener listener) {
332 }
333 };
334 }
335
336 /**
337 * Returns an observable set that contains the same elements as the given
338 * set, and fires the same events as the given set, but can be disposed of
339 * without disposing of the wrapped set.
340 *
341 * @param target
342 * the set to wrap
343 * @return a disposable proxy for the given observable set
344 */
345 public static IObservableSet proxyObservableSet(IObservableSet target) {
346 return new ProxyObservableSet(target);
347 }
348
349 /**
350 * Returns an observable list that contains the same elements as the given
351 * list, and fires the same events as the given list, but can be disposed of
352 * without disposing of the wrapped list.
353 *
354 * @param target
355 * the list to wrap
356 * @return a disposable proxy for the given observable list
357 * @since 1.1
358 */
359 public static IObservableList proxyObservableList(IObservableList target) {
360 return new ProxyObservableList(target);
361 }
362
363 /**
364 * Returns an observable list backed by the given list.
365 *
366 * @param list
367 * the list to wrap in an IObservableList
368 * @return an observable list backed by the given unchanging list
369 */
370 public static IObservableList staticObservableList(List list) {
371 return staticObservableList(Realm.getDefault(), list, Object.classinfo);
372 }
373
374 /**
375 * Returns an observable list of the given element type, backed by the given
376 * list.
377 *
378 * @param list
379 * the list to wrap in an IObservableList
380 * @param elementType
381 * the element type of the returned list
382 * @return an observable list backed by the given unchanging list
383 * @since 1.1
384 */
385 public static IObservableList staticObservableList(List list,
386 Object elementType) {
387 return staticObservableList(Realm.getDefault(), list, elementType);
388 }
389
390 /**
391 * Returns an observable list belonging to the given realm, backed by the
392 * given list.
393 *
394 * @param realm
395 * the realm of the returned list
396 * @param list
397 * the list to wrap in an IObservableList
398 * @return an observable list backed by the given unchanging list
399 */
400 public static IObservableList staticObservableList(Realm realm, List list) {
401 return staticObservableList(realm, list, Object.classinfo);
402 }
403
404 /**
405 * Returns an observable list of the given element type and belonging to the
406 * given realm, backed by the given list.
407 *
408 * @param realm
409 * the realm of the returned list
410 * @param list
411 * the list to wrap in an IObservableList
412 * @param elementType
413 * the element type of the returned list
414 * @return an observable list backed by the given unchanging list
415 * @since 1.1
416 */
417 public static IObservableList staticObservableList(Realm realm, List list,
418 Object elementType) {
419 return new class(realm, list, elementType) ObservableList {
420 public void addChangeListener(IChangeListener listener) {
421 }
422
423 public void addStaleListener(IStaleListener listener) {
424 }
425
426 public void addListChangeListener(IListChangeListener listener) {
427 }
428 };
429 }
430
431 /**
432 * Returns an observable value of type <code>Boolean.TYPE</code> which
433 * tracks whether the given observable is stale.
434 *
435 * @param observable
436 * the observable to track
437 * @return an observable value which tracks whether the given observable is
438 * stale
439 *
440 * @since 1.1
441 */
442 public static IObservableValue observeStale(IObservable observable) {
443 return new StalenessObservableValue(observable);
444 }
445
446 /**
447 * Returns an observable value that tracks changes to the value of an
448 * observable map's entry specified by its key.
449 * <p>
450 * The state where the key does not exist in the map is equivalent to the
451 * state where the key exists and its value is <code>null</code>. The
452 * transition between these two states is not considered a value change and
453 * no event is fired.
454 *
455 * @param map
456 * the observable map whose entry will be tracked.
457 * @param key
458 * the key identifying the map entry to track.
459 * @param valueType
460 * the type of the value. May be <code>null</code>, meaning
461 * the value is untyped.
462 * @return an observable value that tracks the value associated with the
463 * specified key in the given map
464 * @since 1.1
465 */
466 public static IObservableValue observeMapEntry(IObservableMap map,
467 Object key, Object valueType) {
468 return new MapEntryObservableValue(map, key, valueType);
469 }
470
471 /**
472 * Returns a factory for creating obervable values tracking the value of the
473 * {@link IObservableMap observable map} entry identified by a particular
474 * key.
475 *
476 * @param map
477 * the observable map whose entry will be tracked.
478 * @param valueType
479 * the type of the value. May be <code>null</code>, meaning
480 * the value is untyped.
481 * @return a factory for creating observable values tracking the value of
482 * the observable map entry identified by a particular key object.
483 * @since 1.1
484 */
485 public static IObservableFactory mapEntryValueFactory(
486 IObservableMap map, Object valueType) {
487 return new class() IObservableFactory {
488 IObservableMap map_;
489 Object valueType_;
490 this(IObservableMap a, Object b){
491 map_=a; valueType_=b;
492 }
493 public IObservable createObservable(Object key) {
494 return observeMapEntry(map_, key, valueType_);
495 }
496 };
497 }
498
499 /**
500 * Helper method for <code>MasterDetailObservables.detailValue(master,
501 * mapEntryValueFactory(map, valueType), valueType)</code>.
502 *
503 * @param map
504 * the observable map whose entry will be tracked.
505 * @param master
506 * the observable value that identifies which map entry to track.
507 * @param valueType
508 * the type of the value. May be <code>null</code>, meaning
509 * the value is untyped.
510 * @return an observable value tracking the current value of the specified
511 * key in the given map an observable value that tracks the current
512 * value of the named property for the current value of the master
513 * observable value
514 * @since 1.1
515 */
516 public static IObservableValue observeDetailMapEntry(IObservableMap map,
517 IObservableValue master, Object valueType) {
518 return MasterDetailObservables.detailValue(master,
519 mapEntryValueFactory(map, valueType), valueType);
520 }
521 }