I am writing a simple eclipse plugin, but I have a problem: when a user right-clicks on a node (possibly a project, file, java compilation unit or others), I want to get the project it belongs to.
Code example:
public class MyAction implements IObjectActionDelegate { private IProject project; public void selectionChanged(IAction action, ISelection selection) { this.project = getSelectedProject(selection); } public static IProject getSelectedProject(Object obj) throws Exception { if (obj == null) { return null; } if (obj instanceof IResource) { return ((IResource) obj).getProject(); } else if (obj instanceof IStructuredSelection) { return getSelectedProject(((IStructuredSelection) obj).getFirstElement()); } return null; } }
It works in most cases, but sometimes, for example, I right-click on a java file, the choice will be ICompilationUnit . Although I can add more if to getSelectedProject , but I don't think this is a good idea.
Is there a way to get the project of the selected object that was selected? I do not want to add them one by one.
source share