1 11 12 package org.eclipse.jface.internal.databinding.provisional.observable.mapping; 13 14 import java.util.Iterator ; 15 16 import org.eclipse.jface.internal.databinding.provisional.observable.IObservableCollection; 17 import org.eclipse.jface.internal.databinding.provisional.observable.list.IListChangeListener; 18 import org.eclipse.jface.internal.databinding.provisional.observable.list.IObservableList; 19 import org.eclipse.jface.internal.databinding.provisional.observable.list.ListDiff; 20 import org.eclipse.jface.internal.databinding.provisional.observable.list.ListDiffEntry; 21 import org.eclipse.jface.internal.databinding.provisional.observable.set.IObservableSet; 22 import org.eclipse.jface.internal.databinding.provisional.observable.set.ISetChangeListener; 23 import org.eclipse.jface.internal.databinding.provisional.observable.set.SetDiff; 24 25 29 abstract public class ObservableMultiMappingWithDomain extends 30 AbstractObservableMultiMapping implements 31 IObservableMultiMappingWithDomain { 32 33 private ISetChangeListener listener = new ISetChangeListener() { 34 public void handleSetChange(IObservableSet source, SetDiff diff) { 35 for (Iterator it = diff.getAdditions().iterator(); it.hasNext();) { 36 addListenerTo(it.next()); 37 } 38 for (Iterator it = diff.getRemovals().iterator(); it.hasNext();) { 39 removeListenerFrom(it.next()); 40 } 41 } 42 }; 43 44 private IListChangeListener listListener = new IListChangeListener() { 45 46 public void handleListChange(IObservableList source, ListDiff diff) { 47 ListDiffEntry[] entries = diff.getDifferences(); 48 for (int i = 0; i < entries.length; i++) { 49 ListDiffEntry entry = entries[i]; 50 if (entry.isAddition()) { 51 addListenerTo(entry.getElement()); 52 } else { 53 removeListenerFrom(entry.getElement()); 54 } 55 } 56 } 57 }; 58 59 private IObservableCollection domain; 60 61 64 public ObservableMultiMappingWithDomain() { 65 } 66 67 70 protected void initDomain(IObservableCollection domain) { 71 this.domain = domain; 72 if (domain instanceof IObservableList) { 73 IObservableList listDomain = (IObservableList) domain; 74 listDomain.addListChangeListener(listListener); 75 } else { 76 ((IObservableSet) domain).addSetChangeListener(listener); 77 } 78 for (Iterator it = getDomainIterator(domain); it.hasNext();) { 79 addListenerTo(it.next()); 80 } 81 } 82 83 private Iterator getDomainIterator(IObservableCollection domain) { 84 Iterator it; 85 if (domain instanceof IObservableList) { 86 IObservableList listDomain = (IObservableList) domain; 87 it = listDomain.iterator(); 88 } else { 89 IObservableSet setDomain = (IObservableSet) domain; 90 it = setDomain.iterator(); 91 } 92 return it; 93 } 94 95 98 public IObservableCollection getDomain() { 99 return domain; 100 } 101 102 105 protected abstract void addListenerTo(Object domainElement); 106 107 110 protected abstract void removeListenerFrom(Object domainElement); 111 112 public void dispose() { 113 for (Iterator iter = getDomainIterator(domain); iter.hasNext();) { 114 removeListenerFrom(iter.next()); 115 } 116 if (domain instanceof IObservableList) { 117 IObservableList listDomain = (IObservableList) domain; 118 listDomain.removeListChangeListener(listListener); 119 } else { 120 IObservableSet setDomain = (IObservableSet) domain; 121 setDomain.removeSetChangeListener(listener); 122 } 123 super.dispose(); 124 } 125 126 } 127 | Popular Tags |