|
Added
Link Here
|
| 1 |
/** |
| 2 |
* <copyright> |
| 3 |
* |
| 4 |
* Copyright (c) 2009 BestSolution.at and others. |
| 5 |
* All rights reserved. This program and the accompanying materials |
| 6 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
* which accompanies this distribution, and is available at |
| 8 |
* http://www.eclipse.org/legal/epl-v10.html |
| 9 |
* |
| 10 |
* Contributors: |
| 11 |
* Tom Schindl<tom.schindl@bestsolution.at> - Initial API and implementation in 262160 |
| 12 |
* </copyright> |
| 13 |
* |
| 14 |
* $Id: $ |
| 15 |
*/ |
| 16 |
package org.eclipse.emf.databinding.internal; |
| 17 |
|
| 18 |
import java.util.Collection; |
| 19 |
import java.util.Iterator; |
| 20 |
import java.util.List; |
| 21 |
import java.util.ListIterator; |
| 22 |
|
| 23 |
import org.eclipse.core.databinding.observable.Diffs; |
| 24 |
import org.eclipse.core.databinding.observable.ObservableTracker; |
| 25 |
import org.eclipse.core.databinding.observable.Realm; |
| 26 |
import org.eclipse.core.databinding.observable.list.AbstractObservableList; |
| 27 |
import org.eclipse.core.databinding.observable.list.IObservableList; |
| 28 |
import org.eclipse.core.databinding.observable.list.ListDiff; |
| 29 |
import org.eclipse.core.databinding.observable.list.ListDiffEntry; |
| 30 |
import org.eclipse.emf.common.notify.Adapter; |
| 31 |
import org.eclipse.emf.common.notify.Notification; |
| 32 |
import org.eclipse.emf.common.notify.Notifier; |
| 33 |
import org.eclipse.emf.common.notify.NotifyingList; |
| 34 |
import org.eclipse.emf.common.notify.impl.AdapterImpl; |
| 35 |
import org.eclipse.emf.ecore.resource.Resource; |
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* Writable list which can be used to observe an {@link NotifyingList} |
| 40 |
* |
| 41 |
* @param <Type> the type |
| 42 |
* @since 2.5 |
| 43 |
*/ |
| 44 |
public class EWritableList<Type> extends AbstractObservableList implements IObservableList |
| 45 |
{ |
| 46 |
private NotifyingList<Type> wrappedList; |
| 47 |
private Object elementType; |
| 48 |
private boolean stale = false; |
| 49 |
|
| 50 |
private class Listener extends AdapterImpl |
| 51 |
{ |
| 52 |
private Object feature; |
| 53 |
|
| 54 |
public Listener(Object feature) |
| 55 |
{ |
| 56 |
this.feature = feature; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public void notifyChanged(Notification msg) |
| 61 |
{ |
| 62 |
|
| 63 |
if (feature == null && msg.getFeature() == null && msg.getFeatureID(Resource.class) != Resource.RESOURCE__CONTENTS) |
| 64 |
{ |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
if (feature == msg.getFeature() && !msg.isTouch()) |
| 69 |
{ |
| 70 |
final ListDiff diff; |
| 71 |
switch (msg.getEventType()) |
| 72 |
{ |
| 73 |
case Notification.ADD: { |
| 74 |
diff = Diffs.createListDiff(Diffs.createListDiffEntry(msg.getPosition(), true, msg.getNewValue())); |
| 75 |
// fireListChange(diff); |
| 76 |
break; |
| 77 |
} |
| 78 |
case Notification.ADD_MANY: { |
| 79 |
Collection< ? > newValues = (Collection< ? >)msg.getNewValue(); |
| 80 |
ListDiffEntry[] listDiffEntries = new ListDiffEntry [newValues.size()]; |
| 81 |
int position = msg.getPosition(); |
| 82 |
int index = 0; |
| 83 |
for (Object newValue : newValues) |
| 84 |
{ |
| 85 |
listDiffEntries[index++] = Diffs.createListDiffEntry(position++, true, newValue); |
| 86 |
} |
| 87 |
diff = Diffs.createListDiff(listDiffEntries); |
| 88 |
// fireListChange(diff); |
| 89 |
break; |
| 90 |
} |
| 91 |
case Notification.REMOVE: { |
| 92 |
diff = Diffs.createListDiff(Diffs.createListDiffEntry(msg.getPosition(), false, msg.getOldValue())); |
| 93 |
// fireListChange(diff); |
| 94 |
break; |
| 95 |
} |
| 96 |
case Notification.REMOVE_MANY: { |
| 97 |
Collection< ? > oldValues = (Collection< ? >)msg.getOldValue(); |
| 98 |
ListDiffEntry[] listDiffEntries = new ListDiffEntry [oldValues.size()]; |
| 99 |
int position = msg.getPosition(); |
| 100 |
int index = 0; |
| 101 |
for (Object oldValue : oldValues) |
| 102 |
{ |
| 103 |
listDiffEntries[index++] = Diffs.createListDiffEntry(position++, false, oldValue); |
| 104 |
} |
| 105 |
diff = Diffs.createListDiff(listDiffEntries); |
| 106 |
// fireListChange(diff); |
| 107 |
break; |
| 108 |
} |
| 109 |
case Notification.MOVE: { |
| 110 |
Object movedValue = msg.getNewValue(); |
| 111 |
ListDiffEntry[] listDiffEntries = new ListDiffEntry [2]; |
| 112 |
listDiffEntries[0] = Diffs.createListDiffEntry((Integer)msg.getOldValue(), false, movedValue); |
| 113 |
listDiffEntries[1] = Diffs.createListDiffEntry(msg.getPosition(), true, movedValue); |
| 114 |
diff = Diffs.createListDiff(listDiffEntries); |
| 115 |
// fireListChange(diff); |
| 116 |
break; |
| 117 |
} |
| 118 |
case Notification.UNSET: { |
| 119 |
// This just represents going back to the unset state, but |
| 120 |
// that doesn't affect the contents of the list. |
| 121 |
// |
| 122 |
return; |
| 123 |
} |
| 124 |
default: { |
| 125 |
throw new RuntimeException("unhandled case"); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
getRealm().exec(new Runnable() |
| 130 |
{ |
| 131 |
public void run() |
| 132 |
{ |
| 133 |
fireListChange(diff); |
| 134 |
} |
| 135 |
}); |
| 136 |
|
| 137 |
// System.err.println("CHANGE: " + |
| 138 |
// diff.getDifferences()[0].getElement()); |
| 139 |
|
| 140 |
// fireListChange(diff); |
| 141 |
// listener.handlePropertyChange(new SimplePropertyEvent(msg |
| 142 |
// .getNotifier(), EMFListProperty.this, diff)); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
private Adapter listener; |
| 149 |
|
| 150 |
/** |
| 151 |
* New writable list wrapping the {@link NotifyingList} |
| 152 |
* |
| 153 |
* @param wrappedList |
| 154 |
* the wrapped list |
| 155 |
*/ |
| 156 |
public EWritableList(NotifyingList<Type> wrappedList) |
| 157 |
{ |
| 158 |
this(Realm.getDefault(), wrappedList); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* New writable list wrapping the {@link NotifyingList} and using the |
| 163 |
* {@link Realm} |
| 164 |
* |
| 165 |
* @param realm |
| 166 |
* the realm |
| 167 |
* @param wrappedList |
| 168 |
* the wrapped list |
| 169 |
*/ |
| 170 |
public EWritableList(Realm realm, NotifyingList<Type> wrappedList) |
| 171 |
{ |
| 172 |
this(realm, wrappedList, null); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* New writable list wrapping the {@link NotifyingList} |
| 177 |
* |
| 178 |
* @param realm |
| 179 |
* the realm |
| 180 |
* @param wrappedList |
| 181 |
* the wrapped list |
| 182 |
* @param elementType |
| 183 |
* the element type |
| 184 |
*/ |
| 185 |
public EWritableList(Realm realm, NotifyingList<Type> wrappedList, Class<Type> elementType) |
| 186 |
{ |
| 187 |
super(realm); |
| 188 |
this.wrappedList = wrappedList; |
| 189 |
this.elementType = elementType; |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
protected void firstListenerAdded() |
| 194 |
{ |
| 195 |
if (wrappedList.getNotifier() instanceof Notifier) |
| 196 |
{ |
| 197 |
Notifier notifier = (Notifier)wrappedList.getNotifier(); |
| 198 |
listener = new Listener(wrappedList.getFeature()); |
| 199 |
notifier.eAdapters().add(listener); |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
throw new IllegalArgumentException("Wrapped list must have a notifier attached!"); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
protected void lastListenerRemoved() |
| 209 |
{ |
| 210 |
if (wrappedList.getNotifier() instanceof Notifier) |
| 211 |
{ |
| 212 |
Notifier notifier = (Notifier)wrappedList.getNotifier(); |
| 213 |
listener = new Listener(wrappedList.getFeature()); |
| 214 |
notifier.eAdapters().remove(listener); |
| 215 |
} |
| 216 |
else |
| 217 |
{ |
| 218 |
throw new IllegalArgumentException("Wrapped list must have a notifier attached!"); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
@Override |
| 223 |
public synchronized void dispose() |
| 224 |
{ |
| 225 |
super.dispose(); |
| 226 |
} |
| 227 |
|
| 228 |
private void getterCalled() |
| 229 |
{ |
| 230 |
ObservableTracker.getterCalled(this); |
| 231 |
} |
| 232 |
|
| 233 |
@SuppressWarnings("unchecked") |
| 234 |
public boolean add(Object o) |
| 235 |
{ |
| 236 |
checkRealm(); |
| 237 |
return wrappedList.add((Type)o); |
| 238 |
} |
| 239 |
|
| 240 |
@SuppressWarnings("unchecked") |
| 241 |
public boolean addAll(Collection c) |
| 242 |
{ |
| 243 |
checkRealm(); |
| 244 |
return wrappedList.addAll(c); |
| 245 |
} |
| 246 |
|
| 247 |
@SuppressWarnings("unchecked") |
| 248 |
public boolean addAll(int index, Collection c) |
| 249 |
{ |
| 250 |
checkRealm(); |
| 251 |
return wrappedList.addAll(index, c); |
| 252 |
} |
| 253 |
|
| 254 |
public boolean contains(Object o) |
| 255 |
{ |
| 256 |
getterCalled(); |
| 257 |
return wrappedList.contains(o); |
| 258 |
} |
| 259 |
|
| 260 |
@SuppressWarnings("unchecked") |
| 261 |
public boolean containsAll(Collection c) |
| 262 |
{ |
| 263 |
getterCalled(); |
| 264 |
return wrappedList.containsAll(c); |
| 265 |
} |
| 266 |
|
| 267 |
public Object get(int index) |
| 268 |
{ |
| 269 |
getterCalled(); |
| 270 |
return wrappedList.get(index); |
| 271 |
} |
| 272 |
|
| 273 |
public Object getElementType() |
| 274 |
{ |
| 275 |
checkRealm(); |
| 276 |
return elementType; |
| 277 |
} |
| 278 |
|
| 279 |
public int indexOf(Object o) |
| 280 |
{ |
| 281 |
getterCalled(); |
| 282 |
return wrappedList.indexOf(o); |
| 283 |
} |
| 284 |
|
| 285 |
public boolean isEmpty() |
| 286 |
{ |
| 287 |
getterCalled(); |
| 288 |
return wrappedList.isEmpty(); |
| 289 |
} |
| 290 |
|
| 291 |
public Iterator<Type> iterator() |
| 292 |
{ |
| 293 |
getterCalled(); |
| 294 |
return wrappedList.iterator(); |
| 295 |
} |
| 296 |
|
| 297 |
public int lastIndexOf(Object o) |
| 298 |
{ |
| 299 |
getterCalled(); |
| 300 |
return wrappedList.lastIndexOf(o); |
| 301 |
} |
| 302 |
|
| 303 |
public ListIterator<Type> listIterator() |
| 304 |
{ |
| 305 |
getterCalled(); |
| 306 |
return wrappedList.listIterator(); |
| 307 |
} |
| 308 |
|
| 309 |
public ListIterator<Type> listIterator(int index) |
| 310 |
{ |
| 311 |
getterCalled(); |
| 312 |
return wrappedList.listIterator(index); |
| 313 |
} |
| 314 |
|
| 315 |
public Object move(int oldIndex, int newIndex) |
| 316 |
{ |
| 317 |
checkRealm(); |
| 318 |
return wrappedList.move(oldIndex, newIndex); |
| 319 |
} |
| 320 |
|
| 321 |
public boolean remove(Object o) |
| 322 |
{ |
| 323 |
checkRealm(); |
| 324 |
return wrappedList.remove(o); |
| 325 |
} |
| 326 |
|
| 327 |
public Object remove(int index) |
| 328 |
{ |
| 329 |
checkRealm(); |
| 330 |
return wrappedList.remove(index); |
| 331 |
} |
| 332 |
|
| 333 |
@SuppressWarnings("unchecked") |
| 334 |
public boolean removeAll(Collection c) |
| 335 |
{ |
| 336 |
checkRealm(); |
| 337 |
return wrappedList.removeAll(c); |
| 338 |
} |
| 339 |
|
| 340 |
@SuppressWarnings("unchecked") |
| 341 |
public boolean retainAll(Collection c) |
| 342 |
{ |
| 343 |
checkRealm(); |
| 344 |
return wrappedList.retainAll(c); |
| 345 |
} |
| 346 |
|
| 347 |
@SuppressWarnings("unchecked") |
| 348 |
public Object set(int index, Object element) |
| 349 |
{ |
| 350 |
checkRealm(); |
| 351 |
return wrappedList.set(index, (Type)element); |
| 352 |
} |
| 353 |
|
| 354 |
public int doGetSize() |
| 355 |
{ |
| 356 |
getterCalled(); |
| 357 |
return wrappedList.size(); |
| 358 |
} |
| 359 |
|
| 360 |
public List<Type> subList(int fromIndex, int toIndex) |
| 361 |
{ |
| 362 |
getterCalled(); |
| 363 |
return wrappedList.subList(fromIndex, toIndex); |
| 364 |
} |
| 365 |
|
| 366 |
public Object[] toArray() |
| 367 |
{ |
| 368 |
getterCalled(); |
| 369 |
return wrappedList.toArray(); |
| 370 |
} |
| 371 |
|
| 372 |
public Object[] toArray(Object[] a) |
| 373 |
{ |
| 374 |
getterCalled(); |
| 375 |
return wrappedList.toArray(); |
| 376 |
} |
| 377 |
|
| 378 |
@SuppressWarnings("unchecked") |
| 379 |
public void add(int index, Object element) |
| 380 |
{ |
| 381 |
checkRealm(); |
| 382 |
wrappedList.add(index, (Type)element); |
| 383 |
} |
| 384 |
|
| 385 |
public void clear() |
| 386 |
{ |
| 387 |
checkRealm(); |
| 388 |
wrappedList.clear(); |
| 389 |
} |
| 390 |
|
| 391 |
public boolean isStale() |
| 392 |
{ |
| 393 |
getterCalled(); |
| 394 |
return stale; |
| 395 |
} |
| 396 |
|
| 397 |
// public void setStale(boolean stale) { |
| 398 |
// checkRealm(); |
| 399 |
// |
| 400 |
// boolean wasStale = this.stale; |
| 401 |
// this.stale = stale; |
| 402 |
// if (!wasStale && stale) { |
| 403 |
// fireStale(); |
| 404 |
// } |
| 405 |
// } |
| 406 |
} |