1 11 12 package org.eclipse.core.databinding.observable.set; 13 14 import java.util.HashSet ; 15 import java.util.Set ; 16 17 import org.eclipse.core.databinding.observable.Diffs; 18 import org.eclipse.core.databinding.observable.Realm; 19 import org.eclipse.core.databinding.observable.list.IListChangeListener; 20 import org.eclipse.core.databinding.observable.list.IObservableList; 21 import org.eclipse.core.databinding.observable.list.ListChangeEvent; 22 import org.eclipse.core.databinding.observable.list.ListDiffEntry; 23 24 37 public class ListToSetAdapter extends ObservableSet { 38 39 private final IObservableList list; 40 41 private IListChangeListener listener = new IListChangeListener() { 42 43 public void handleListChange(ListChangeEvent event) { 44 Set added = new HashSet (); 45 Set removed = new HashSet (); 46 ListDiffEntry[] differences = event.diff.getDifferences(); 47 for (int i = 0; i < differences.length; i++) { 48 ListDiffEntry entry = differences[i]; 49 Object element = entry.getElement(); 50 if (entry.isAddition()) { 51 if (wrappedSet.add(element)) { 52 if (!removed.remove(element)) 53 added.add(element); 54 } 55 } else { 56 if (wrappedSet.remove(element)) { 57 removed.add(element); 58 added.remove(element); 59 } 60 } 61 } 62 fireSetChange(Diffs.createSetDiff(added, removed)); 63 } 64 }; 65 66 69 public ListToSetAdapter(IObservableList list) { 70 super(list.getRealm(), new HashSet (), list.getElementType()); 71 this.list = list; 72 wrappedSet.addAll(list); 73 this.list.addListChangeListener(listener); 74 } 75 76 public synchronized void dispose() { 77 super.dispose(); 78 if (list != null && listener != null) { 79 list.removeListChangeListener(listener); 80 listener = null; 81 } 82 } 83 84 } 85 | Popular Tags |