Skip to content

Instantly share code, notes, and snippets.

@suxiaogang
Last active December 15, 2015 15:39
Show Gist options
  • Save suxiaogang/5283720 to your computer and use it in GitHub Desktop.
Save suxiaogang/5283720 to your computer and use it in GitHub Desktop.
getCurrentProject in plugin dev
public static IProject getCurrentProject(){
ISelectionService selectionService =
Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();
ISelection selection = selectionService.getSelection();
IProject project = null;
if(selection instanceof IStructuredSelection) {
Object element = ((IStructuredSelection)selection).getFirstElement();
if (element instanceof IResource) {
project= ((IResource)element).getProject();
} else if (element instanceof PackageFragmentRootContainer) {
IJavaProject jProject =
((PackageFragmentRootContainer)element).getJavaProject();
project = jProject.getProject();
} else if (element instanceof IJavaElement) {
IJavaProject jProject= ((IJavaElement)element).getJavaProject();
project = jProject.getProject();
}
}
return project;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment