KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > internal > observable > StalenessTracker


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 package org.eclipse.jface.internal.databinding.internal.observable;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.jface.internal.databinding.internal.IdentityWrapper;
18 import org.eclipse.jface.internal.databinding.provisional.observable.IChangeListener;
19 import org.eclipse.jface.internal.databinding.provisional.observable.IObservable;
20 import org.eclipse.jface.internal.databinding.provisional.observable.IStaleListener;
21
22 /**
23  * @since 1.0
24  *
25  */

26 public class StalenessTracker {
27
28     private Map JavaDoc staleMap = new HashMap JavaDoc();
29
30     private int staleCount = 0;
31
32     private final IStalenessConsumer stalenessConsumer;
33
34     private class ChildListener implements IStaleListener, IChangeListener {
35         public void handleStale(IObservable source) {
36             processStalenessChange(source, true);
37         }
38
39         public void handleChange(IObservable source) {
40             processStalenessChange(source, true);
41         }
42     }
43
44     private ChildListener childListener = new ChildListener();
45
46     /**
47      * @param observables
48      * @param stalenessConsumer
49      */

50     public StalenessTracker(IObservable[] observables,
51             IStalenessConsumer stalenessConsumer) {
52         this.stalenessConsumer = stalenessConsumer;
53         for (int i = 0; i < observables.length; i++) {
54             IObservable observable = observables[i];
55             doAddObservable(observable, false);
56         }
57         stalenessConsumer.setStale(staleCount > 0);
58     }
59
60     /**
61      * @param child
62      * @param callback
63      */

64     public void processStalenessChange(IObservable child, boolean callback) {
65         boolean oldStale = staleCount > 0;
66         IdentityWrapper wrappedChild = new IdentityWrapper(child);
67         boolean oldChildStale = getOldChildStale(wrappedChild);
68         boolean newChildStale = child.isStale();
69         if (oldChildStale != newChildStale) {
70             if (oldChildStale) {
71                 staleCount--;
72             } else {
73                 staleCount++;
74             }
75             staleMap.put(wrappedChild, new Boolean JavaDoc(newChildStale));
76         }
77         boolean newStale = staleCount > 0;
78         if (callback && (newStale != oldStale)) {
79             stalenessConsumer.setStale(newStale);
80         }
81     }
82
83     /**
84      * @param wrappedChild
85      */

86     private boolean getOldChildStale(IdentityWrapper wrappedChild) {
87         Object JavaDoc oldChildValue = staleMap.get(wrappedChild);
88         boolean oldChildStale = oldChildValue == null ? false
89                 : ((Boolean JavaDoc) oldChildValue).booleanValue();
90         return oldChildStale;
91     }
92
93     /**
94      * @param observable
95      */

96     public void addObservable(IObservable observable) {
97         doAddObservable(observable, true);
98     }
99
100     private void doAddObservable(IObservable observable, boolean callback) {
101         processStalenessChange(observable, callback);
102         observable.addChangeListener(childListener);
103         observable.addStaleListener(childListener);
104     }
105
106     /**
107      * @param observable
108      */

109     public void removeObservable(IObservable observable) {
110         boolean oldStale = staleCount > 0;
111         IdentityWrapper wrappedChild = new IdentityWrapper(observable);
112         boolean oldChildStale = getOldChildStale(wrappedChild);
113         if (oldChildStale) {
114             staleCount--;
115         }
116         staleMap.remove(wrappedChild);
117         observable.removeChangeListener(childListener);
118         observable.removeStaleListener(childListener);
119         boolean newStale = staleCount > 0;
120         if (newStale != oldStale) {
121             stalenessConsumer.setStale(newStale);
122         }
123     }
124
125 }
126
Popular Tags