Conversion between java.io.File and org.eclipse.core.resources.IFile [message #449742] |
Thu, 18 May 2006 13:00  |
Eclipse User |
|
|
|
Originally posted by: kammerj.bit-sys.com
In attempting to open a file for editing in a properties editor (using examples from the books referenced below, I am running into a problem where I have created a temp file (java.io.File) but need to have an IFile to pass to the org.eclipse.ui.part.FileEditorInput constructor.
How can I take a file and wrap/convert it into an IFile? Barring that, from whence can I obtain an IFile?
This seems like it ought to be easy, but I'm having one hell of a time trying to track it down. Thank in advance for your help.
-- John Kammer
Refs:
1) Eclipse: Building commercial quality plugins (2nd ed.)
2) Eclispe: Rich Client Platform: Designing, Coding and Packaging Java Application.
|
|
|
|
Re: Conversion between java.io.File and org.eclipse.core.resources.IFile [message #449846 is a reply to message #449744] |
Mon, 22 May 2006 15:22   |
Eclipse User |
|
|
|
Originally posted by: kammerj.bit-sys.com
> John Kammer wrote:
> > In attempting to open a file for editing in a
>> properties editor (using examples from the books
>> referenced below, I am running into a problem where I
>> have created a temp file (java.io.File) but need to
>> have an IFile to pass to the
>> org.eclipse.ui.part.FileEditorInput constructor.
>> >
>> > How can I take a file and wrap/convert it into an
>> IFile? Barring that, from whence can I obtain an
>> IFile?
>>
> IFiles only exist in your workspace. You can get
> them using the
> ResourcesPlugin.getWorkspace().getRoot() and methods
> around there.
>
> While you can probably turn a java.io.File that
> exists in your workspace
> into an IFile, generally you can't do the conversion.
>
> Later,
> PW
Paul,
Thanks for your reply. I find that being new to Eclipse RCP development I am wrestling with what must be a paradigm mismatch here regarding files and accessing them properly. If you (or anyone) could point me in the right direction on these separate issues I would be most thankful.
1) Opening local files. I think I have this one figured out. I can use a File Open dialog to open a file that exists locally and load that file into my editor. In this case the file is a java.io.File not an IFile, I'm not too worried about that.
2) Is there an easy way, or any way, to get such a file imported into my RCP Application's workspace once I've identified the file? And why would I either want to or specifically not want to do this? The File-to-Workspace relationship is confusing me.
3) Here's a simple concept and one for which there must be tons of code out there somewhere: Let's say my application processes foobar files. The user has a directory C:/FOO/BAR/FOOBARS in which these files are stored. I would like a simple navigator view that filters the files in this directory presenting only those with a .foobar extension. When I right click on a .foobar file in this navigator I would like the file to open up in a MyEditor. I assume to have the files listed in my navigator-like view they need to be in the workspace? Or am I totally missing the concept here?
4) If a file exists in the plugin itself I believe I can access it using something like ResourcesPlugin.getWorkspace().getRoot().getFileForLocation (IPath). This requires the org.eclipse.core.resources plugin (if I understand correctly) and at the moment I don't see the downside (if any) to that. If there is a better way to get access to these files, please let me know. I assume resource files like these to be read only.
Thanks for any help, This RCP looks to be powerful but it seems also to be a pain getting started and up to speed.
P.S. If someone out there were to write a paper or presentation covering these concepts I am sure there are a great number of people who would appreciate it.
|
|
|
Re: Conversion between java.io.File and org.eclipse.core.resources.IFile [message #449861 is a reply to message #449846] |
Tue, 23 May 2006 09:59   |
Eclipse User |
|
|
|
1) You seem to have this worked out, but basically you can wrap your
java.io.File in your own IEditorInput.
2) The action for File>Import...>File System would probably give you an
example, but basically you would create your matching IFile in your
workspace, open an InputStream on the java.io.File, and then use
IFile#setContents(*).
3) you can knock one of these together for ordinary java.io.Files using
a TreeViewer and your own ContentProvider and LabelProvider. You can
probably add filters as well. If you're in the workspace, then you can
use WorkbenchContentProvider/WorkbenchLabelProvider.
In 3.2, you can use the org.eclipse.ui.navigator to pull together most
of the functionality for navigating java.io.Files, and
org.eclipse.ui.navigator.resources if you're in the workspace.
4) It's valid to include org.eclipse.runtime.resources in an RCP app if
you want to use the workspace.
I'm sure there must be articles about resources and the workspace out
there somewhere.
Basically, the workspace "scopes" part of the filesystem. Eclipse
stores information in <workspace>/.metadata, and the workspace contains
projects like <workspace>/proj1, <workspace>/proj2, etc.
The abstraction layer for the workspace is org.eclipse.core.resources,
which uses IProject, IFolder, IFile. You get functionality like
resource change notification.
good luck.
Later,
PW
|
|
|
|
Re: Conversion between java.io.File and org.eclipse.core.resources.IFile [message #449872 is a reply to message #449742] |
Tue, 23 May 2006 14:00  |
Eclipse User |
|
|
|
Originally posted by: kammerj.bit-sys.com
> In attempting to open a file for editing in a
> properties editor (using examples from the books
> referenced below, I am running into a problem where I
> have created a temp file (java.io.File) but need to
> have an IFile to pass to the
> org.eclipse.ui.part.FileEditorInput constructor.
>
> How can I take a file and wrap/convert it into an
> IFile? Barring that, from whence can I obtain an
> IFile?
>
> This seems like it ought to be easy, but I'm having
> one hell of a time trying to track it down. Thank in
> advance for your help.
>
> -- John Kammer
>
> Refs:
> 1) Eclipse: Building commercial quality plugins (2nd
> ed.)
> 2) Eclispe: Rich Client Platform: Designing, Coding
> and Packaging Java Application.
OK, I think I found a solution to this. If for some reason this is a stupid thing to do, please feel free to let me know about it.
I can create a project within my workspace and then add in some linked resources to the project. This way the files I've linked in (or the files in the directory I've linked in) can be accessed as IFiles.
I've not tested this heavily but the simple test I have done seems to work.
-- kammer
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPathVariableManager ipvm = workspace.getPathVariableManager();
String name = "TEMP";
IPath value = new Path ("/home/tmp");
if (ipvm.validateName(name).isOK() && ipvm.validateValue(value).isOK()) {
try {
ipvm.setValue(name, value);
} catch (CoreException e) {
e.printStackTrace();
}
} else {
RuntimeException e = new RuntimeException ("Boofed up");
e.printStackTrace();
}
IFolder link = project.getFolder("newLinks");
IPath location = new Path ("TEMP");
if (workspace.validateLinkLocation(link, location).isOK()) {
try {
link.createLink(location, IResource.NONE, null);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
RuntimeException e = new RuntimeException ("Boofed up again");
e.printStackTrace();
}
try {
IResource [] resources = link.members();
System.out.println("link Folder: "+folder.getName());
System.out.println("link Members: "+folder.members().length);
for (int i = 0; i< resources.length; i++ ) {
if (resources[i] instanceof IFile) {
System.out.println("File: "+((IFile)resources[i]).getName());
} else {
System.out.println("Resource not a file: "+resources[i].getName());
}
}
} catch (CoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
|
|
|
Powered by
FUDForum. Page generated in 0.04418 seconds