78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2005, 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 * Brad Reynolds - bug 116920
|
|
11 * Brad Reynolds - bug 147515
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.core.databinding.observable.value.ComputedValue;
|
81
|
14 import org.eclipse.core.databinding.observable.value.ValueDiff;
|
|
15 import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
|
|
16 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
|
78
|
17
|
|
18 import java.lang.all;
|
|
19
|
|
20 import org.eclipse.core.databinding.observable.ChangeEvent;
|
|
21 import org.eclipse.core.databinding.observable.IChangeListener;
|
|
22 import org.eclipse.core.databinding.observable.IObservable;
|
|
23 import org.eclipse.core.databinding.observable.IStaleListener;
|
|
24 import org.eclipse.core.databinding.observable.ObservableTracker;
|
|
25 import org.eclipse.core.databinding.observable.Realm;
|
|
26 import org.eclipse.core.databinding.observable.StaleEvent;
|
|
27
|
|
28 /**
|
|
29 * A Lazily calculated value that automatically computes and registers listeners
|
|
30 * on its dependencies as long as all of its dependencies are IObservable
|
|
31 * objects
|
|
32 * <p>
|
|
33 * This class is thread safe. All state accessing methods must be invoked from
|
|
34 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
|
|
35 * listeners may be invoked from any thread.
|
|
36 * </p>
|
|
37 *
|
|
38 * @since 1.0
|
|
39 */
|
|
40 public abstract class ComputedValue : AbstractObservableValue {
|
|
41
|
|
42 private bool dirty = true;
|
|
43
|
|
44 private bool stale = false;
|
|
45
|
|
46 private Object cachedValue = null;
|
|
47
|
|
48 /**
|
|
49 * Array of observables this computed value depends on. This field has a
|
|
50 * value of <code>null</code> if we are not currently listening.
|
|
51 */
|
|
52 private IObservable[] dependencies = null;
|
|
53
|
|
54 /**
|
|
55 *
|
|
56 */
|
|
57 public this() {
|
|
58 this(Realm.getDefault(), null);
|
|
59 }
|
|
60
|
|
61 /**
|
|
62 * @param valueType
|
|
63 * can be <code>null</code>
|
|
64 */
|
|
65 public this(Object valueType) {
|
|
66 this(Realm.getDefault(), valueType);
|
|
67 }
|
|
68
|
|
69 /**
|
|
70 * @param realm
|
|
71 *
|
|
72 */
|
|
73 public this(Realm realm) {
|
|
74 this(realm, null);
|
|
75 }
|
|
76
|
|
77 /**
|
|
78 * @param realm
|
|
79 * @param valueType
|
|
80 */
|
|
81 public this(Realm realm, Object valueType) {
|
|
82 super(realm);
|
|
83 this.valueType = valueType;
|
|
84 }
|
|
85
|
|
86 /**
|
|
87 * Inner class that implements interfaces that we don't want to expose as
|
|
88 * public API. Each interface could have been implemented using a separate
|
|
89 * anonymous class, but we combine them here to reduce the memory overhead
|
|
90 * and number of classes.
|
|
91 *
|
|
92 * <p>
|
|
93 * The Runnable calls computeValue and stores the result in cachedValue.
|
|
94 * </p>
|
|
95 *
|
|
96 * <p>
|
|
97 * The IChangeListener stores each observable in the dependencies list. This
|
|
98 * is registered as the listener when calling ObservableTracker, to detect
|
|
99 * every observable that is used by computeValue.
|
|
100 * </p>
|
|
101 *
|
|
102 * <p>
|
|
103 * The IChangeListener is attached to every dependency.
|
|
104 * </p>
|
|
105 *
|
|
106 */
|
|
107 private class PrivateInterface : Runnable, IChangeListener,
|
|
108 IStaleListener {
|
|
109 public void run() {
|
|
110 cachedValue = calculate();
|
|
111 }
|
|
112
|
|
113 public void handleStale(StaleEvent event) {
|
|
114 if (!dirty && !stale) {
|
|
115 stale = true;
|
|
116 fireStale();
|
|
117 }
|
|
118 }
|
|
119
|
|
120 public void handleChange(ChangeEvent event) {
|
|
121 makeDirty();
|
|
122 }
|
|
123 }
|
|
124
|
|
125 private PrivateInterface privateInterface = new PrivateInterface();
|
|
126
|
|
127 private Object valueType;
|
|
128
|
|
129 protected final Object doGetValue() {
|
|
130 if (dirty) {
|
|
131 // This line will do the following:
|
|
132 // - Run the calculate method
|
|
133 // - While doing so, add any observable that is touched to the
|
|
134 // dependencies list
|
|
135 IObservable[] newDependencies = ObservableTracker.runAndMonitor(
|
|
136 privateInterface, privateInterface, null);
|
|
137
|
|
138 stale = false;
|
|
139 for (int i = 0; i < newDependencies.length; i++) {
|
|
140 IObservable observable = newDependencies[i];
|
|
141 // Add a change listener to the new dependency.
|
|
142 if (observable.isStale()) {
|
|
143 stale = true;
|
|
144 } else {
|
|
145 observable.addStaleListener(privateInterface);
|
|
146 }
|
|
147 }
|
|
148
|
|
149 dependencies = newDependencies;
|
|
150
|
|
151 dirty = false;
|
|
152 }
|
|
153
|
|
154 return cachedValue;
|
|
155 }
|
|
156
|
|
157 /**
|
|
158 * Subclasses must override this method to provide the object's value.
|
|
159 *
|
|
160 * @return the object's value
|
|
161 */
|
|
162 protected abstract Object calculate();
|
|
163
|
|
164 protected final void makeDirty() {
|
|
165 if (!dirty) {
|
|
166 dirty = true;
|
|
167
|
|
168 stopListening();
|
|
169
|
|
170 // copy the old value
|
|
171 final Object oldValue = cachedValue;
|
|
172 // Fire the "dirty" event. This implementation recomputes the new
|
|
173 // value lazily.
|
|
174 fireValueChange(new class() ValueDiff {
|
|
175
|
|
176 public Object getOldValue() {
|
|
177 return oldValue;
|
|
178 }
|
|
179
|
|
180 public Object getNewValue() {
|
|
181 return getValue();
|
|
182 }
|
|
183 });
|
|
184 }
|
|
185 }
|
|
186
|
|
187 /**
|
|
188 *
|
|
189 */
|
|
190 private void stopListening() {
|
|
191 // Stop listening for dependency changes.
|
|
192 if (dependencies !is null) {
|
|
193 for (int i = 0; i < dependencies.length; i++) {
|
|
194 IObservable observable = dependencies[i];
|
|
195
|
|
196 observable.removeChangeListener(privateInterface);
|
|
197 observable.removeStaleListener(privateInterface);
|
|
198 }
|
|
199 dependencies = null;
|
|
200 }
|
|
201 }
|
|
202
|
|
203 public bool isStale() {
|
|
204 // we need to recompute, otherwise staleness wouldn't mean anything
|
|
205 getValue();
|
|
206 return stale;
|
|
207 }
|
|
208
|
|
209 public Object getValueType() {
|
|
210 return valueType;
|
|
211 }
|
|
212
|
|
213 // this method exists here so that we can call it from the runnable below.
|
|
214 /**
|
|
215 * @since 1.1
|
|
216 */
|
|
217 protected bool hasListeners() {
|
|
218 return super.hasListeners();
|
|
219 }
|
|
220
|
|
221 public synchronized void addChangeListener(IChangeListener listener) {
|
|
222 super.addChangeListener(listener);
|
|
223 // If somebody is listening, we need to make sure we attach our own
|
|
224 // listeners
|
|
225 computeValueForListeners();
|
|
226 }
|
|
227
|
|
228 /**
|
|
229 * Some clients just add a listener and expect to get notified even if they
|
|
230 * never called getValue(), so we have to call getValue() ourselves here to
|
|
231 * be sure. Need to be careful about realms though, this method can be
|
|
232 * called outside of our realm. See also bug 198211. If a client calls this
|
|
233 * outside of our realm, they may receive change notifications before the
|
|
234 * runnable below has been executed. It is their job to figure out what to
|
|
235 * do with those notifications.
|
|
236 */
|
|
237 private void computeValueForListeners() {
|
|
238 getRealm().exec(new class() Runnable {
|
|
239 public void run() {
|
|
240 if (dependencies is null) {
|
|
241 // We are not currently listening.
|
|
242 if (hasListeners()) {
|
|
243 // But someone is listening for changes. Call getValue()
|
|
244 // to make sure we start listening to the observables we
|
|
245 // depend on.
|
|
246 getValue();
|
|
247 }
|
|
248 }
|
|
249 }
|
|
250 });
|
|
251 }
|
|
252
|
|
253 public synchronized void addValueChangeListener(
|
|
254 IValueChangeListener listener) {
|
|
255 super.addValueChangeListener(listener);
|
|
256 // If somebody is listening, we need to make sure we attach our own
|
|
257 // listeners
|
|
258 computeValueForListeners();
|
|
259 }
|
|
260
|
|
261 public synchronized void dispose() {
|
|
262 super.dispose();
|
|
263 stopListening();
|
|
264 }
|
|
265
|
|
266 }
|