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