Get a file from the workspace? [message #492456] |
Tue, 20 October 2009 10:05  |
Eclipse User |
|
|
|
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 #493435 is a reply to message #492456] |
Mon, 26 October 2009 05:30  |
Eclipse User |
|
|
|
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 11:03  |
Eclipse User |
|
|
|
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 11:34  |
Eclipse User |
|
|
|
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.
|
|
|
Powered by
FUDForum. Page generated in 0.04571 seconds