1 11 12 package org.eclipse.core.databinding.observable; 13 14 import org.eclipse.core.runtime.Assert; 15 import org.eclipse.core.runtime.ListenerList; 16 17 24 class ChangeManager { 25 26 ListenerList[] listenerLists = null; 27 Object listenerTypes[] = null; 28 private Realm realm; 29 30 34 ChangeManager(Realm realm) { 35 Assert.isNotNull(realm); 36 this.realm = realm; 37 } 38 39 43 protected void addListener(Object listenerType, 44 IObservablesListener listener) { 45 int listenerTypeIndex = findListenerTypeIndex(listenerType); 46 if (listenerTypeIndex == -1) { 47 int length; 48 if (listenerTypes == null) { 49 length = 0; 50 listenerTypes = new Object [1]; 51 listenerLists = new ListenerList[1]; 52 } else { 53 length = listenerTypes.length; 54 System.arraycopy(listenerTypes, 0, 55 listenerTypes = new Object [length + 1], 0, length); 56 System 57 .arraycopy(listenerLists, 0, 58 listenerLists = new ListenerList[length + 1], 59 0, length); 60 } 61 listenerTypes[length] = listenerType; 62 listenerLists[length] = new ListenerList(); 63 boolean hadListeners = hasListeners(); 64 listenerLists[length].add(listener); 65 if (!hadListeners) { 66 this.firstListenerAdded(); 67 } 68 return; 69 } 70 ListenerList listenerList = listenerLists[listenerTypeIndex]; 71 boolean hadListeners = true; 72 if (listenerList.size() == 0) { 73 hadListeners = hasListeners(); 74 } 75 listenerList.add(listener); 76 if (!hadListeners) { 77 firstListenerAdded(); 78 } 79 } 80 81 85 protected void removeListener(Object listenerType, 86 IObservablesListener listener) { 87 int listenerTypeIndex = findListenerTypeIndex(listenerType); 88 if (listenerTypeIndex != -1) { 89 listenerLists[listenerTypeIndex].remove(listener); 90 if (listenerLists[listenerTypeIndex].size() == 0) { 91 if (!hasListeners()) { 92 this.lastListenerRemoved(); 93 } 94 } 95 } 96 } 97 98 protected boolean hasListeners() { 99 if (listenerTypes == null) { 100 return false; 101 } 102 for (int i = 0; i < listenerTypes.length; i++) { 103 if (listenerLists[i].size() > 0) { 104 return true; 105 } 106 } 107 return false; 108 } 109 110 private int findListenerTypeIndex(Object listenerType) { 111 if (listenerTypes != null) { 112 for (int i = 0; i < listenerTypes.length; i++) { 113 if (listenerTypes[i] == listenerType) { 114 return i; 115 } 116 } 117 } 118 return -1; 119 } 120 121 protected void fireEvent(ObservableEvent event) { 122 Object listenerType = event.getListenerType(); 123 int listenerTypeIndex = findListenerTypeIndex(listenerType); 124 if (listenerTypeIndex != -1) { 125 Object [] listeners = listenerLists[listenerTypeIndex] 126 .getListeners(); 127 for (int i = 0; i < listeners.length; i++) { 128 event.dispatch((IObservablesListener) listeners[i]); 129 } 130 } 131 } 132 133 136 protected void firstListenerAdded() { 137 } 138 139 142 protected void lastListenerRemoved() { 143 } 144 145 148 public void dispose() { 149 listenerLists = null; 150 listenerTypes = null; 151 realm = null; 152 } 153 154 157 public Realm getRealm() { 158 return realm; 159 } 160 161 } 162 | Popular Tags |