1 13 14 package org.eclipse.core.databinding.observable.list; 15 16 import java.util.Collection ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.ListIterator ; 20 21 import org.eclipse.core.databinding.observable.AbstractObservable; 22 import org.eclipse.core.databinding.observable.Diffs; 23 import org.eclipse.core.databinding.observable.ObservableTracker; 24 import org.eclipse.core.databinding.observable.Realm; 25 26 37 public abstract class ObservableList extends AbstractObservable implements 38 IObservableList { 39 40 protected List wrappedList; 41 42 45 private boolean stale = false; 46 47 private Object elementType; 48 49 protected ObservableList(List wrappedList, Object elementType) { 50 this(Realm.getDefault(), wrappedList, elementType); 51 } 52 53 protected ObservableList(Realm realm, List wrappedList, Object elementType) { 54 super(realm); 55 this.wrappedList = wrappedList; 56 this.elementType = elementType; 57 } 58 59 public synchronized void addListChangeListener(IListChangeListener listener) { 60 addListener(ListChangeEvent.TYPE, listener); 61 } 62 63 public synchronized void removeListChangeListener(IListChangeListener listener) { 64 removeListener(ListChangeEvent.TYPE, listener); 65 } 66 67 protected void fireListChange(ListDiff diff) { 68 super.fireChange(); 70 fireEvent(new ListChangeEvent(this, diff)); 71 } 72 73 public boolean contains(Object o) { 74 getterCalled(); 75 return wrappedList.contains(o); 76 } 77 78 public boolean containsAll(Collection c) { 79 getterCalled(); 80 return wrappedList.containsAll(c); 81 } 82 83 public boolean equals(Object o) { 84 getterCalled(); 85 return wrappedList.equals(o); 86 } 87 88 public int hashCode() { 89 getterCalled(); 90 return wrappedList.hashCode(); 91 } 92 93 public boolean isEmpty() { 94 getterCalled(); 95 return wrappedList.isEmpty(); 96 } 97 98 public Iterator iterator() { 99 getterCalled(); 100 final Iterator wrappedIterator = wrappedList.iterator(); 101 return new Iterator () { 102 103 public void remove() { 104 throw new UnsupportedOperationException (); 105 } 106 107 public boolean hasNext() { 108 return wrappedIterator.hasNext(); 109 } 110 111 public Object next() { 112 return wrappedIterator.next(); 113 } 114 }; 115 } 116 117 public int size() { 118 getterCalled(); 119 return wrappedList.size(); 120 } 121 122 public Object [] toArray() { 123 getterCalled(); 124 return wrappedList.toArray(); 125 } 126 127 public Object [] toArray(Object [] a) { 128 getterCalled(); 129 return wrappedList.toArray(a); 130 } 131 132 public String toString() { 133 getterCalled(); 134 return wrappedList.toString(); 135 } 136 137 140 public Object get(int index) { 141 getterCalled(); 142 return wrappedList.get(index); 143 } 144 145 148 public int indexOf(Object o) { 149 getterCalled(); 150 return wrappedList.indexOf(o); 151 } 152 153 156 public int lastIndexOf(Object o) { 157 getterCalled(); 158 return wrappedList.lastIndexOf(o); 159 } 160 161 163 166 public ListIterator listIterator() { 167 return listIterator(0); 168 } 169 170 173 public ListIterator listIterator(int index) { 174 getterCalled(); 175 final ListIterator wrappedIterator = wrappedList.listIterator(index); 176 return new ListIterator () { 177 178 public int nextIndex() { 179 return wrappedIterator.nextIndex(); 180 } 181 182 public int previousIndex() { 183 return wrappedIterator.previousIndex(); 184 } 185 186 public void remove() { 187 throw new UnsupportedOperationException (); 188 } 189 190 public boolean hasNext() { 191 return wrappedIterator.hasNext(); 192 } 193 194 public boolean hasPrevious() { 195 return wrappedIterator.hasPrevious(); 196 } 197 198 public Object next() { 199 return wrappedIterator.next(); 200 } 201 202 public Object previous() { 203 return wrappedIterator.previous(); 204 } 205 206 public void add(Object o) { 207 throw new UnsupportedOperationException (); 208 } 209 210 public void set(Object o) { 211 throw new UnsupportedOperationException (); 212 } 213 }; 214 } 215 216 217 public List subList(int fromIndex, int toIndex) { 218 getterCalled(); 219 return wrappedList.subList(fromIndex, toIndex); 220 } 221 222 protected void getterCalled() { 223 ObservableTracker.getterCalled(this); 224 } 225 226 public Object set(int index, Object element) { 227 throw new UnsupportedOperationException (); 228 } 229 230 public Object remove(int index) { 231 throw new UnsupportedOperationException (); 232 } 233 234 public boolean add(Object o) { 235 throw new UnsupportedOperationException (); 236 } 237 238 public void add(int index, Object element) { 239 throw new UnsupportedOperationException (); 240 } 241 242 public boolean addAll(Collection c) { 243 throw new UnsupportedOperationException (); 244 } 245 246 public boolean addAll(int index, Collection c) { 247 throw new UnsupportedOperationException (); 248 } 249 250 public boolean remove(Object o) { 251 throw new UnsupportedOperationException (); 252 } 253 254 public boolean removeAll(Collection c) { 255 throw new UnsupportedOperationException (); 256 } 257 258 public boolean retainAll(Collection c) { 259 throw new UnsupportedOperationException (); 260 } 261 262 public void clear() { 263 throw new UnsupportedOperationException (); 264 } 265 266 271 public boolean isStale() { 272 checkRealm(); 273 return stale; 274 } 275 276 284 public void setStale(boolean stale) { 285 checkRealm(); 286 287 boolean wasStale = this.stale; 288 this.stale = stale; 289 if (!wasStale && stale) { 290 fireStale(); 291 } 292 } 293 294 protected void fireChange() { 295 throw new RuntimeException ("fireChange should not be called, use fireListChange() instead"); } 297 298 301 public synchronized void dispose() { 302 super.dispose(); 303 } 304 305 public Object getElementType() { 306 return elementType; 307 } 308 309 protected void updateWrappedList(List newList) { 310 List oldList = wrappedList; 311 ListDiff listDiff = Diffs.computeListDiff(oldList, newList); 312 wrappedList = newList; 313 fireListChange(listDiff); 314 } 315 316 } 317 | Popular Tags |