Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » Comparing org.eclipse.core.resources.IFile s
Comparing org.eclipse.core.resources.IFile s [message #657138] Tue, 01 March 2011 17:25 Go to next message
Giulio is currently offline GiulioFriend
Messages: 11
Registered: February 2011
Junior Member

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 Go to previous messageGo to next message
Giulio is currently offline GiulioFriend
Messages: 11
Registered: February 2011
Junior Member

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 Go to previous message
Giulio is currently offline GiulioFriend
Messages: 11
Registered: February 2011
Junior Member

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

Previous Topic:eclipse PHP plug-in to copy fail and classes
Next Topic:Highlighting text
Goto Forum:
  


Current Time: Wed Apr 24 18:37:28 GMT 2024

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

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

Back to the top