1 11 12 package org.eclipse.jface.internal.databinding.provisional.observable.mapping; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashSet ; 17 18 import org.eclipse.jface.internal.databinding.provisional.observable.AbstractObservable; 19 import org.eclipse.jface.internal.databinding.provisional.observable.IChangeListener; 20 21 25 public abstract class BaseObservableMapping extends AbstractObservable { 26 27 31 protected Object mappingChangeListeners = null; 32 33 public void addMappingChangeListener(IMappingChangeListener listener) { 34 if (mappingChangeListeners == null) { 35 boolean hadListeners = hasListeners(); 36 mappingChangeListeners = listener; 37 if (!hadListeners) { 38 firstListenerAdded(); 39 } 40 return; 41 } 42 43 Collection listenerList; 44 if (mappingChangeListeners instanceof IMappingChangeListener) { 45 IChangeListener l = (IChangeListener) mappingChangeListeners; 46 47 listenerList = new ArrayList (); 48 listenerList.add(l); 49 } else { 50 listenerList = (Collection ) mappingChangeListeners; 51 } 52 53 if (listenerList.size() > 16) { 54 HashSet listenerSet = new HashSet (); 55 listenerSet.addAll(listenerList); 56 mappingChangeListeners = listenerList; 57 } 58 59 listenerList.add(listener); 60 } 61 62 public void removeMappingChangeListener(IMappingChangeListener listener) { 63 if (mappingChangeListeners == listener) { 64 mappingChangeListeners = null; 65 if (!hasListeners()) { 66 lastListenerRemoved(); 67 } 68 return; 69 } 70 71 if (mappingChangeListeners instanceof Collection ) { 72 Collection listenerList = (Collection ) mappingChangeListeners; 73 listenerList.remove(listener); 74 if (listenerList.size() == 0) { 75 mappingChangeListeners = null; 76 if (!hasListeners()) { 77 lastListenerRemoved(); 78 } 79 } 80 } 81 } 82 83 protected void fireMappingValueChange(MappingDiff diff) { 84 if (mappingChangeListeners == null) { 85 return; 86 } 87 88 if (mappingChangeListeners instanceof IMappingChangeListener) { 89 ((IMappingChangeListener) mappingChangeListeners).handleMappingValueChange(this, diff); 90 return; 91 } 92 93 Collection changeListenerCollection = (Collection ) mappingChangeListeners; 94 95 IMappingChangeListener[] listeners = (IMappingChangeListener[]) (changeListenerCollection) 96 .toArray(new IMappingChangeListener[changeListenerCollection.size()]); 97 for (int i = 0; i < listeners.length; i++) { 98 listeners[i].handleMappingValueChange(this, diff); 99 } 100 } 101 102 } 103 | Popular Tags |