1 11 12 package org.eclipse.jface.internal.databinding.provisional.observable; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashSet ; 17 18 22 public abstract class AbstractObservable implements IObservable { 23 24 28 private Object changeListeners = null; 29 30 34 private Object staleListeners = null; 35 36 public void addChangeListener(IChangeListener listener) { 37 if (changeListeners == null) { 38 boolean hadListeners = hasListeners(); 39 changeListeners = listener; 40 if (!hadListeners) { 41 firstListenerAdded(); 42 } 43 return; 44 } 45 46 Collection listenerList; 47 if (changeListeners instanceof IChangeListener) { 48 IChangeListener l = (IChangeListener) changeListeners; 49 50 listenerList = new ArrayList (); 51 listenerList.add(l); 52 } else { 53 listenerList = (Collection ) changeListeners; 54 } 55 56 if (listenerList.size() > 16) { 57 HashSet listenerSet = new HashSet (); 58 listenerSet.addAll(listenerList); 59 changeListeners = listenerList; 60 } 61 62 listenerList.add(listener); 63 } 64 65 public void removeChangeListener(IChangeListener listener) { 66 if (changeListeners == listener) { 67 changeListeners = null; 68 if (!hasListeners()) { 69 lastListenerRemoved(); 70 } 71 return; 72 } 73 74 if (changeListeners instanceof Collection ) { 75 Collection listenerList = (Collection ) changeListeners; 76 listenerList.remove(listener); 77 if (listenerList.size() == 0) { 78 changeListeners = null; 79 if (!hasListeners()) { 80 lastListenerRemoved(); 81 } 82 } 83 } 84 } 85 86 public void addStaleListener(IStaleListener listener) { 87 if (staleListeners == null) { 88 boolean hadListeners = hasListeners(); 89 staleListeners = listener; 90 if (!hadListeners) { 91 firstListenerAdded(); 92 } 93 return; 94 } 95 96 Collection listenerList; 97 if (staleListeners instanceof IStaleListener) { 98 IStaleListener l = (IStaleListener) staleListeners; 99 100 listenerList = new ArrayList (); 101 listenerList.add(l); 102 } else { 103 listenerList = (Collection ) staleListeners; 104 } 105 106 if (listenerList.size() > 16) { 107 HashSet listenerSet = new HashSet (); 108 listenerSet.addAll(listenerList); 109 staleListeners = listenerList; 110 } 111 112 listenerList.add(listener); 113 } 114 115 public void removeStaleListener(IStaleListener listener) { 116 if (staleListeners == listener) { 117 staleListeners = null; 118 if (!hasListeners()) { 119 lastListenerRemoved(); 120 } 121 return; 122 } 123 124 if (staleListeners instanceof Collection ) { 125 Collection listenerList = (Collection ) staleListeners; 126 listenerList.remove(listener); 127 if (listenerList.size() == 0) { 128 staleListeners = null; 129 if (!hasListeners()) { 130 lastListenerRemoved(); 131 } 132 } 133 } 134 } 135 136 protected void fireChange() { 137 if (changeListeners == null) { 138 return; 139 } 140 141 if (changeListeners instanceof IChangeListener) { 142 ((IChangeListener) changeListeners).handleChange(this); 143 return; 144 } 145 146 Collection changeListenerCollection = (Collection ) changeListeners; 147 148 IChangeListener[] listeners = (IChangeListener[]) (changeListenerCollection) 149 .toArray(new IChangeListener[changeListenerCollection.size()]); 150 for (int i = 0; i < listeners.length; i++) { 151 listeners[i].handleChange(this); 152 } 153 } 154 155 protected void fireStale() { 156 if (staleListeners == null) { 157 return; 158 } 159 160 if (staleListeners instanceof IChangeListener) { 161 ((IChangeListener) staleListeners).handleChange(this); 162 return; 163 } 164 165 Collection changeListenerCollection = (Collection ) staleListeners; 166 167 IChangeListener[] listeners = (IChangeListener[]) (changeListenerCollection) 168 .toArray(new IChangeListener[changeListenerCollection.size()]); 169 for (int i = 0; i < listeners.length; i++) { 170 listeners[i].handleChange(this); 171 } 172 } 173 174 177 protected boolean hasListeners() { 178 return changeListeners != null || staleListeners != null; 179 } 180 181 184 protected void firstListenerAdded() { 185 } 186 187 190 protected void lastListenerRemoved() { 191 } 192 193 196 public void dispose() { 197 changeListeners = null; 198 staleListeners = null; 199 } 200 201 } 202 | Popular Tags |