Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Access objects displayed in editor
Access objects displayed in editor [message #201380] Mon, 11 August 2008 10:38 Go to next message
Eclipse UserFriend
Originally posted by: daylmer.opencloud.com

I have made a simple ecore model representing a basic state transition
machine. From that I have used gmf to generate the necessary files to
create the graphical editor. I can run this editor as a separate eclipse
application. I can create a new diagram and add states, transitions etc.
and it updates both the diagram and the model. My question is how do I get
hold of the Java objects that have been instantiated which represent the
various parts of the diagram. How can I write a simple class in this
running eclipse application which could access the states and transitions
in order to check them for integrity or simply print out their names on
the command line?

Thank you
Re: Access objects displayed in editor [message #201446 is a reply to message #201380] Mon, 11 August 2008 16:57 Go to previous messageGo to next message
Alexander Shatalin is currently offline Alexander ShatalinFriend
Messages: 2928
Registered: July 2009
Senior Member
Hello David,

Use EMF Resource API for loading diagra/domain model Resource + accessing
it's contents. Like:

ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.createResource(<resourceURI>);
resource.load(<EmptyMap>);
<RootModelElement> el = (<RootModelElement>) resoruce.getContents().first();

-----------------
Alex Shatalin
Re: Access objects displayed in editor [message #201555 is a reply to message #201446] Tue, 12 August 2008 09:48 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: daylmer.opencloud.com

Thank you Alex,

I have got the required libraries set up for the objects I need but I am
having a little trouble with the URI. What should I be putting in there? I'm
assuming that should be a URI referring to the diagram I have made but I'm
unsure of how to access it.

Thank you,

David Aylmer
Re: Access objects displayed in editor [message #201562 is a reply to message #201555] Tue, 12 August 2008 10:21 Go to previous messageGo to next message
Alexander Shatalin is currently offline Alexander ShatalinFriend
Messages: 2928
Registered: July 2009
Senior Member
Hello David,

Use URI.createPlatformResourceURI() method passing WS-relative path to EMF
resource ("/<poject>/<path>")
or URI.createFileURI() passing full path to the corresponding file on a file
system (like c:/folder1/file1)

-----------------
Alex Shatalin
Re: Access objects displayed in editor [message #201568 is a reply to message #201562] Tue, 12 August 2008 10:27 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: daylmer.opencloud.com

Thanks Alex,

I have tried using both of those methods and I always get a null Resource
object returned. Am I supposed to be passing in the location of the diagram
as an argument?

David Aylmer
Re: Access objects displayed in editor [message #201574 is a reply to message #201568] Tue, 12 August 2008 12:17 Go to previous messageGo to next message
Alexander Shatalin is currently offline Alexander ShatalinFriend
Messages: 2928
Registered: July 2009
Senior Member
Hello David,

> I have tried using both of those methods and I always get a null
> Resource object returned. Am I supposed to be passing in the location
> of the diagram as an argument?
Any diagram or domain model URI is ok for this call. As a result you'll get
different root resource objects (Diagram itself or diagram element from the
domain model).
You can try to debug ResourceImpl to see wich kind of URI instances are passed
to this class while loading diagram by GMF-generated editor and compare these
URI(s) with one passed by your custom code.

-----------------
Alex Shatalin
Re: Access objects displayed in editor [message #201625 is a reply to message #201574] Tue, 12 August 2008 14:59 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: daylmer.opencloud.com

Thanks Alex,

Could the problem be to do with the fact that there is no registered
factory? How would I go about registering my own factory?

I tried debugging the ResourceImpl class but the debugger doesn't seem to be
able to access the source code that it's debugging.

David Aylmer
Re: Access objects displayed in editor [message #201704 is a reply to message #201625] Wed, 13 August 2008 08:19 Go to previous messageGo to next message
Alexander Shatalin is currently offline Alexander ShatalinFriend
Messages: 2928
Registered: July 2009
Senior Member
Hello David,

I doubt the problem is in a factory. At least, GMF-generated diagram uses
the same way to create/load diagram/domain model.

-----------------
Alex Shatalin
Re: Access objects displayed in editor [message #201732 is a reply to message #201704] Wed, 13 August 2008 08:46 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: daylmer.opencloud.com

Thanks Alex,

I found this example for loading resources:

ResourceSet resourceSet = new ResourceSetImpl();

resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put(
Resource.Factory.Registry.DEFAULT_EXTENSION, new
XMIResourceFactoryImpl());

LibraryPackage libraryPackage = LibraryPackage.eINSTANCE; //*

URI fileURI = URI.createFileURI(new
File("mylibrary.xmi").getAbsolutePath());

Resource resource = resourceSet.getResource(fileURI, true);

try
{
resource.save(System.out, Collections.EMPTY_MAP);
}
catch (IOException e) {}


* So my question is how can I do this line in my plugin when it has no
knowledge of the LibraryPackage class. (Or rather my version of that class)

The errors I am getting are to do with not being able to find the ecore
file.

David Aylmer
Re: Access objects displayed in editor [message #201748 is a reply to message #201732] Wed, 13 August 2008 09:02 Go to previous messageGo to next message
Alexander Shatalin is currently offline Alexander ShatalinFriend
Messages: 2928
Registered: July 2009
Senior Member
Hello David,

First, ResourceSetImpl should be able to load resources even without explicitly
specified "ExtensionToFactoryMap".


> * So my question is how can I do this line in my plugin when it has no
> knowledge of the LibraryPackage class. (Or rather my version of that
> class)
Do not see any usage of "LibraryPackage libraryPackage" in a code snippet
above...

-----------------
Alex Shatalin
Re: Access objects displayed in editor [message #201756 is a reply to message #201748] Wed, 13 August 2008 09:08 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: daylmer.opencloud.com

Thanks Alex

> First, ResourceSetImpl should be able to load resources even without
> explicitly specified "ExtensionToFactoryMap".

The tutorial says that is required for standalone applications.

> Do not see any usage of "LibraryPackage libraryPackage" in a code snippet
> above...

The tutorial says that calling .eINSTANCE is sufficient to register it in
the package registry "which the resource uses to obtain the appropriate
metadata and factory for the model it is loading."

David Aylmer
Re: Access objects displayed in editor [message #201770 is a reply to message #201756] Wed, 13 August 2008 09:18 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: daylmer.opencloud.com

My extremely limited knowledge of GMF would suggest to me that when a user
adds nodes and connections to the diagram, the plugin is instantiating the
objects in the ecore model. I can't understand how it can be so difficult to
get hold of these objects. I thought that was the whole point of building
the graphical editor.

Constructing the diagram and saving as a xml file or equivalent and then
having to reload it to get the objects seems a crazy way of getting hold of
the objects.

David Aylmer
Re: Access objects displayed in editor [message #201788 is a reply to message #201770] Wed, 13 August 2008 10:50 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: daylmer.opencloud.com

Well, I managed to grab the objects form the diagram as per the example but
I had to include some reference libraries including the classes which make
up the source code I wrote for the model. How do I now configure the plugin
so that when I run it, it will already have those libraries?

David Aylmer
Re: Access objects displayed in editor [message #201804 is a reply to message #201770] Wed, 13 August 2008 11:42 Go to previous message
Thomas Beyer is currently offline Thomas BeyerFriend
Messages: 47
Registered: July 2009
Member
Hi David,

i am not sure, what exact approach you are trying to achieve, but
referring to your initial posting, you only want the access the domain
model objects, right?

Maybe this simple snippets helps to get the diagrameditpart and go from
there?
(this approach works only, once you want to access the wrappers of the
current diagram file, opened in the editor)
#######
IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().getActiveEditor();
if (part instanceof HmiDiagramEditor) {
HmiDiagramEditor editor = (HmiDiagramEditor) part;
DiagramEditPart dep = editor.getDiagramEditPart();
}
#######
Now you could upcast this DiagramEditPart to your application's root
EditPart and access the children via the appropriate getter-methods.

Or you could access specific GraphicalEditParts in your diagram via the
workbech-selection service as follows:
####

ISelection selection =
PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getActivePage().getActiveEditor( ).getEditorSite().getSelectionProvider().getSelection();
if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection)
selection;
Object obj = structuredSelection.getFirstElement();
if (obj instanceof GraphicalEditPart){
GraphicalEditPart ep = (GraphicalEditPart) obj;
}
}

To get the domain model object from the GraphicalEditPart you could use:
#######
GraphicalEditPart ep;
EObject domainModelEObject = ((View) ep.getModel()).getElement()
#######

If this is not, what your looking for, maybe you could provide a more
detailed description, of how you want to access the domain model and when.

HTH
Thomas



David Aylmer wrote:

> My extremely limited knowledge of GMF would suggest to me that when a user
> adds nodes and connections to the diagram, the plugin is instantiating the
> objects in the ecore model. I can't understand how it can be so difficult to
> get hold of these objects. I thought that was the whole point of building
> the graphical editor.

> Constructing the diagram and saving as a xml file or equivalent and then
> having to reload it to get the objects seems a crazy way of getting hold of
> the objects.

> David Aylmer
Previous Topic:Focus on Shortcut Container with Double Click
Next Topic:gmf bpmn tutorial
Goto Forum:
  


Current Time: Fri Apr 26 10:55:25 GMT 2024

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

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

Back to the top