78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006 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 ******************************************************************************/
|
|
11
|
|
12 module org.eclipse.core.internal.databinding.observable.StalenessTracker;
|
81
|
13 import org.eclipse.core.internal.databinding.observable.IStalenessConsumer;
|
78
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import java.util.HashMap;
|
|
18 import java.util.Map;
|
|
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.StaleEvent;
|
|
25 import org.eclipse.core.internal.databinding.IdentityWrapper;
|
|
26
|
|
27 /**
|
|
28 * @since 1.0
|
|
29 *
|
|
30 */
|
|
31 public class StalenessTracker {
|
|
32
|
|
33 private Map staleMap = new HashMap();
|
|
34
|
|
35 private int staleCount = 0;
|
|
36
|
|
37 private final IStalenessConsumer stalenessConsumer;
|
|
38
|
|
39 private class ChildListener : IStaleListener, IChangeListener {
|
|
40 public void handleStale(StaleEvent event) {
|
|
41 processStalenessChange(cast(IObservable) event.getSource(), true);
|
|
42 }
|
|
43
|
|
44 public void handleChange(ChangeEvent event) {
|
|
45 processStalenessChange(cast(IObservable) event.getSource(), true);
|
|
46 }
|
|
47 }
|
|
48
|
|
49 private ChildListener childListener = new ChildListener();
|
|
50
|
|
51 /**
|
|
52 * @param observables
|
|
53 * @param stalenessConsumer
|
|
54 */
|
|
55 public this(IObservable[] observables,
|
|
56 IStalenessConsumer stalenessConsumer) {
|
|
57 this.stalenessConsumer = stalenessConsumer;
|
|
58 for (int i = 0; i < observables.length; i++) {
|
|
59 IObservable observable = observables[i];
|
|
60 doAddObservable(observable, false);
|
|
61 }
|
|
62 stalenessConsumer.setStale(staleCount > 0);
|
|
63 }
|
|
64
|
|
65 /**
|
|
66 * @param child
|
|
67 * @param callback
|
|
68 */
|
|
69 public void processStalenessChange(IObservable child, bool callback) {
|
|
70 bool oldStale = staleCount > 0;
|
|
71 IdentityWrapper wrappedChild = new IdentityWrapper(child);
|
|
72 bool oldChildStale = getOldChildStale(wrappedChild);
|
|
73 bool newChildStale = child.isStale();
|
|
74 if (oldChildStale !is newChildStale) {
|
|
75 if (oldChildStale) {
|
|
76 staleCount--;
|
|
77 } else {
|
|
78 staleCount++;
|
|
79 }
|
|
80 staleMap.put(wrappedChild, newChildStale ? Boolean.TRUE : Boolean.FALSE);
|
|
81 }
|
|
82 bool newStale = staleCount > 0;
|
|
83 if (callback && (newStale !is oldStale)) {
|
|
84 stalenessConsumer.setStale(newStale);
|
|
85 }
|
|
86 }
|
|
87
|
|
88 /**
|
|
89 * @param wrappedChild
|
|
90 */
|
|
91 private bool getOldChildStale(IdentityWrapper wrappedChild) {
|
|
92 Object oldChildValue = staleMap.get(wrappedChild);
|
|
93 bool oldChildStale = oldChildValue is null ? false
|
|
94 : (cast(Boolean) oldChildValue).booleanValue();
|
|
95 return oldChildStale;
|
|
96 }
|
|
97
|
|
98 /**
|
|
99 * @param observable
|
|
100 */
|
|
101 public void addObservable(IObservable observable) {
|
|
102 doAddObservable(observable, true);
|
|
103 }
|
|
104
|
|
105 private void doAddObservable(IObservable observable, bool callback) {
|
|
106 processStalenessChange(observable, callback);
|
|
107 observable.addChangeListener(childListener);
|
|
108 observable.addStaleListener(childListener);
|
|
109 }
|
|
110
|
|
111 /**
|
|
112 * @param observable
|
|
113 */
|
|
114 public void removeObservable(IObservable observable) {
|
|
115 bool oldStale = staleCount > 0;
|
|
116 IdentityWrapper wrappedChild = new IdentityWrapper(observable);
|
|
117 bool oldChildStale = getOldChildStale(wrappedChild);
|
|
118 if (oldChildStale) {
|
|
119 staleCount--;
|
|
120 }
|
|
121 staleMap.remove(wrappedChild);
|
|
122 observable.removeChangeListener(childListener);
|
|
123 observable.removeStaleListener(childListener);
|
|
124 bool newStale = staleCount > 0;
|
|
125 if (newStale !is oldStale) {
|
|
126 stalenessConsumer.setStale(newStale);
|
|
127 }
|
|
128 }
|
|
129
|
|
130 }
|