Comparing org.eclipse.core.resources.IFile s [message #657138] |
Tue, 01 March 2011 17:25 |
|
Hi,
I'm trying to insert a selection listener in my plugin, but I'm facing the trouble that I can't match the files from the selection with the files in my domain.
After investigating I realized that the problem is that the Path of the files I get in the selection is relative to the workspace, While the Paths of the files in my domain are expressed absolute to the disk.
I've tried every method in path to try to make these two paths be equally relative, or equally absolute. Including the path.toFile().getAbsolutePath(), which resulted in d:/projectName/dir/dir/dir/filename. (which didn't even exist)
The paths on my domain are generated by:
Quote: | IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
.getProjects();
List<File> appBaseDirs = new ArrayList<File>();
for (IProject curProj : projects) {
IPath folder = curProj.getLocation();
|
I even arrived to re obtain the file from the path from the workspace, and then retranslating it to an IPath:
Quote: | IPath otherCastedPath = ResourcesPlugin.getWorkspace().getRoot()
.getFile(casted.getFullPath()).getLocation();
IPath thisFilePath = ResourcesPlugin.getWorkspace()
.getRoot().getFile(thisFile.getFullPath()).getLocation();
|
thisFilePath is a null and throws nullPointerException when I try to use it's equals(obj) method.
Will I need to make a String comparison? or is there any nice way to do it which I didn't think of?
Thanks in advance
Giulio
|
|
|
Re: Comparing org.eclipse.core.resources.IFile s [message #657289 is a reply to message #657138] |
Wed, 02 March 2011 10:49 |
|
Exploring more, the root cause on my issue is probably summarized in the following snippet:
Quote: | IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
.getProjects();
List<IPath> appBaseDirs = new ArrayList<IPath>();
for (IProject curProj : projects) {
IPath folder = curProj.getFullPath();
if (!folder.toFile().exists()) {
throw new RuntimeException(folder.toFile() + " found not existent");
}
|
This always throws the runtime exception, because the folder.toFile() method returns a File object that it's initialized with a relative path. Which is imho wrong.
Am I getting it all wrong?
thanks,
Giulio
|
|
|
Re: Comparing org.eclipse.core.resources.IFile s [message #658609 is a reply to message #657138] |
Wed, 09 March 2011 09:16 |
|
I finally solved it, by having an accessor class "MyFile"
Quote: | import org.eclipse.core.runtime.IPath;
public class MyFile {
public final IPath absolutePath;
public final IPath relativePath;
public MyFile(IPath absolute, IPath relative) {
this.absolutePath = absolute;
this.relativePath = relative;
}
public MyFile append(String subDirName) {
return new MyFile(absolutePath.append(subDirName),
relativePath.append(subDirName));
}
}
|
I initialize this with:
Quote: |
for (IProject curProj : projects)
MyFile curDir =
new MyFile(curProj.getLocation(),
curProj.getFullPath());
...
|
So I can carry around with my model both the relative and the absolute paths on my project. and use the absolute or the relative paths whenever they are needed.
Not extremely clean, but it works for me.
Thanks,
Giulio
[Updated on: Wed, 09 March 2011 09:18] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.02614 seconds