Reputation: 488
I'm trying to implement a refresh algorithm for an Eclipse plugin and can't find a way to handle linked folders.
To sum it up I have some features in my application which generate some files in the same project I work in. In order to see those files I usually have to press F5. However I tried implementing a refresh algorithm so I don't have to press F5 anymore and the refresh to be made automatically.
Here is a part of my algorithm
File osFile = iPath.toFile();
if (osFile.exists()) {
if (osFile.isFile()) {
IFile[] filesArr = workspaceRoot
.findFilesForLocationURI(iPath.toFile().toURI());
if (filesArr != null && filesArr.length > 0) {
for (IFile file : filesArr) {
try {
file.refreshLocal(IResource.DEPTH_INFINITE,
new NullProgressMonitor());
} catch (CoreException e) {
Activator.getDefault().log(IStatus.ERROR,
e.getLocalizedMessage(), e);
}
}
}
} else if (osFile.isDirectory()) {
iPath = iPath.makeRelativeTo(workspaceRoot.getLocation());
IResource resource = null;
if (iPath.segmentCount() == 1) {
resource = workspaceRoot.getProject(iPath.toString());
} else {
try {
resource = workspaceRoot.getFolder(iPath);
} catch (Exception e) {
Activator.getDefault().log(IStatus.ERROR,
e.getLocalizedMessage(), e);
}
}
try {
if (resource != null) {
resource.refreshLocal(IResource.DEPTH_INFINITE,
new NullProgressMonitor());
}
} catch (CoreException e) {
Activator.getDefault().log(IStatus.ERROR,
e.getLocalizedMessage(), e);
}
}
Where iPath is the absolute path (C:......) to the file or folder I want to refresh.
For individual files and for non-linked folders this works great, however when it comes to linked folders it fails. The problem comes at this very line:
iPath = iPath.makeRelativeTo(workspaceRoot.getLocation());
Where at a non-linked folder it will transform an absolute path(C:......) to an Eclipse local path (MyProject/tmp/destinationFolder), for a linked folder it will just get the absolute path on the OS.
Then the following line of code:
resource = workspaceRoot.getFolder(iPath);
Will remove the "C:\" from the path and concatenate it to a tentative eclipse path (resulting something like F/user/desktop/project/folder) ultimately pointing to nothing.
So, can anyone help me refactor my algorithm in order to correctly handle linked folders? There might be some methods or classes I'm not familiar with and missing out on, that might deal with exactly this kind of issues.
Upvotes: 0
Views: 319
Reputation: 111216
IWorkspaceRoot
has findContainersForLocationURI
which returns the container for a location URI (container is either a folder or a project).
Upvotes: 1