Sunday, January 3, 2010

Eclipse : Tips - find a resource in workspace using visitor

public class WorkspaceResourceLocator implements IResourceProxyVisitor {
private String location;
private String resourceName;
private WorkspaceResourceLocator(String resourceLocation) {
this.location = resourceLocation;
this.resourceName = // find the resource name from the location
}
public static IResource findResource(String absolutePath) throws CoreException {
WorkspaceResourceLocator fileLocator = new WorkspaceResourceLocator(absolutePath);
ResourcesPlugin.getWorkspace().getRoot().accept(fileLocator, IResource.NONE);
return fileLocator.locatedResource;
}
public boolean visit(IResourceProxy proxy) throws CoreException {
// check if the name of the resource matches
if (proxy.getName().equals(resourceName)) {
locatedResource = proxy.requestResource();
// apply your logic to find required resource
return false;
}
return true;
}
}

No comments: