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 #492456] 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 #492472 is a reply to message #492456] 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 #492479 is a reply to message #492472] Tue, 20 October 2009 15:03 Go to previous messageGo to next message
Erwan  is currently offline Erwan Friend
Messages: 8
Registered: October 2009
Junior Member
Hi Brian,

Thank you for your response. I will look at the pointers you gave me.

To give details about what I am trying to do: I'm writing a plugin for a web framework. In some Java classes there are calls to a "render()" method that will render a template. I want to give an easy access to the template file from the line that contains the call to the render method.

So when the user calls my command (actually it is a command, not an action), I parse the code of the Java file (from the current editor window) and determine the relative URL of my template, for example "templates/index.html". I have that working. Now all I need to do is open automatically that file in a new editor (or focus the existing editor if already open).

Also, while I can walk the open projects, I'm not sure if I can easily identify the one who owns the file the user was interacting when invoking the command...

[Updated on: Tue, 20 October 2009 15:05]

Report message to a moderator

Re: Get a file from the workspace? [message #492687 is a reply to message #492456] 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 #493213 is a reply to message #492479] Fri, 23 October 2009 15:34 Go to previous messageGo to next message
Brian de Alwis is currently offline Brian de AlwisFriend
Messages: 242
Registered: July 2009
Senior Member
Erwan wrote:
> So when the user calls my command (actually it is a command, not an
> action), I parse the code of the Java file (from the current editor
> window) and determine the relative URL of my template, for example
> "templates/index.html". I have that working. Now all I need to do is
> open automatically that file in a new editor (or focus the existing
> editor if already open).

So the template is being pulled in from the same project? Then you
should be able to find the active editor (IEditorPart), and obtain its
editor input (IEditorInput). Providing it's an IFileEditorInput, you
can query for its IFile, and thence obtain its IProject.

There are other types of IEditorInput, and you may choose to handle them
too.

Brian.
Re: Get a file from the workspace? [message #493435 is a reply to message #492456] 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().
Re: Get a file from the workspace? [message #602663 is a reply to message #492472] Tue, 20 October 2009 15:03 Go to previous message
Erwan  is currently offline Erwan Friend
Messages: 8
Registered: October 2009
Junior Member
Hi Brian,

Thank you for your response. I will look at the pointers you gave me.

To give details about what I am trying to do: I'm writing a plugin for a web framework. In some Java classes there are calls to a "render()" method that will render a template. I want to give an easy access to the template file from the line that contains the call to the render method.

So when the user calls my command (actually it is a command, not an action), I parse the code of the Java file (from the current editor window) and determine the relative URL of my template, for example "templates/index.html". I have that working. Now all I need to do is open automatically that file in a new editor (or focus the existing editor if already open).
Re: Get a file from the workspace? [message #602845 is a reply to message #492479] Fri, 23 October 2009 15:34 Go to previous message
Brian de Alwis is currently offline Brian de AlwisFriend
Messages: 242
Registered: July 2009
Senior Member
Erwan wrote:
> So when the user calls my command (actually it is a command, not an
> action), I parse the code of the Java file (from the current editor
> window) and determine the relative URL of my template, for example
> "templates/index.html". I have that working. Now all I need to do is
> open automatically that file in a new editor (or focus the existing
> editor if already open).

So the template is being pulled in from the same project? Then you
should be able to find the active editor (IEditorPart), and obtain its
editor input (IEditorInput). Providing it's an IFileEditorInput, you
can query for its IFile, and thence obtain its IProject.

There are other types of IEditorInput, and you may choose to handle them
too.

Brian.
Previous Topic:Programmatically adding a plug-in dependency
Next Topic:Get a file from the workspace?
Goto Forum:
  


Current Time: Tue Aug 11 20:26:19 GMT 2020

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

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

Back to the top