1 11 12 package org.eclipse.jface.internal.databinding.provisional.observable.list; 13 14 import java.util.AbstractList ; 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 20 import org.eclipse.jface.internal.databinding.provisional.observable.IChangeListener; 21 import org.eclipse.jface.internal.databinding.provisional.observable.IStaleListener; 22 import org.eclipse.jface.internal.databinding.provisional.observable.ObservableTracker; 23 24 30 public abstract class AbstractObservableList extends AbstractList implements 31 IObservableList { 32 33 private Object listChangeListeners; 34 35 39 private Object changeListeners = null; 40 41 45 private Object staleListeners = null; 46 47 public boolean isStale() { 48 return false; 49 } 50 51 public void addListChangeListener(IListChangeListener listener) { 52 if (listChangeListeners == null) { 53 boolean hadListeners = hasListeners(); 54 listChangeListeners = listener; 55 if (!hadListeners) { 56 firstListenerAdded(); 57 } 58 return; 59 } 60 61 Collection listenerList; 62 if (listChangeListeners instanceof Collection ) { 63 listenerList = (Collection ) listChangeListeners; 64 } else { 65 IListChangeListener l = (IListChangeListener) listChangeListeners; 66 67 listenerList = new ArrayList (); 68 listenerList.add(l); 69 listChangeListeners = listenerList; 70 } 71 72 if (listenerList.size() > 16) { 73 listenerList = new HashSet (listenerList); 74 listChangeListeners = listenerList; 75 } 76 77 listenerList.add(listener); 78 } 79 80 public void removeListChangeListener(IListChangeListener listener) { 81 82 if (listChangeListeners == listener) { 83 listChangeListeners = null; 84 if (!hasListeners()) { 85 lastListenerRemoved(); 86 } 87 return; 88 } 89 90 if (listChangeListeners instanceof Collection ) { 91 Collection listenerList = (Collection ) listChangeListeners; 92 listenerList.remove(listener); 93 if (listenerList.size() == 0) { 94 listChangeListeners = null; 95 if (!hasListeners()) { 96 lastListenerRemoved(); 97 } 98 } 99 } 100 } 101 102 protected void fireListChange(ListDiff diff) { 103 fireChange(); 105 106 if (listChangeListeners == null) { 107 return; 108 } 109 110 if (listChangeListeners instanceof IListChangeListener) { 111 ((IListChangeListener) listChangeListeners).handleListChange(this, 112 diff); 113 return; 114 } 115 116 Collection changeListenerCollection = (Collection ) listChangeListeners; 117 118 IListChangeListener[] listeners = (IListChangeListener[]) (changeListenerCollection) 119 .toArray(new IListChangeListener[changeListenerCollection 120 .size()]); 121 for (int i = 0; i < listeners.length; i++) { 122 listeners[i].handleListChange(this, diff); 123 } 124 } 125 126 public void addChangeListener(IChangeListener listener) { 127 if (changeListeners == null) { 128 boolean hadListeners = hasListeners(); 129 changeListeners = listener; 130 if (!hadListeners) { 131 firstListenerAdded(); 132 } 133 return; 134 } 135 136 Collection listenerList; 137 if (changeListeners instanceof IChangeListener) { 138 IChangeListener l = (IChangeListener) changeListeners; 139 140 listenerList = new ArrayList (); 141 listenerList.add(l); 142 } else { 143 listenerList = (Collection ) changeListeners; 144 } 145 146 if (listenerList.size() > 16) { 147 HashSet listenerSet = new HashSet (); 148 listenerSet.addAll(listenerList); 149 changeListeners = listenerList; 150 } 151 152 listenerList.add(listener); 153 } 154 155 public void removeChangeListener(IChangeListener listener) { 156 if (changeListeners == listener) { 157 changeListeners = null; 158 if (!hasListeners()) { 159 lastListenerRemoved(); 160 } 161 return; 162 } 163 164 if (changeListeners instanceof Collection ) { 165 Collection listenerList = (Collection ) changeListeners; 166 listenerList.remove(listener); 167 if (listenerList.size() == 0) { 168 changeListeners = null; 169 if (!hasListeners()) { 170 lastListenerRemoved(); 171 } 172 } 173 } 174 } 175 176 public void addStaleListener(IStaleListener listener) { 177 if (staleListeners == null) { 178 boolean hadListeners = hasListeners(); 179 staleListeners = listener; 180 if (!hadListeners) { 181 firstListenerAdded(); 182 } 183 return; 184 } 185 186 Collection listenerList; 187 if (staleListeners instanceof IStaleListener) { 188 IStaleListener l = (IStaleListener) staleListeners; 189 190 listenerList = new ArrayList (); 191 listenerList.add(l); 192 } else { 193 listenerList = (Collection ) staleListeners; 194 } 195 196 if (listenerList.size() > 16) { 197 HashSet listenerSet = new HashSet (); 198 listenerSet.addAll(listenerList); 199 staleListeners = listenerList; 200 } 201 202 listenerList.add(listener); 203 } 204 205 public void removeStaleListener(IStaleListener listener) { 206 if (staleListeners == listener) { 207 staleListeners = null; 208 if (!hasListeners()) { 209 lastListenerRemoved(); 210 } 211 return; 212 } 213 214 if (staleListeners instanceof Collection ) { 215 Collection listenerList = (Collection ) staleListeners; 216 listenerList.remove(listener); 217 if (listenerList.size() == 0) { 218 staleListeners = null; 219 if (!hasListeners()) { 220 lastListenerRemoved(); 221 } 222 } 223 } 224 } 225 226 protected void fireChange() { 227 if (changeListeners == null) { 228 return; 229 } 230 231 if (changeListeners instanceof IChangeListener) { 232 ((IChangeListener) changeListeners).handleChange(this); 233 return; 234 } 235 236 Collection changeListenerCollection = (Collection ) changeListeners; 237 238 IChangeListener[] listeners = (IChangeListener[]) (changeListenerCollection) 239 .toArray(new IChangeListener[changeListenerCollection.size()]); 240 for (int i = 0; i < listeners.length; i++) { 241 listeners[i].handleChange(this); 242 } 243 } 244 245 protected void fireStale() { 246 if (staleListeners == null) { 247 return; 248 } 249 250 if (staleListeners instanceof IChangeListener) { 251 ((IChangeListener) staleListeners).handleChange(this); 252 return; 253 } 254 255 Collection changeListenerCollection = (Collection ) staleListeners; 256 257 IChangeListener[] listeners = (IChangeListener[]) (changeListenerCollection) 258 .toArray(new IChangeListener[changeListenerCollection.size()]); 259 for (int i = 0; i < listeners.length; i++) { 260 listeners[i].handleChange(this); 261 } 262 } 263 264 267 protected boolean hasListeners() { 268 return changeListeners != null || staleListeners != null 269 || listChangeListeners != null; 270 } 271 272 275 protected void firstListenerAdded() { 276 } 277 278 281 protected void lastListenerRemoved() { 282 } 283 284 287 public void dispose() { 288 listChangeListeners = null; 289 changeListeners = null; 290 staleListeners = null; 291 lastListenerRemoved(); 292 } 293 294 public final int size() { 295 getterCalled(); 296 return doGetSize(); 297 } 298 299 302 protected abstract int doGetSize(); 303 304 307 private void getterCalled() { 308 ObservableTracker.getterCalled(this); 309 } 310 311 public boolean isEmpty() { 312 getterCalled(); 313 return super.isEmpty(); 314 } 315 316 public boolean contains(Object o) { 317 getterCalled(); 318 return super.contains(o); 319 } 320 321 public Iterator iterator() { 322 final Iterator wrappedIterator = super.iterator(); 323 return new Iterator () { 324 public void remove() { 325 wrappedIterator.remove(); 326 } 327 328 public boolean hasNext() { 329 getterCalled(); 330 return wrappedIterator.hasNext(); 331 } 332 333 public Object next() { 334 getterCalled(); 335 return wrappedIterator.next(); 336 } 337 }; 338 } 339 340 public Object [] toArray() { 341 getterCalled(); 342 return super.toArray(); 343 } 344 345 public Object [] toArray(Object a[]) { 346 getterCalled(); 347 return super.toArray(a); 348 } 349 350 352 public boolean add(Object o) { 353 getterCalled(); 354 return super.add(o); 355 } 356 357 public boolean remove(Object o) { 358 getterCalled(); 359 return super.remove(o); 360 } 361 362 364 public boolean containsAll(Collection c) { 365 getterCalled(); 366 return super.containsAll(c); 367 } 368 369 public boolean addAll(Collection c) { 370 getterCalled(); 371 return super.addAll(c); 372 } 373 374 public boolean addAll(int index, Collection c) { 375 getterCalled(); 376 return super.addAll(c); 377 } 378 379 public boolean removeAll(Collection c) { 380 getterCalled(); 381 return super.removeAll(c); 382 } 383 384 public boolean retainAll(Collection c) { 385 getterCalled(); 386 return super.retainAll(c); 387 } 388 389 391 public boolean equals(Object o) { 392 getterCalled(); 393 return super.equals(o); 394 } 395 396 public int hashCode() { 397 getterCalled(); 398 return super.hashCode(); 399 } 400 401 public int indexOf(Object o) { 402 getterCalled(); 403 return super.indexOf(o); 404 } 405 406 public int lastIndexOf(Object o) { 407 getterCalled(); 408 return super.lastIndexOf(o); 409 } 410 411 } 412 | Popular Tags |