Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » Get a file from the workspace?
Get a file from the workspace? [message #602650] Tue, 20 October 2009 14:05 Go to next message
Erwan  is currently offline Erwan Friend
Messages: 8
Registered: October 2009
Junior Member
Hi,

In an Eclipse plugin, from a command, I'm trying to open in an editor a file from the workspace. So far I found this page:
http://wiki.eclipse.org/FAQ_How_do_I_open_an_editor_on_a_fil e_in_the_workspace%3F

...But it doesn't tell me how I can get an IFile. It seems like I could get it from IProject.getFile(String name); but the problem is I can't find how to get to the IProject I want. That would be the project that owns the editor where the action was initiated (I actually managed to get the ITextEditor from the event).

So, I can I get the IFile, or the IProject? I've read that we can get it from IStructuredSelection but all I manage to get is ITextSelection - that is, I get the selection from inside the text editor but no other selection.

Thank you!
Re: Get a file from the workspace? [message #602660 is a reply to message #602650] Tue, 20 October 2009 14:50 Go to previous messageGo to next message
Brian de Alwis is currently offline Brian de AlwisFriend
Messages: 242
Registered: July 2009
Senior Member
Hi Erwan.

Erwan wrote:
> ..But it doesn't tell me how I can get an IFile. It seems like I could
> get it from IProject.getFile(String name); but the problem is I can't
> find how to get to the IProject I want. That would be the project that
> owns the editor where the action was initiated (I actually managed to
> get the ITextEditor from the event).
>
> So, I can I get the IFile, or the IProject? I've read that we can get it
> from IStructuredSelection but all I manage to get is ITextSelection -
> that is, I get the selection from inside the text editor but no other
> selection.

You haven't really provided enough context to provide detailed help.

How is your action being invoked? How when will you know that your
plugin needs to open this file? Is it through a context-menu on a
project? From an editor on an existing file? (I'm guessing the latter,
since you're receiving a text selection)

IObjectActionDelegate will be sent a selectionChanged() event that
provides a selection. This is usually an IStructuredSelection which
will contain the selected objects.

IEditorActionDelegate will have sent setActiveEditor() on any editor
changes.

If you know there's should be a particular file in some project, you can
walk the available projects with

ResourcesPlugin.getWorkspace().getRoot().getProjects()

Brian.
Re: Get a file from the workspace? [message #602729 is a reply to message #602650] Wed, 21 October 2009 12:09 Go to previous messageGo to next message
Erwan  is currently offline Erwan Friend
Messages: 8
Registered: October 2009
Junior Member
I managed to do it using getProjects, looping through projects to find the file with the correct name:

IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
IFile file = null;
for (IProject project: projects) {
file = project.getFile(filename);
if (file != null)
return file;
}
return null;

It's not bullet proof because there could be 2 files with the same relative path in 2 different projects, but it's good enough for now.
Re: Get a file from the workspace? [message #602905 is a reply to message #602650] Mon, 26 October 2009 09:30 Go to previous message
Erwan  is currently offline Erwan Friend
Messages: 8
Registered: October 2009
Junior Member
Indeed, I'm getting the project from the IFile (by going up with getParent() until I get an IProject).

Also, the code I posted above doesn't really work because when the file doesn't exist, it doesn't return null but an IFile instance. Instead I have to check existence with IFile.exists().
Previous Topic:Get a file from the workspace?
Next Topic:Use eclipsec.exe when building headless RCP
Goto Forum:
  


Current Time: Fri Apr 19 03:08:14 GMT 2024

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

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

Back to the top