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