1 17 package org.eclipse.emf.common.notify.impl; 18 19 20 import java.util.Collection ; 21 22 import org.eclipse.emf.common.notify.Adapter; 23 import org.eclipse.emf.common.notify.Notification; 24 import org.eclipse.emf.common.notify.Notifier; 25 import org.eclipse.emf.common.util.BasicEList; 26 import org.eclipse.emf.common.util.ECollections; 27 import org.eclipse.emf.common.util.EList; 28 29 30 33 public class BasicNotifierImpl implements Notifier 34 { 35 38 public BasicNotifierImpl() 39 { 40 } 41 42 public static class EAdapterList extends BasicEList 43 { 44 protected Notifier notifier; 45 46 public EAdapterList(Notifier notifier) 47 { 48 this.notifier = notifier; 49 } 50 51 protected boolean safe; 52 53 protected boolean canContainNull() 54 { 55 return false; 56 } 57 58 protected boolean useEquals() 59 { 60 return false; 61 } 62 63 protected Object [] newData(int capacity) 64 { 65 return new Adapter [capacity]; 66 } 67 68 protected void didAdd(int index, Object newObject) 69 { 70 Adapter adapter = (Adapter)newObject; 71 adapter.setTarget(notifier); 72 } 73 74 protected void didRemove(int index, Object oldObject) 75 { 76 Adapter adapter = (Adapter)oldObject; 77 if (notifier.eDeliver()) 78 { 79 Notification notification = 80 new NotificationImpl(Notification.REMOVING_ADAPTER, oldObject, null, index) 81 { 82 public Object getNotifier() 83 { 84 return notifier; 85 } 86 }; 87 adapter.notifyChanged(notification); 88 } 89 if (adapter instanceof Adapter.Internal) 90 { 91 ((Adapter.Internal)adapter).unsetTarget(notifier); 92 } 93 else if (adapter.getTarget() == notifier) 94 { 95 adapter.setTarget(null); 96 } 97 } 98 99 public Object [] data() 100 { 101 safe = true; 102 return (Adapter [])data; 103 } 104 105 protected void ensureSafety() 106 { 107 if (safe && data != null) 108 { 109 Object [] oldData = data; 110 data = newData(data.length); 111 System.arraycopy(oldData, 0, data, 0, size); 112 safe = false; 113 } 114 } 115 116 public boolean add(Object object) 117 { 118 ensureSafety(); 119 return super.add(object); 120 } 121 122 public void add(int index, Object object) 123 { 124 ensureSafety(); 125 super.add(index, object); 126 } 127 128 public boolean addAll(Collection collection) 129 { 130 ensureSafety(); 131 return super.addAll(collection); 132 } 133 134 public boolean remove(Object object) 135 { 136 ensureSafety(); 137 return super.remove(object); 138 } 139 140 public Object remove(int index) 141 { 142 ensureSafety(); 143 return super.remove(index); 144 } 145 146 public boolean removeAll(Collection collection) 147 { 148 ensureSafety(); 149 return super.removeAll(collection); 150 } 151 152 public void clear() 153 { 154 ensureSafety(); 155 super.clear(); 156 } 157 158 public boolean retainAll(Collection collection) 159 { 160 ensureSafety(); 161 return super.retainAll(collection); 162 } 163 164 public Object set(int index, Object object) 165 { 166 ensureSafety(); 167 return super.set(index, object); 168 } 169 170 public void move(int newPosition, Object object) 171 { 172 ensureSafety(); 173 super.move(newPosition, object); 174 } 175 176 public Object move(int newPosition, int oldPosition) 177 { 178 ensureSafety(); 179 return super.move(newPosition, oldPosition); 180 } 181 } 182 183 186 public EList eAdapters() 187 { 188 return ECollections.EMPTY_ELIST; 189 } 190 191 195 protected BasicEList eBasicAdapters() 196 { 197 return null; 198 } 199 200 203 public boolean eDeliver() 204 { 205 return false; 206 } 207 208 211 public void eSetDeliver(boolean deliver) 212 { 213 throw new UnsupportedOperationException (); 214 } 215 216 219 public void eNotify(Notification notification) 220 { 221 if (eDeliver() && eBasicAdapters() != null) 222 { 223 int size = eBasicAdapters().size(); 224 if (size > 0) 225 { 226 Adapter [] adapters = (Adapter [])eBasicAdapters().data(); 227 for (int i = 0; i < size; ++i) 228 { 229 adapters[i].notifyChanged(notification); 230 } 231 } 232 } 233 } 234 235 241 public boolean eNotificationRequired() 242 { 243 BasicEList eAdapters = eBasicAdapters(); 244 return eAdapters != null && eDeliver() && !eAdapters.isEmpty(); 245 } 246 } 247 | Popular Tags |