1

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;
 }
Subash J
  • 2,028
  • 3
  • 13
  • 27
Maana
  • 640
  • 3
  • 9
  • 22

1 Answers1

1

An Eclipse workspace is organised like this:

IWorkspaceRoot
 - IProject
   - mixture of IFolder and IFile
 - IProject
   - mixture of IFolder and IFile
 - ... more IProjects

IFolder in turn can contain a mixture of more IFolder and IFile resources.

IWorkspaceRoot, IProject, and IFolder all extend IContainer. IContainer and IFile extend IResource.

The IResource.getProject method always returns the top IProject resource (except for IWorkspaceRoot).

IResource.getParent returns the immediate IContainer parent (null for IWorkspaceRoot)

So if you want the parent container of a resource call IResource.getParent, if you want the top level containing project call IResource.getProject.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Quick question here, so for a given project which has a submodule (defined in parent pom.xml) will that submodule is IProject or IFolder. For example below. Student //IProject and its submodule is Department will it be a Ifolder or IProject ? – Maana Oct 11 '22 at 15:37
  • maven modules are nothing whatsoever to do with Eclipse APIs, the are completely unrelated things. If you want to work with maven pom.xml so should use maven APIs. – greg-449 Oct 11 '22 at 16:34
  • Actually, I am testing my Eclipse API plugin in maven based project. – Maana Oct 11 '22 at 17:07
  • Fine, but that doesn't mean that the modules are related to IFolder/IProject at all. maven just sees file system folders, some of them might be IFolders, some might be IProjects. – greg-449 Oct 11 '22 at 17:21
  • Thaks for clarification Greg – Maana Oct 11 '22 at 17:46
  • @Maana IFolder/IProject type is general for all the project type and it is not specific to Maven projects. If you would like to filter the maven project type then you might have to write custom conditions to do so. – Subash J Oct 13 '22 at 10:23
  • @Maana for further queries you can reach out to me through LinkedIn https://www.linkedin.com/in/subashjanarthanan/ – Subash J Nov 21 '22 at 08:07