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