1 11 12 package org.eclipse.jface.internal.databinding.provisional.observable.set; 13 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs; 22 import org.eclipse.jface.internal.databinding.provisional.observable.IObservable; 23 import org.eclipse.jface.internal.databinding.provisional.observable.mapping.IMappingChangeListener; 24 import org.eclipse.jface.internal.databinding.provisional.observable.mapping.IObservableMapping; 25 import org.eclipse.jface.internal.databinding.provisional.observable.mapping.MappingDiff; 26 27 31 public class MappedSet extends ObservableSet { 32 33 private final IObservableMapping wrappedMapping; 34 35 38 private Map valueCounts = new HashMap (); 39 40 private ISetChangeListener domainListener = new ISetChangeListener() { 41 public void handleSetChange(IObservableSet source, SetDiff diff) { 42 Set additions = new HashSet (); 43 for (Iterator it = diff.getAdditions().iterator(); it.hasNext();) { 44 Object added = it.next(); 45 Object mappingValue = wrappedMapping.getMappingValue(added); 46 if (handleAddition(mappingValue)) { 47 additions.add(mappingValue); 48 } 49 } 50 Set removals = new HashSet (); 51 for (Iterator it = diff.getRemovals().iterator(); it.hasNext();) { 52 Object removed = it.next(); 53 Object mappingValue = wrappedMapping.getMappingValue(removed); 54 if (handleRemoval(mappingValue)) { 55 removals.add(mappingValue); 56 } 57 } 58 fireSetChange(Diffs.createSetDiff(additions, removals)); 59 } 60 }; 61 62 private IMappingChangeListener mappingChangeListener = new IMappingChangeListener() { 63 public void handleMappingValueChange(IObservable source, 64 MappingDiff diff) { 65 Set affectedElements = diff.getElements(); 66 Set additions = new HashSet (); 67 Set removals = new HashSet (); 68 for (Iterator it = affectedElements.iterator(); it.hasNext();) { 69 Object element = it.next(); 70 Object oldFunctionValue = diff.getOldMappingValues(element, 71 new int[] { 0 })[0]; 72 Object newFunctionValue = diff.getNewMappingValues(element, 73 new int[] { 0 }); 74 if (handleRemoval(oldFunctionValue)) { 75 removals.add(oldFunctionValue); 76 } 77 if (handleAddition(newFunctionValue)) { 78 additions.add(newFunctionValue); 79 } 80 } 81 fireSetChange(Diffs.createSetDiff(additions, removals)); 82 } 83 }; 84 85 private IObservableSet input; 86 87 91 public MappedSet(IObservableSet input, IObservableMapping mapping) { 92 super(Collections.EMPTY_SET, mapping.getValueType()); 93 setWrappedSet(valueCounts.keySet()); 94 this.wrappedMapping = mapping; 95 this.input = input; 96 for (Iterator it = input.iterator(); it.hasNext();) { 97 Object element = it.next(); 98 Object functionValue = wrappedMapping.getMappingValue(element); 99 handleAddition(functionValue); 100 } 101 input.addSetChangeListener(domainListener); 102 mapping.addMappingChangeListener(mappingChangeListener); 103 } 104 105 109 protected boolean handleAddition(Object mappingValue) { 110 Integer count = (Integer ) valueCounts.get(mappingValue); 111 if (count == null) { 112 valueCounts.put(mappingValue, new Integer (1)); 113 return true; 114 } 115 valueCounts.put(mappingValue, new Integer (count.intValue() + 1)); 116 return false; 117 } 118 119 123 protected boolean handleRemoval(Object mappingValue) { 124 Integer count = (Integer ) valueCounts.get(mappingValue); 125 if (count.intValue() <= 1) { 126 valueCounts.remove(mappingValue); 127 return true; 128 } 129 valueCounts.put(mappingValue, new Integer (count.intValue() - 1)); 130 return false; 131 } 132 133 public void dispose() { 134 wrappedMapping.removeMappingChangeListener(mappingChangeListener); 135 input.removeSetChangeListener(domainListener); 136 } 137 138 public Object getElementType() { 139 return wrappedMapping.getValueType(); 140 } 141 142 } 143 | Popular Tags |