Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to get an EObject from an EObjectNode
How to get an EObject from an EObjectNode [message #1010446] Sun, 17 February 2013 10:10 Go to next message
Andrea Sindico is currently offline Andrea SindicoFriend
Messages: 266
Registered: November 2010
Senior Member

n an independent ViewPart I managed to get the EObjectNode selected in the OutlineView. I now need to get the EObject it refers to but I can't see how.

Follow me on Twitter @andreasindico

Re: How to get an EObject from an EObjectNode [message #1010452 is a reply to message #1010446] Sun, 17 February 2013 10:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

have a look at org.eclipse.xtext.ui.editor.outline.impl.AbstractOutlineNode.readOnly(IUnitOfWork<T, EObject>)

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get an EObject from an EObjectNode [message #1010469 is a reply to message #1010452] Sun, 17 February 2013 11:52 Go to previous messageGo to next message
Andrea Sindico is currently offline Andrea SindicoFriend
Messages: 266
Registered: November 2010
Senior Member

I have seen the object selected in the outline is a AbstractOutlineNode.
I can't see how to use the readonly method to get the related EObject. I understand from some blogs i found on the Internet that IUnitOfWork i used to get an EObject from the DSL editor. Is it exploitable to this purpose as well? How should I used it?

Thanks for your kind help

Regards


Follow me on Twitter @andreasindico

Re: How to get an EObject from an EObjectNode [message #1010472 is a reply to message #1010469] Sun, 17 February 2013 12:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
No,

i just help you to do work on it.

if you want to process it further.

(1) your iresourcesetprovider to obtain a resourceset for the project
(2) ask the resourceset for a resource with the objects uri (with trimmed fragment)
(3) use to get the object from the resource with org.eclipse.emf.ecore.resource.Resource.getEObject(String) and the fragment


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get an EObject from an EObjectNode [message #1010579 is a reply to message #1010472] Sun, 17 February 2013 19:01 Go to previous messageGo to next message
Andrea Sindico is currently offline Andrea SindicoFriend
Messages: 266
Registered: November 2010
Senior Member

This is the answer I was looking for

if (selection instanceof IStructuredSelection) {
        Object first = ((IStructuredSelection)selection).getFirstElement();
        EObjectNode enode = (EObjectNode)first;
        ResourceSet resourceSet = new ResourceSetImpl();
        Resource inResource = resourceSet.getResource(enode.getEObjectURI(),true);
        MyEcoreElement element = (MyEcoreElement)inResource.getEObject(enode.getEObjectURI().fragment());
        //....  
 }


Regards


Follow me on Twitter @andreasindico

Re: How to get an EObject from an EObjectNode [message #1010603 is a reply to message #1010579] Sun, 17 February 2013 20:40 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

No. Christian told you about IUnitOfWork, which is fundamental to
correct use of Xtext. (Xtext doesn't use URIs just to make your life
difficult; it's to protect you from unsafe access to the EObjects. The
IUnitOfWork provides the safe access.)

At best your approach requires a model reload; it may not be a loadable URI.

At worst you get a stale copy.

Regards

Ed Willink

On 17/02/2013 19:01, Andrea Sindico wrote:
> This is the answer I was looking for
>
> if (selection instanceof IStructuredSelection) {
> Object first =
> ((IStructuredSelection)selection).getFirstElement();
> EObjectNode enode = (EObjectNode)first;
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource inResource =
> resourceSet.getResource(enode.getEObjectURI(),true);
> MyEcoreElement element =
> (MyEcoreElement)inResource.getEObject(enode.getEObjectURI().fragment());
> //.... }
>
> Regards
Previous Topic:XMI resource to xText resource
Next Topic:Node model reconciler and editing domain
Goto Forum:
  


Current Time: Fri Apr 19 01:55:55 GMT 2024

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

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

Back to the top