1 11 12 package org.eclipse.core.internal.databinding.observable; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 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 28 public class StalenessTracker { 29 30 private Map staleMap = new HashMap (); 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 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 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 88 private boolean getOldChildStale(IdentityWrapper wrappedChild) { 89 Object oldChildValue = staleMap.get(wrappedChild); 90 boolean oldChildStale = oldChildValue == null ? false 91 : ((Boolean ) oldChildValue).booleanValue(); 92 return oldChildStale; 93 } 94 95 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 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 |