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
|
85
|
33 private Map staleMap;
|
78
|
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
|
85
|
49 private ChildListener childListener;
|
78
|
50
|
|
51 /**
|
|
52 * @param observables
|
|
53 * @param stalenessConsumer
|
|
54 */
|
|
55 public this(IObservable[] observables,
|
|
56 IStalenessConsumer stalenessConsumer) {
|
85
|
57 childListener = new ChildListener();
|
|
58 staleMap = new HashMap();
|
78
|
59 this.stalenessConsumer = stalenessConsumer;
|
|
60 for (int i = 0; i < observables.length; i++) {
|
|
61 IObservable observable = observables[i];
|
|
62 doAddObservable(observable, false);
|
|
63 }
|
|
64 stalenessConsumer.setStale(staleCount > 0);
|
|
65 }
|
|
66
|
|
67 /**
|
|
68 * @param child
|
|
69 * @param callback
|
|
70 */
|
|
71 public void processStalenessChange(IObservable child, bool callback) {
|
|
72 bool oldStale = staleCount > 0;
|
85
|
73 IdentityWrapper wrappedChild = new IdentityWrapper(cast(Object)child);
|
78
|
74 bool oldChildStale = getOldChildStale(wrappedChild);
|
|
75 bool newChildStale = child.isStale();
|
|
76 if (oldChildStale !is newChildStale) {
|
|
77 if (oldChildStale) {
|
|
78 staleCount--;
|
|
79 } else {
|
|
80 staleCount++;
|
|
81 }
|
|
82 staleMap.put(wrappedChild, newChildStale ? Boolean.TRUE : Boolean.FALSE);
|
|
83 }
|
|
84 bool newStale = staleCount > 0;
|
|
85 if (callback && (newStale !is oldStale)) {
|
|
86 stalenessConsumer.setStale(newStale);
|
|
87 }
|
|
88 }
|
|
89
|
|
90 /**
|
|
91 * @param wrappedChild
|
|
92 */
|
|
93 private bool getOldChildStale(IdentityWrapper wrappedChild) {
|
|
94 Object oldChildValue = staleMap.get(wrappedChild);
|
|
95 bool oldChildStale = oldChildValue is null ? false
|
|
96 : (cast(Boolean) oldChildValue).booleanValue();
|
|
97 return oldChildStale;
|
|
98 }
|
|
99
|
|
100 /**
|
|
101 * @param observable
|
|
102 */
|
|
103 public void addObservable(IObservable observable) {
|
|
104 doAddObservable(observable, true);
|
|
105 }
|
|
106
|
|
107 private void doAddObservable(IObservable observable, bool callback) {
|
|
108 processStalenessChange(observable, callback);
|
|
109 observable.addChangeListener(childListener);
|
|
110 observable.addStaleListener(childListener);
|
|
111 }
|
|
112
|
|
113 /**
|
|
114 * @param observable
|
|
115 */
|
|
116 public void removeObservable(IObservable observable) {
|
|
117 bool oldStale = staleCount > 0;
|
85
|
118 IdentityWrapper wrappedChild = new IdentityWrapper(cast(Object)observable);
|
78
|
119 bool oldChildStale = getOldChildStale(wrappedChild);
|
|
120 if (oldChildStale) {
|
|
121 staleCount--;
|
|
122 }
|
|
123 staleMap.remove(wrappedChild);
|
|
124 observable.removeChangeListener(childListener);
|
|
125 observable.removeStaleListener(childListener);
|
|
126 bool newStale = staleCount > 0;
|
|
127 if (newStale !is oldStale) {
|
|
128 stalenessConsumer.setStale(newStale);
|
|
129 }
|
|
130 }
|
|
131
|
|
132 }
|