-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1301] Add a cross reference adapter to clean the inverse references
This adapter specializes ECrossReferenceAdapter in case of deletion of an EObject or a Resource in order to: * clean the proxies of the inverse references * clean the ECrossReferenceAdapter itself Bug: #1301 Signed-off-by: Laurent Fasani <laurent.fasani@obeo.fr>
- Loading branch information
Showing
1 changed file
with
115 additions
and
0 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
.../java/org/eclipse/sirius/components/emf/services/EditingContextCrossReferenceAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 CEA. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.sirius.components.emf.services; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
import org.eclipse.emf.common.notify.Notification; | ||
import org.eclipse.emf.common.util.TreeIterator; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.ecore.EReference; | ||
import org.eclipse.emf.ecore.EStructuralFeature; | ||
import org.eclipse.emf.ecore.EStructuralFeature.Setting; | ||
import org.eclipse.emf.ecore.resource.Resource; | ||
import org.eclipse.emf.ecore.util.ECrossReferenceAdapter; | ||
import org.eclipse.emf.ecore.util.EcoreUtil; | ||
|
||
/** | ||
* A {@link ECrossReferenceAdapter}@ that is able to clean dangling references when an object is deleted or when a | ||
* resource is removed from the ResourceSet. | ||
* | ||
* @author lfasani | ||
*/ | ||
public class EditingContextCrossReferenceAdapter extends ECrossReferenceAdapter { | ||
|
||
@Override | ||
protected void handleContainment(Notification notification) { | ||
Object oldValue = notification.getOldValue(); | ||
int eventType = notification.getEventType(); | ||
|
||
if (eventType == Notification.REMOVE || eventType == Notification.REMOVE_MANY) { | ||
if (eventType == Notification.REMOVE) { | ||
this.handleRemoveObject(oldValue); | ||
|
||
} else if (eventType == Notification.REMOVE_MANY) { | ||
for (Object object : (Collection<?>) oldValue) { | ||
this.handleRemoveObject(object); | ||
} | ||
} | ||
} else { | ||
super.handleContainment(notification); | ||
} | ||
} | ||
|
||
private void handleRemoveObject(Object object) { | ||
if (object instanceof EObject) { | ||
this.handleRemoveEObject((EObject) object); | ||
} else if (object instanceof Resource) { | ||
this.handleRemoveResource((Resource) object); | ||
} | ||
} | ||
|
||
private void handleRemoveEObject(EObject eObject) { | ||
// Remove all inverse references | ||
TreeIterator<Object> allProperContents = EcoreUtil.getAllProperContents(eObject, false); | ||
while (allProperContents.hasNext()) { | ||
Object object = allProperContents.next(); | ||
this.removeProxyFromInverseReference(object); | ||
} | ||
|
||
// clean the crossRefererenceAdapter | ||
this.unsetTarget(eObject); | ||
} | ||
|
||
private void handleRemoveResource(Resource resource) { | ||
// Remove all inverse references | ||
TreeIterator<Object> allProperContents = EcoreUtil.getAllProperContents(resource, false); | ||
while (allProperContents.hasNext()) { | ||
Object object = allProperContents.next(); | ||
this.removeProxyFromInverseReference(object); | ||
} | ||
|
||
// clean the crossRefererenceAdapter | ||
this.unsetTarget(resource); | ||
} | ||
|
||
private void removeProxyFromInverseReference(Object referencedObject) { | ||
Collection<Setting> settings = this.inverseCrossReferencer.get(referencedObject); | ||
if (settings != null) { | ||
Collection<Setting> nonContainmentReferences = this.getNonContainmentReferences(settings); | ||
for (Setting setting : nonContainmentReferences) { | ||
try { | ||
EcoreUtil.remove(setting, referencedObject); | ||
} catch (NullPointerException e) { | ||
// thrown when trying to remove from an object that is present in the being removed Resource. | ||
// But we do not care about this resource. | ||
} | ||
} | ||
} | ||
} | ||
|
||
private Collection<Setting> getNonContainmentReferences(Collection<Setting> inverseReferences) { | ||
Collection<Setting> containmentReferences = new ArrayList<>(); | ||
for (EStructuralFeature.Setting setting : inverseReferences) { | ||
EStructuralFeature eStructuralFeature = setting.getEStructuralFeature(); | ||
if (eStructuralFeature instanceof EReference) { | ||
EReference eReference = (EReference) eStructuralFeature; | ||
if (!eReference.isContainment()) { | ||
containmentReferences.add(setting); | ||
} | ||
} | ||
} | ||
return containmentReferences; | ||
} | ||
} |