1 7 package org.eclipse.emf.ecore.change.impl; 8 9 import java.util.Collection ; 10 import java.util.Iterator ; 11 12 import org.eclipse.emf.common.notify.Notification; 13 import org.eclipse.emf.common.notify.NotificationChain; 14 import org.eclipse.emf.common.util.BasicEList; 15 import org.eclipse.emf.common.util.ECollections; 16 import org.eclipse.emf.common.util.EList; 17 import org.eclipse.emf.common.util.URI; 18 import org.eclipse.emf.ecore.EClass; 19 import org.eclipse.emf.ecore.EStructuralFeature; 20 import org.eclipse.emf.ecore.InternalEObject; 21 import org.eclipse.emf.ecore.change.ChangePackage; 22 import org.eclipse.emf.ecore.change.ListChange; 23 import org.eclipse.emf.ecore.change.ResourceChange; 24 import org.eclipse.emf.ecore.impl.ENotificationImpl; 25 import org.eclipse.emf.ecore.impl.EObjectImpl; 26 import org.eclipse.emf.ecore.resource.Resource; 27 import org.eclipse.emf.ecore.resource.ResourceSet; 28 import org.eclipse.emf.ecore.util.EObjectContainmentEList; 29 import org.eclipse.emf.ecore.util.InternalEList; 30 31 47 public class ResourceChangeImpl extends EObjectImpl implements ResourceChange 48 { 49 57 protected static final String RESOURCE_URI_EDEFAULT = null; 58 59 67 protected String resourceURI = RESOURCE_URI_EDEFAULT; 68 69 77 protected static final Resource RESOURCE_EDEFAULT = null; 78 79 87 protected Resource resource = RESOURCE_EDEFAULT; 88 89 97 protected static final EList VALUE_EDEFAULT = null; 98 99 107 protected EList listChanges = null; 108 109 protected EList valueField = null; 110 111 116 protected ResourceChangeImpl() 117 { 118 super(); 119 } 120 121 protected ResourceChangeImpl(Resource resource, EList value) 122 { 123 this(); 124 setResource(resource); 125 setValue(value); 126 } 127 128 133 protected EClass eStaticClass() 134 { 135 return ChangePackage.eINSTANCE.getResourceChange(); 136 } 137 138 143 public String getResourceURI() 144 { 145 return resourceURI; 146 } 147 148 153 public void setResourceURI(String newResourceURI) 154 { 155 String oldResourceURI = resourceURI; 156 resourceURI = newResourceURI; 157 if (eNotificationRequired()) 158 eNotify(new ENotificationImpl(this, Notification.SET, ChangePackage.RESOURCE_CHANGE__RESOURCE_URI, oldResourceURI, resourceURI)); 159 } 160 161 166 public Resource getResource() 167 { 168 if (resource == null) 169 { 170 if (getResourceURI() != null) 171 { 172 Resource changeResource = eResource(); 173 if (changeResource != null) 174 { 175 ResourceSet resourceSet = changeResource.getResourceSet(); 176 if (resourceSet != null) 177 { 178 resource = resourceSet.getResource(URI.createURI(getResourceURI()), false); 179 } 180 } 181 } 182 } 183 return resource; 184 } 185 186 191 public void setResourceGen(Resource newResource) 192 { 193 Resource oldResource = resource; 194 resource = newResource; 195 if (eNotificationRequired()) 196 eNotify(new ENotificationImpl(this, Notification.SET, ChangePackage.RESOURCE_CHANGE__RESOURCE, oldResource, resource)); 197 } 198 199 public void setResource(Resource newResource) 200 { 201 setResourceGen(newResource); 202 setResourceURI(newResource.getURI() == null ? null : newResource.getURI().toString()); 203 } 204 205 210 public EList getValue() 211 { 212 if (valueField == null) 213 { 214 Resource resource = getResource(); 215 if (resource != null) 216 { 217 EList changedList = new BasicEList(resource.getContents()); 218 apply(changedList); 219 valueField = changedList; return changedList; 221 } 222 return ECollections.EMPTY_ELIST; 223 } 224 return valueField; 225 } 226 227 protected void setValue(EList value) 228 { 229 valueField = value; 230 } 231 232 237 public EList getListChanges() 238 { 239 if (listChanges == null) 240 { 241 listChanges = new EObjectContainmentEList(ListChange.class, this, ChangePackage.RESOURCE_CHANGE__LIST_CHANGES); 242 } 243 return listChanges; 244 } 245 246 251 public void apply() 252 { 253 Resource resource = getResource(); 254 if (resource != null && listChanges != null) 255 { 256 apply(resource.getContents()); 257 } 258 } 259 260 265 public void applyAndReverse() 266 { 267 Resource resource = getResource(); 268 if (resource != null && listChanges != null) 269 { 270 EList applyToList = resource.getContents(); 271 for (Iterator iter = getListChanges().iterator(); iter.hasNext(); ) 272 { 273 ListChange listChange = (ListChange)iter.next(); 274 listChange.applyAndReverse(applyToList); 275 } 276 ECollections.reverse(getListChanges()); 277 setValue(null); 278 } 279 } 280 281 protected void apply(EList toList) 282 { 283 for (Iterator iter = getListChanges().iterator(); iter.hasNext(); ) 284 { 285 ListChange listChange = (ListChange)iter.next(); 286 listChange.apply(toList); 287 } 288 } 289 290 295 public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, Class baseClass, NotificationChain msgs) 296 { 297 if (featureID >= 0) 298 { 299 switch (eDerivedStructuralFeatureID(featureID, baseClass)) 300 { 301 case ChangePackage.RESOURCE_CHANGE__LIST_CHANGES: 302 return ((InternalEList)getListChanges()).basicRemove(otherEnd, msgs); 303 default: 304 return eDynamicInverseRemove(otherEnd, featureID, baseClass, msgs); 305 } 306 } 307 return eBasicSetContainer(null, featureID, msgs); 308 } 309 310 315 public Object eGet(EStructuralFeature eFeature, boolean resolve) 316 { 317 switch (eDerivedStructuralFeatureID(eFeature)) 318 { 319 case ChangePackage.RESOURCE_CHANGE__RESOURCE_URI: 320 return getResourceURI(); 321 case ChangePackage.RESOURCE_CHANGE__RESOURCE: 322 return getResource(); 323 case ChangePackage.RESOURCE_CHANGE__VALUE: 324 return getValue(); 325 case ChangePackage.RESOURCE_CHANGE__LIST_CHANGES: 326 return getListChanges(); 327 } 328 return eDynamicGet(eFeature, resolve); 329 } 330 331 336 public void eSet(EStructuralFeature eFeature, Object newValue) 337 { 338 switch (eDerivedStructuralFeatureID(eFeature)) 339 { 340 case ChangePackage.RESOURCE_CHANGE__RESOURCE_URI: 341 setResourceURI((String )newValue); 342 return; 343 case ChangePackage.RESOURCE_CHANGE__RESOURCE: 344 setResource((Resource)newValue); 345 return; 346 case ChangePackage.RESOURCE_CHANGE__LIST_CHANGES: 347 getListChanges().clear(); 348 getListChanges().addAll((Collection )newValue); 349 return; 350 } 351 eDynamicSet(eFeature, newValue); 352 } 353 354 359 public void eUnset(EStructuralFeature eFeature) 360 { 361 switch (eDerivedStructuralFeatureID(eFeature)) 362 { 363 case ChangePackage.RESOURCE_CHANGE__RESOURCE_URI: 364 setResourceURI(RESOURCE_URI_EDEFAULT); 365 return; 366 case ChangePackage.RESOURCE_CHANGE__RESOURCE: 367 setResource(RESOURCE_EDEFAULT); 368 return; 369 case ChangePackage.RESOURCE_CHANGE__LIST_CHANGES: 370 getListChanges().clear(); 371 return; 372 } 373 eDynamicUnset(eFeature); 374 } 375 376 381 public boolean eIsSet(EStructuralFeature eFeature) 382 { 383 switch (eDerivedStructuralFeatureID(eFeature)) 384 { 385 case ChangePackage.RESOURCE_CHANGE__RESOURCE_URI: 386 return RESOURCE_URI_EDEFAULT == null ? resourceURI != null : !RESOURCE_URI_EDEFAULT.equals(resourceURI); 387 case ChangePackage.RESOURCE_CHANGE__RESOURCE: 388 return RESOURCE_EDEFAULT == null ? resource != null : !RESOURCE_EDEFAULT.equals(resource); 389 case ChangePackage.RESOURCE_CHANGE__VALUE: 390 return VALUE_EDEFAULT == null ? getValue() != null : !VALUE_EDEFAULT.equals(getValue()); 391 case ChangePackage.RESOURCE_CHANGE__LIST_CHANGES: 392 return listChanges != null && !listChanges.isEmpty(); 393 } 394 return eDynamicIsSet(eFeature); 395 } 396 397 402 public String toString() 403 { 404 if (eIsProxy()) return super.toString(); 405 406 StringBuffer result = new StringBuffer (super.toString()); 407 result.append(" (resourceURI: "); 408 result.append(resourceURI); 409 result.append(", resource: "); 410 result.append(resource); 411 result.append(')'); 412 return result.toString(); 413 } 414 415 } | Popular Tags |