KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > 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.core.internal.databinding.observable;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.databinding.observable.ChangeEvent;
18 import org.eclipse.core.databinding.observable.IChangeListener;
19 import org.eclipse.core.databinding.observable.IObservable;
20 import org.eclipse.core.databinding.observable.IStaleListener;
21 import org.eclipse.core.databinding.observable.StaleEvent;
22 import org.eclipse.core.internal.databinding.IdentityWrapper;
23
24 /**
25  * @since 1.0
26  *
27  */

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

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

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

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

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

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