1 17 package org.eclipse.emf.ecore.util; 18 19 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 23 import org.eclipse.emf.common.notify.Notification; 24 import org.eclipse.emf.common.notify.Notifier; 25 import org.eclipse.emf.common.notify.impl.AdapterImpl; 26 import org.eclipse.emf.ecore.EObject; 27 import org.eclipse.emf.ecore.EReference; 28 import org.eclipse.emf.ecore.resource.Resource; 29 import org.eclipse.emf.ecore.resource.ResourceSet; 30 31 32 37 public class EContentAdapter extends AdapterImpl 38 { 39 42 public void notifyChanged(Notification notification) 43 { 44 selfAdapt(notification); 45 46 super.notifyChanged(notification); 47 } 48 49 53 protected void selfAdapt(Notification notification) 54 { 55 Object notifier = notification.getNotifier(); 56 if (notification.getEventType() == Notification.REMOVING_ADAPTER) 57 { 58 unsetTarget(notifier); 59 } 60 else if (notifier instanceof ResourceSet) 61 { 62 if (notification.getFeatureID(ResourceSet.class) == ResourceSet.RESOURCE_SET__RESOURCES) 63 { 64 handleContainment(notification); 65 } 66 } 67 else if (notifier instanceof Resource) 68 { 69 if (notification.getFeatureID(Resource.class) == Resource.RESOURCE__CONTENTS) 70 { 71 handleContainment(notification); 72 } 73 } 74 else if (notifier instanceof EObject) 75 { 76 Object feature = notification.getFeature(); 77 if (feature instanceof EReference && ((EReference)feature).isContainment()) 78 { 79 handleContainment(notification); 80 } 81 } 82 } 83 84 87 protected void handleContainment(Notification notification) 88 { 89 switch (notification.getEventType()) 90 { 91 case Notification.SET: 92 case Notification.UNSET: 93 { 94 Notifier oldValue = (Notifier)notification.getOldValue(); 95 if (oldValue != null) 96 { 97 removeAdapter(oldValue); 98 } 99 Notifier newValue = (Notifier)notification.getNewValue(); 100 if (newValue != null) 101 { 102 addAdapter(newValue); 103 } 104 break; 105 } 106 case Notification.ADD: 107 { 108 Notifier newValue = (Notifier)notification.getNewValue(); 109 if (newValue != null) 110 { 111 addAdapter(newValue); 112 } 113 break; 114 } 115 case Notification.ADD_MANY: 116 { 117 Collection newValues = (Collection )notification.getNewValue(); 118 for (Iterator i = newValues.iterator(); i.hasNext(); ) 119 { 120 Notifier newValue = (Notifier)i.next(); 121 addAdapter(newValue); 122 } 123 break; 124 } 125 case Notification.REMOVE: 126 { 127 Notifier oldValue = (Notifier)notification.getOldValue(); 128 if (oldValue != null) 129 { 130 removeAdapter(oldValue); 131 } 132 break; 133 } 134 case Notification.REMOVE_MANY: 135 { 136 Collection oldValues = (Collection )notification.getOldValue(); 137 for (Iterator i = oldValues.iterator(); i.hasNext(); ) 138 { 139 Notifier oldContentValue = (Notifier)i.next(); 140 removeAdapter(oldContentValue); 141 } 142 break; 143 } 144 } 145 } 146 147 151 public void setTarget(Notifier target) 152 { 153 super.setTarget(target); 154 155 Collection contents = 156 target instanceof EObject ? 157 ((EObject)target).eContents() : 158 target instanceof ResourceSet ? 159 ((ResourceSet)target).getResources() : 160 target instanceof Resource ? 161 ((Resource)target).getContents() : 162 null; 163 if (contents != null) 164 { 165 for (Iterator i = contents.iterator(); i.hasNext(); ) 166 { 167 Notifier notifier = (Notifier)i.next(); 168 addAdapter(notifier); 169 } 170 } 171 } 172 173 177 protected void unsetTarget(Object target) 178 { 179 super.setTarget(null); 180 181 Collection contents = 182 target instanceof EObject ? 183 ((EObject)target).eContents() : 184 target instanceof ResourceSet ? 185 ((ResourceSet)target).getResources() : 186 target instanceof Resource ? 187 ((Resource)target).getContents() : 188 null; 189 if (contents != null) 190 { 191 for (Iterator i = contents.iterator(); i.hasNext(); ) 192 { 193 Notifier notifier = (Notifier)i.next(); 194 removeAdapter(notifier); 195 } 196 } 197 } 198 199 protected void addAdapter(Notifier notifier) 200 { 201 notifier.eAdapters().add(this); 202 } 203 204 protected void removeAdapter(Notifier notifier) 205 { 206 notifier.eAdapters().remove(this); 207 } 208 } 209 | Popular Tags |