1 11 12 package org.eclipse.jface.internal.databinding.provisional.observable.list; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.ListIterator ; 20 21 import org.eclipse.jface.internal.databinding.provisional.observable.AbstractObservable; 22 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs; 23 import org.eclipse.jface.internal.databinding.provisional.observable.ObservableTracker; 24 25 32 public abstract class ObservableList extends AbstractObservable implements 33 IObservableList { 34 35 protected List wrappedList; 36 37 private boolean stale = false; 38 39 private Object listChangeListeners; 40 41 private Object elementType; 42 43 protected ObservableList(List wrappedList, Object elementType) { 44 this.wrappedList = wrappedList; 45 this.elementType = elementType; 46 } 47 48 public void addListChangeListener(IListChangeListener listener) { 49 if (listChangeListeners == null) { 50 boolean hadListeners = hasListeners(); 51 listChangeListeners = listener; 52 if (!hadListeners) { 53 firstListenerAdded(); 54 } 55 return; 56 } 57 58 Collection listenerList; 59 if (listChangeListeners instanceof Collection ) { 60 listenerList = (Collection ) listChangeListeners; 61 } else { 62 IListChangeListener l = (IListChangeListener) listChangeListeners; 63 64 listenerList = new ArrayList (); 65 listenerList.add(l); 66 listChangeListeners = listenerList; 67 } 68 69 if (listenerList.size() > 16) { 70 listenerList = new HashSet (listenerList); 71 listChangeListeners = listenerList; 72 } 73 74 listenerList.add(listener); 75 } 76 77 public void removeListChangeListener(IListChangeListener listener) { 78 79 if (listChangeListeners == listener) { 80 listChangeListeners = null; 81 if (!hasListeners()) { 82 lastListenerRemoved(); 83 } 84 return; 85 } 86 87 if (listChangeListeners instanceof Collection ) { 88 Collection listenerList = (Collection ) listChangeListeners; 89 listenerList.remove(listener); 90 if (listenerList.size() == 0) { 91 listChangeListeners = null; 92 if (!hasListeners()) { 93 lastListenerRemoved(); 94 } 95 } 96 } 97 } 98 99 protected boolean hasListeners() { 100 return super.hasListeners() || listChangeListeners!=null; 101 } 102 103 protected void fireListChange(ListDiff diff) { 104 super.fireChange(); 106 107 if (listChangeListeners == null) { 108 return; 109 } 110 111 if (listChangeListeners instanceof IListChangeListener) { 112 ((IListChangeListener) listChangeListeners).handleListChange(this, diff); 113 return; 114 } 115 116 Collection changeListenerCollection = (Collection ) listChangeListeners; 117 118 IListChangeListener[] listeners = (IListChangeListener[]) (changeListenerCollection) 119 .toArray(new IListChangeListener[changeListenerCollection.size()]); 120 for (int i = 0; i < listeners.length; i++) { 121 listeners[i].handleListChange(this, diff); 122 } 123 } 124 125 public boolean contains(Object o) { 126 getterCalled(); 127 return wrappedList.contains(o); 128 } 129 130 public boolean containsAll(Collection c) { 131 getterCalled(); 132 return wrappedList.containsAll(c); 133 } 134 135 public boolean equals(Object o) { 136 getterCalled(); 137 return wrappedList.equals(o); 138 } 139 140 public int hashCode() { 141 getterCalled(); 142 return wrappedList.hashCode(); 143 } 144 145 public boolean isEmpty() { 146 getterCalled(); 147 return wrappedList.isEmpty(); 148 } 149 150 public Iterator iterator() { 151 final Iterator wrappedIterator = wrappedList.iterator(); 152 return new Iterator () { 153 154 public void remove() { 155 throw new UnsupportedOperationException (); 156 } 157 158 public boolean hasNext() { 159 ObservableTracker.getterCalled(ObservableList.this); 160 return wrappedIterator.hasNext(); 161 } 162 163 public Object next() { 164 ObservableTracker.getterCalled(ObservableList.this); 165 return wrappedIterator.next(); 166 } 167 }; 168 } 169 170 public int size() { 171 getterCalled(); 172 return wrappedList.size(); 173 } 174 175 public Object [] toArray() { 176 getterCalled(); 177 return wrappedList.toArray(); 178 } 179 180 public Object [] toArray(Object [] a) { 181 getterCalled(); 182 return wrappedList.toArray(a); 183 } 184 185 public String toString() { 186 getterCalled(); 187 return wrappedList.toString(); 188 } 189 190 193 public Object get(int index) { 194 getterCalled(); 195 return wrappedList.get(index); 196 } 197 198 201 public int indexOf(Object o) { 202 getterCalled(); 203 return wrappedList.indexOf(o); 204 } 205 206 209 public int lastIndexOf(Object o) { 210 getterCalled(); 211 return wrappedList.lastIndexOf(o); 212 } 213 214 216 219 public ListIterator listIterator() { 220 return listIterator(0); 221 } 222 223 226 public ListIterator listIterator(int index) { 227 final ListIterator wrappedIterator = wrappedList.listIterator(index); 228 return new ListIterator () { 229 230 public int nextIndex() { 231 getterCalled(); 232 return wrappedIterator.nextIndex(); 233 } 234 235 public int previousIndex() { 236 getterCalled(); 237 return wrappedIterator.previousIndex(); 238 } 239 240 public void remove() { 241 throw new UnsupportedOperationException (); 242 } 243 244 public boolean hasNext() { 245 getterCalled(); 246 return wrappedIterator.hasNext(); 247 } 248 249 public boolean hasPrevious() { 250 getterCalled(); 251 return wrappedIterator.hasPrevious(); 252 } 253 254 public Object next() { 255 getterCalled(); 256 return wrappedIterator.next(); 257 } 258 259 public Object previous() { 260 getterCalled(); 261 return wrappedIterator.previous(); 262 } 263 264 public void add(Object o) { 265 throw new UnsupportedOperationException (); 266 } 267 268 public void set(Object o) { 269 throw new UnsupportedOperationException (); 270 } 271 }; 272 } 273 274 275 public List subList(int fromIndex, int toIndex) { 276 getterCalled(); 277 return wrappedList.subList(fromIndex, toIndex); 278 } 279 280 protected void getterCalled() { 281 ObservableTracker.getterCalled(this); 282 } 283 284 public Object set(int index, Object element) { 285 throw new UnsupportedOperationException (); 286 } 287 288 public Object remove(int index) { 289 throw new UnsupportedOperationException (); 290 } 291 292 public boolean add(Object o) { 293 throw new UnsupportedOperationException (); 294 } 295 296 public void add(int index, Object element) { 297 throw new UnsupportedOperationException (); 298 } 299 300 public boolean addAll(Collection c) { 301 throw new UnsupportedOperationException (); 302 } 303 304 public boolean addAll(int index, Collection c) { 305 throw new UnsupportedOperationException (); 306 } 307 308 public boolean remove(Object o) { 309 throw new UnsupportedOperationException (); 310 } 311 312 public boolean removeAll(Collection c) { 313 throw new UnsupportedOperationException (); 314 } 315 316 public boolean retainAll(Collection c) { 317 throw new UnsupportedOperationException (); 318 } 319 320 public void clear() { 321 throw new UnsupportedOperationException (); 322 } 323 324 327 public boolean isStale() { 328 return stale; 329 } 330 331 337 public void setStale(boolean stale) { 338 boolean wasStale = this.stale; 339 this.stale = stale; 340 if (!wasStale && stale) { 341 fireStale(); 342 } 343 } 344 345 protected void fireChange() { 346 throw new RuntimeException ("fireChange should not be called, use fireListChange() instead"); } 348 349 352 public void dispose() { 353 listChangeListeners = null; 354 super.dispose(); 355 } 356 357 public Object getElementType() { 358 return elementType; 359 } 360 361 protected void updateWrappedList(List newList) { 362 List oldList = wrappedList; 365 ListDiff listDiff = Diffs.computeListDiff(oldList, newList); 366 wrappedList = newList; 367 fireListChange(listDiff); 368 } 369 370 } 371 | Popular Tags |