Java Code Examples for org.eclipse.emf.ecore.util.EcoreUtil.UsageCrossReferencer

The following are top voted examples for showing how to use org.eclipse.emf.ecore.util.EcoreUtil.UsageCrossReferencer. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: emfstore-rest   File: IdEObjectCollectionImpl.java   View source code 5 votes vote down vote up
/**
 * 
 * {@inheritDoc}
 * 
 * @see org.eclipse.emf.emfstore.internal.common.model.IdEObjectCollection#deleteModelElement(org.eclipse.emf.ecore.EObject)
 */
public void deleteModelElement(final EObject modelElement) {
	if (!this.contains(modelElement)) {
		throw new IllegalArgumentException("Cannot delete a model element that is not contained in this project.");
	}

	// remove cross references
	ModelUtil.deleteOutgoingCrossReferences(this, modelElement);
	final Collection<Setting> crossReferences = UsageCrossReferencer.find(modelElement, this);
	ModelUtil.deleteIncomingCrossReferencesFromParent(crossReferences, modelElement);

	// remove containment
	final EObject containerModelElement = ModelUtil.getContainerModelElement(modelElement);
	if (containerModelElement == null) {
		// removeModelElementAndChildrenFromCache(modelElement);
		// getEobjectsIdMap().remove(modelElement);
		getModelElements().remove(modelElement);
	} else {
		final EReference containmentFeature = modelElement.eContainmentFeature();
		if (containmentFeature.isMany()) {
			final EList<?> containmentList = (EList<?>) containerModelElement.eGet(containmentFeature);
			containmentList.remove(modelElement);
		} else {
			containerModelElement.eSet(containmentFeature, null);
		}

		removeModelElementAndChildrenFromResource(modelElement);
	}
}
 
Example 2
Project: anatlyzer   File: EMFHandler.java   View source code 4 votes vote down vote up
public void replaceBy(EObject oldObject, EObject newObject) {
	EObject eObject = oldObject;
	// Copied from EcoreUtil#delete
    EObject rootEObject = EcoreUtil.getRootContainer(eObject);
    Resource resource = rootEObject.eResource();

    Collection<EStructuralFeature.Setting> usages;
    if (resource == null)
    {
      usages = UsageCrossReferencer.find(eObject, rootEObject);
    }
    else
    {
      ResourceSet resourceSet = resource.getResourceSet();
      if (resourceSet == null)
      {
        usages = UsageCrossReferencer.find(eObject, resource);
      }
      else
      {
        usages = UsageCrossReferencer.find(eObject, resourceSet);
      }
    }

    for (EStructuralFeature.Setting setting : usages)
    {
      if (setting.getEStructuralFeature().isChangeable())
      {
    	  EObject holder = setting.getEObject();
    	  EStructuralFeature f = setting.getEStructuralFeature();
    	  if ( f.isMany() ) {
    		  EList<EObject> list = (EList<EObject>) holder.eGet(f);
    		  list.set(list.indexOf(oldObject), newObject);
    	  } else {
    		  holder.eSet(f, newObject);
    	  }
      }
    }
	
	
	EcoreUtil.replace(oldObject, newObject);
	allCreatedObjectsIntoResource.remove(oldObject);
}
 
Example 3
Project: Environment   File: OverviewSpecificationUtil.java   View source code 4 votes vote down vote up
/**
 *
 * Recursively count all "external" references.
 * 
 * @param rootDesc Descriptor in question.
 * @param desc Descriptor used for recursion. It must be equal to @rootDesc. 
 */
private static synchronized boolean isDescriptorReferencedElsewhere(Descriptor rootDesc, Descriptor desc){
	//remove descriptor only if it is not referenced elsewhere
	//TODO: check if it works as expected
	EObject root = EcoreUtil.getRootContainer(desc);
	
	int descriptroRefCounter = 0;
	for(Object object : UsageCrossReferencer.find(desc, root)){
		
		//check if reference is marked for release
		boolean isInsideRoot = false;
		if(object instanceof EObject){
			EObject ref = (EObject)object;
			while(ref != null){
				if(ref == rootDesc){
					isInsideRoot = true;
					break;
				}
				ref = ref.eContainer(); 
			}
		}
		
		if(!isInsideRoot){
			descriptroRefCounter++;
		}
	}
	
	if(descriptroRefCounter > 1){
		return true;
	}
	
	for(EObject child : desc.eContents()){
		if(child instanceof Descriptor){
			if(isDescriptorReferencedElsewhere(rootDesc, (Descriptor)child)){
				return true;
			}
		}
	}
	
	return false;
}
 
Example 4
Project: eclectic   File: EMFHandler.java   View source code 4 votes vote down vote up
public void replaceBy(EObject oldObject, EObject newObject) {
	EObject eObject = oldObject;
	// Copied from EcoreUtil#delete
    EObject rootEObject = EcoreUtil.getRootContainer(eObject);
    Resource resource = rootEObject.eResource();

    Collection<EStructuralFeature.Setting> usages;
    if (resource == null)
    {
      usages = UsageCrossReferencer.find(eObject, rootEObject);
    }
    else
    {
      ResourceSet resourceSet = resource.getResourceSet();
      if (resourceSet == null)
      {
        usages = UsageCrossReferencer.find(eObject, resource);
      }
      else
      {
        usages = UsageCrossReferencer.find(eObject, resourceSet);
      }
    }

    for (EStructuralFeature.Setting setting : usages)
    {
      if (setting.getEStructuralFeature().isChangeable())
      {
    	  EObject holder = setting.getEObject();
    	  EStructuralFeature f = setting.getEStructuralFeature();
    	  if ( f.isMany() ) {
    		  EList<EObject> list = (EList<EObject>) holder.eGet(f);
    		  list.set(list.indexOf(oldObject), newObject);
    	  } else {
    		  holder.eSet(f, newObject);
    	  }
      }
    }
	
	
	EcoreUtil.replace(oldObject, newObject);
	allCreatedObjectsIntoResource.remove(oldObject);
}
 
Example 5
Project: emfstore-rest   File: ProjectSpaceBase.java   View source code 3 votes vote down vote up
/**
 * Returns the {@link ECrossReferenceAdapter}, if available.
 * 
 * @param modelElement
 *            the model element for which to find inverse cross references
 * 
 * @return the {@link ECrossReferenceAdapter}
 */
public Collection<Setting> findInverseCrossReferences(EObject modelElement) {
	if (crossReferenceAdapter != null) {
		return crossReferenceAdapter.getInverseReferences(modelElement);
	}

	return UsageCrossReferencer.find(modelElement, resourceSet);
}