Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Remove item from list using its containment Setting
Remove item from list using its containment Setting [message #1795708] Thu, 27 September 2018 08:12 Go to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 854
Registered: July 2009
Senior Member
I have a containment list that I need to remove an element from.
My idea was to get the element's Setting and use that to remove it from its containment list, but calling setting.unset() will clear the whole list.


        MyContainer container = fcn.getRequirementsFolder();
        MyElement element = container.getElements().get(0);
        EStructuralFeature feature = element.eContainmentFeature();
        Setting setting = ((InternalEObject)container).eSetting(feature);
        setting.unset(); // <- this will clear the list: How to remove just the element?


Is there a way to remove just the element using the setting?
Re: Remove item from list using its containment Setting [message #1795712 is a reply to message #1795708] Thu, 27 September 2018 08:40 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

It is probably possible to use the Setting, but that seems like a really hard way to go.

If you know that the container feature is named "requirementsFolder" then setRequirementsFolder(null) should work.

If you also know that the containment feature is named "elements" then getRequirementsFolder().getElements().remove(this) should work.

If you only know that the containment feature is a collection then something like ((List)eContainer().eGet(eContainmentFeature())).remove(this) should work.

If you don't know whether the container is a list or an object you will need to do an isMany() conditionalization.

The Impl API offers other alternatives that I have never tried such as eBasicRemoveFromContainerFeature that should be studied carefully before use.

Regards

Ed Willink

Re: Remove item from list using its containment Setting [message #1795713 is a reply to message #1795712] Thu, 27 September 2018 08:53 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 854
Registered: July 2009
Senior Member
Thanks, Ed!

I do not know if the container is a list or an object (example was just to demo the use case). Would be convenient to use the setting without checking if the feature is many and casting to Collection (I have used that pattern already lots of times).

Ed Willink wrote on Thu, 27 September 2018 10:40
Hi

It is probably possible to use the Setting, but that seems like a really hard way to go.

If you know that the container feature is named "requirementsFolder" then setRequirementsFolder(null) should work.

If you also know that the containment feature is named "elements" then getRequirementsFolder().getElements().remove(this) should work.

If you only know that the containment feature is a collection then something like ((List)eContainer().eGet(eContainmentFeature())).remove(this) should work.

If you don't know whether the container is a list or an object you will need to do an isMany() conditionalization.

The Impl API offers other alternatives that I have never tried such as eBasicRemoveFromContainerFeature that should be studied carefully before use.

Regards

Ed Willink


Re: Remove item from list using its containment Setting [message #1795726 is a reply to message #1795713] Thu, 27 September 2018 10:35 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
You can use org.eclipse.emf.ecore.util.EcoreUtil.remove(EObject) to remove an object from its container; this will remove it from its resource if is directly contained by some resource. There are also org.eclipse.emf.ecore.util.EcoreUtil.remove(Setting, Object) and org.eclipse.emf.ecore.util.EcoreUtil.remove(EObject, EStructuralFeature, Object), not to mention org.eclipse.emf.ecore.util.EcoreUtil.delete(EObject) which will also remove all references to the object being deleted.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Remove item from list using its containment Setting [message #1795764 is a reply to message #1795726] Fri, 28 September 2018 07:47 Go to previous message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 854
Registered: July 2009
Senior Member
Thanks, Ed!
That is what I was looking for!

Ed Merks wrote on Thu, 27 September 2018 12:35
You can use org.eclipse.emf.ecore.util.EcoreUtil.remove(EObject) to remove an object from its container; this will remove it from its resource if is directly contained by some resource. There are also org.eclipse.emf.ecore.util.EcoreUtil.remove(Setting, Object) and org.eclipse.emf.ecore.util.EcoreUtil.remove(EObject, EStructuralFeature, Object), not to mention org.eclipse.emf.ecore.util.EcoreUtil.delete(EObject) which will also remove all references to the object being deleted.

Previous Topic:EcoreUtil.Copier and change of explicit UUIDs
Next Topic:XCore code generation from command line
Goto Forum:
  


Current Time: Fri Apr 26 05:20:12 GMT 2024

Powered by FUDForum. Page generated in 0.03399 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top