KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > ObservablesManager


1 /*******************************************************************************
2  * Copyright (c) 2007 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 package org.eclipse.core.databinding;
14
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.databinding.observable.IObservable;
22 import org.eclipse.core.internal.databinding.Pair;
23
24 /**
25  * An observables manager can be used for lifecycle management of
26  * {@link IObservable} objects.
27  *
28  * This class is not intended to be extended by clients.
29  *
30  * @since 1.0
31  *
32  */

33 public class ObservablesManager {
34
35     private Set JavaDoc managedObservables = new HashSet JavaDoc();
36     private Set JavaDoc excludedObservables = new HashSet JavaDoc();
37     private Map JavaDoc contexts = new HashMap JavaDoc();
38
39     /**
40      * Create a new observables manager.
41      */

42     public ObservablesManager() {
43     }
44
45     /**
46      * Adds the given observable to this manager.
47      *
48      * @param observable
49      * the observable
50      */

51     public void addObservable(IObservable observable) {
52         managedObservables.add(observable);
53     }
54
55     /**
56      * Adds the given observable to this manager's exclusion list. The given
57      * observable will not be disposed of by this manager.
58      *
59      * @param observable
60      * the observable
61      */

62     public void excludeObservable(IObservable observable) {
63         excludedObservables.add(observable);
64     }
65
66     /**
67      * Adds the given data binding context's target and/or model observables to
68      * this manager.
69      *
70      * @param context
71      * the data binding context
72      * @param trackTargets
73      * <code>true</code> if the target observables of the context
74      * should be managed
75      * @param trackModels
76      * <code>true</code> if the model observables of the context
77      * should be managed
78      */

79     public void addObservablesFromContext(DataBindingContext context,
80             boolean trackTargets, boolean trackModels) {
81         if (trackTargets || trackModels) {
82             contexts.put(context, new Pair(new Boolean JavaDoc(trackTargets),
83                     new Boolean JavaDoc(trackModels)));
84         }
85     }
86
87     /**
88      * Disposes of this manager and all observables that it manages.
89      */

90     public void dispose() {
91         Set JavaDoc observables = new HashSet JavaDoc();
92         observables.addAll(managedObservables);
93         for (Iterator JavaDoc it = contexts.keySet().iterator(); it.hasNext();) {
94             DataBindingContext context = (DataBindingContext) it.next();
95             Pair trackModelsOrTargets = (Pair) contexts.get(context);
96             boolean disposeTargets = ((Boolean JavaDoc) trackModelsOrTargets.a)
97                     .booleanValue();
98             boolean disposeModels = ((Boolean JavaDoc) trackModelsOrTargets.b)
99                     .booleanValue();
100             for (Iterator JavaDoc it2 = context.getBindings().iterator(); it2.hasNext();) {
101                 Binding binding = (Binding) it2.next();
102                 if (disposeTargets) {
103                     observables.add(binding.getTarget());
104                 }
105                 if (disposeModels) {
106                     observables.add(binding.getModel());
107                 }
108             }
109         }
110         observables.removeAll(excludedObservables);
111         for (Iterator JavaDoc it = observables.iterator(); it.hasNext();) {
112             IObservable observable = (IObservable) it.next();
113             observable.dispose();
114         }
115     }
116 }
117
Popular Tags