How to get the parent module of project. Here is my code works fine in Eclipse PDE. But when I test the plugin(installing in eclipse) using test application by selecting the child module this condition (if (projectRoot == selectedResource)) is coming true and it return src and target as child modules which is incorrect. Any suggestion on how to get the parent module of project.
IResource selectedResource = Resource.getSelectedProject(); // this return selected module (F/parent/child1)
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); // \R
// this is not returning parent module??
IProject projectRoot = root.getProject(selectedResource.getProject().getName());
List<IResource> childModules = new ArrayList<>();
if (projectRoot == selectedResource) { // this is coming true (parent != child)
IProject project = FileResource.getProject(selectedResource);
childModules = Resource.getChildModules(project);
} else {
childModules.add(selectedResource);
}
Resource.Class
private static IResource selectedResource;
public static void setSelectedResource(IResource resource) {
selectedResource = resource;
}
public static IResource getSelectedProject() {
return selectedResource;
}
An Eclipse workspace is organised like this:
IFolder
in turn can contain a mixture of moreIFolder
andIFile
resources.IWorkspaceRoot
,IProject
, andIFolder
all extendIContainer
.IContainer
andIFile
extendIResource
.The
IResource.getProject
method always returns the topIProject
resource (except forIWorkspaceRoot
).IResource.getParent
returns the immediateIContainer
parent (null forIWorkspaceRoot
)So if you want the parent container of a resource call
IResource.getParent
, if you want the top level containing project callIResource.getProject
.