Skip to main content



      Home
Home » Modeling » Graphiti » Getting IProject instance associated to a Diagram
Getting IProject instance associated to a Diagram [message #754490] Wed, 02 November 2011 13:55 Go to next message
Eclipse UserFriend
Dear all,

I'm looking for a method to retrieve the current project using different resources. If I found methods for a text file, I don't find how to do using a diagram. In particular, when I get the diagram object from the current editor, I would like to retrieve the associated IProject object (and so, all its related information, path, etc ...).

I look at available methods in the Diagram object but didn't find any relevant information. Is there a specific method to do so ?

Thanks for any help,


Best,
Re: Getting IProject instance associated to a Diagram [message #754530 is a reply to message #754490] Wed, 02 November 2011 20:18 Go to previous messageGo to next message
Eclipse UserFriend
Hi Julien. A Diagram doesn't know about a IProject, because a Diagram is not related to Eclipse workbench concepts (you could have a Diagram that is instantiated outside an Eclipse workbench environment). Even inside Eclipse, I Diagram could be instantiated from outside a Project (or even from several projects!).
That would be rather an Editor task: your (Eclipse) editor has an Diagram and it might know from where it opened it (though, again, this could vary, this is dictated not Graphiti ). A possible way, assuming that you opened your diagram from a file inside some eclipse project, is to get the editor input and try to "adapt" to an IFile (editor.getEditorInput().adapt(IFile.class)) and, if that works, ask the Ifile for its iproject. A less clean way is to get the URI for the diagram and try to deduce from it the Project.
Re: Getting IProject instance associated to a Diagram [message #754660 is a reply to message #754530] Thu, 03 November 2011 15:17 Go to previous messageGo to next message
Eclipse UserFriend
Dear Hernan,

Thanks for your feedback. I tried your solution but unfortunately, it does not seem to work. In particular, editor.getEditorInput() does not have the adapt method. Instead, I get an getAdapter method but I don't think it is what I'm looking for ...
Re: Getting IProject instance associated to a Diagram [message #754673 is a reply to message #754660] Thu, 03 November 2011 16:06 Go to previous messageGo to next message
Eclipse UserFriend
Julien wrote on Thu, 03 November 2011 16:17
Dear Hernan,
In particular, editor.getEditorInput() does not have the adapt method. Instead, I get an getAdapter method but I don't think it is what I'm looking for ...


Yes, it is. An Adapter adapts... Razz

http://www.eclipse.org/articles/article.php?file=Article-Adapters/index.html

[Updated on: Thu, 03 November 2011 17:24] by Moderator

Re: Getting IProject instance associated to a Diagram [message #755452 is a reply to message #754673] Tue, 08 November 2011 09:59 Go to previous messageGo to next message
Eclipse UserFriend
If adapter solution would not work for you, here is my implementation of second solution that use URI method.

    public static IProject getWorkspaceProjectFromEObject(EObject eobject) {
        URI uri = EcoreUtil.getURI( eobject );
        uri = uri.trimFragment();
        // remove "platform:..." from uri
        if (uri.isPlatform()) {
            uri = URI.createURI( uri.toPlatformString( true ) );
        }

        IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
        IProject project = null;

        // try to get project from whole uri resource
        IResource resource = workspaceRoot.findMember( uri.toString() );
        if (resource != null) {
            project = resource.getProject();
        }

        // another try,by first segment with project name
        if (project == null && uri.segmentCount() > 0) {
            String projectName = uri.segment( 0 );
            IResource projectResource = workspaceRoot.findMember( projectName );
            if (projectResource != null) {
                project = projectResource.getProject();
            }
        }

        return project;   
 }
Re: Getting IProject instance associated to a Diagram [message #756425 is a reply to message #755452] Sun, 13 November 2011 03:34 Go to previous message
Eclipse UserFriend
Thanks for your code sample, it seems to work correctly !

Best,
Previous Topic:Call Direct Edit feature
Next Topic:Rotate graphical object
Goto Forum:
  


Current Time: Wed Jul 23 13:27:14 EDT 2025

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

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

Back to the top