1 11 package org.eclipse.core.databinding.observable; 12 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Set ; 16 17 import org.eclipse.core.internal.databinding.IdentityWrapper; 18 import org.eclipse.core.runtime.Assert; 19 20 58 public class ObservableTracker { 59 60 66 private static ThreadLocal currentChangeListener = new ThreadLocal (); 67 68 private static ThreadLocal currentStaleListener = new ThreadLocal (); 69 70 private static ThreadLocal currentObservableSet = new ThreadLocal (); 71 72 87 public static IObservable[] runAndMonitor(Runnable runnable, 88 IChangeListener changeListener, IStaleListener staleListener) { 89 Set lastObservableSet = (Set ) currentObservableSet.get(); 91 IChangeListener lastChangeListener = (IChangeListener) currentChangeListener 92 .get(); 93 IStaleListener lastStaleListener = (IStaleListener) currentStaleListener 94 .get(); 95 96 Set observableSet = new HashSet (); 97 currentObservableSet.set(observableSet); 99 currentChangeListener.set(changeListener); 100 currentStaleListener.set(staleListener); 101 try { 102 runnable.run(); 103 } finally { 104 currentObservableSet.set(lastObservableSet); 107 currentChangeListener.set(lastChangeListener); 108 currentStaleListener.set(lastStaleListener); 109 } 110 111 int i = 0; 112 IObservable[] result = new IObservable[observableSet.size()]; 113 for (Iterator it = observableSet.iterator(); it.hasNext();) { 114 IdentityWrapper wrapper = (IdentityWrapper) it.next(); 115 result[i++] = (IObservable) wrapper.unwrap(); 116 } 117 118 return result; 119 } 120 121 131 public static void getterCalled(IObservable observable) { 132 Assert.isTrue(observable.getRealm().isCurrent()); 133 Set lastObservableSet = (Set ) currentObservableSet.get(); 134 IChangeListener lastChangeListener = (IChangeListener) currentChangeListener 135 .get(); 136 IStaleListener lastStaleListener = (IStaleListener) currentStaleListener 137 .get(); 138 139 boolean added = false; 140 if (lastObservableSet != null) { 141 added = lastObservableSet.add(new IdentityWrapper(observable)); 142 } 143 144 if (added && lastChangeListener != null) { 146 observable.addChangeListener(lastChangeListener); 147 } 148 if (added && lastStaleListener != null) { 149 observable.addStaleListener(lastStaleListener); 150 } 151 } 152 } 153 | Popular Tags |