Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Getting IProject instance associated to a Diagram
Getting IProject instance associated to a Diagram [message #754490] Wed, 02 November 2011 17:55 Go to next message
Julien Mising name is currently offline Julien Mising name
Messages: 16
Registered: October 2011
Junior Member
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] Thu, 03 November 2011 00:18 Go to previous messageGo to next message
Hernan Gonzalez is currently offline Hernan Gonzalez
Messages: 181
Registered: October 2010
Location: Buenos Aires, Argentina
Senior Member
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 19:17 Go to previous messageGo to next message
Julien Mising name is currently offline Julien Mising name
Messages: 16
Registered: October 2011
Junior Member
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 20:06 Go to previous messageGo to next message
Hernan Gonzalez is currently offline Hernan Gonzalez
Messages: 181
Registered: October 2010
Location: Buenos Aires, Argentina
Senior Member
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 21:24]

Report message to a moderator

Re: Getting IProject instance associated to a Diagram [message #755452 is a reply to message #754673] Tue, 08 November 2011 14:59 Go to previous messageGo to next message
Dobrou Mising name is currently offline Dobrou Mising name
Messages: 16
Registered: December 2010
Junior Member
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 08:34 Go to previous message
Julien Mising name is currently offline Julien Mising name
Messages: 16
Registered: October 2011
Junior Member
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: Sun Sep 21 06:10:45 GMT 2014

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

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