78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2007, 2008 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 * Bob Smith - bug 198880
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module org.eclipse.core.databinding.ObservablesManager;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import java.util.HashMap;
|
|
18 import java.util.HashSet;
|
|
19 import java.util.Iterator;
|
|
20 import java.util.Map;
|
|
21 import java.util.Set;
|
|
22
|
|
23 import org.eclipse.core.databinding.observable.IObservable;
|
|
24 import org.eclipse.core.internal.databinding.Pair;
|
|
25
|
|
26 /**
|
|
27 * An observables manager can be used for lifecycle management of
|
|
28 * {@link IObservable} objects.
|
|
29 *
|
|
30 * @noextend This class is not intended to be subclassed by clients.
|
|
31 *
|
|
32 * @since 1.0
|
|
33 *
|
|
34 */
|
|
35 public class ObservablesManager {
|
|
36
|
|
37 private Set managedObservables = new HashSet();
|
|
38 private Set excludedObservables = new HashSet();
|
|
39 private Map contexts = new HashMap();
|
|
40
|
|
41 /**
|
|
42 * Create a new observables manager.
|
|
43 */
|
|
44 public this() {
|
|
45 }
|
|
46
|
|
47 /**
|
|
48 * Adds the given observable to this manager.
|
|
49 *
|
|
50 * @param observable
|
|
51 * the observable
|
|
52 */
|
|
53 public void addObservable(IObservable observable) {
|
|
54 managedObservables.add(observable);
|
|
55 }
|
|
56
|
|
57 /**
|
|
58 * Adds the given observable to this manager's exclusion list. The given
|
|
59 * observable will not be disposed of by this manager.
|
|
60 *
|
|
61 * @param observable
|
|
62 * the observable
|
|
63 */
|
|
64 public void excludeObservable(IObservable observable) {
|
|
65 excludedObservables.add(observable);
|
|
66 }
|
|
67
|
|
68 /**
|
|
69 * Adds the given data binding context's target and/or model observables to
|
|
70 * this manager.
|
|
71 *
|
|
72 * @param context
|
|
73 * the data binding context
|
|
74 * @param trackTargets
|
|
75 * <code>true</code> if the target observables of the context
|
|
76 * should be managed
|
|
77 * @param trackModels
|
|
78 * <code>true</code> if the model observables of the context
|
|
79 * should be managed
|
|
80 */
|
|
81 public void addObservablesFromContext(DataBindingContext context,
|
|
82 bool trackTargets, bool trackModels) {
|
|
83 if (trackTargets || trackModels) {
|
|
84 contexts.put(context, new Pair(new Boolean(trackTargets),
|
|
85 new Boolean(trackModels)));
|
|
86 }
|
|
87 }
|
|
88
|
|
89 /**
|
|
90 * Disposes of this manager and all observables that it manages.
|
|
91 */
|
|
92 public void dispose() {
|
|
93 Set observables = new HashSet();
|
|
94 observables.addAll(managedObservables);
|
|
95 for (Iterator it = contexts.keySet().iterator(); it.hasNext();) {
|
|
96 DataBindingContext context = cast(DataBindingContext) it.next();
|
|
97 Pair trackModelsOrTargets = cast(Pair) contexts.get(context);
|
|
98 bool disposeTargets = (cast(Boolean) trackModelsOrTargets.a)
|
|
99 .booleanValue();
|
|
100 bool disposeModels = (cast(Boolean) trackModelsOrTargets.b)
|
|
101 .booleanValue();
|
|
102 for (Iterator it2 = context.getBindings().iterator(); it2.hasNext();) {
|
|
103 Binding binding = cast(Binding) it2.next();
|
|
104 if (disposeTargets) {
|
|
105 observables.add(binding.getTarget());
|
|
106 }
|
|
107 if (disposeModels) {
|
|
108 observables.add(binding.getModel());
|
|
109 }
|
|
110 }
|
|
111 }
|
|
112 observables.removeAll(excludedObservables);
|
|
113 for (Iterator it = observables.iterator(); it.hasNext();) {
|
|
114 IObservable observable = cast(IObservable) it.next();
|
|
115 observable.dispose();
|
|
116 }
|
|
117 }
|
|
118 }
|