javaeclipseeclipse-pluginselectionpackage-explorer

Eclipse selection is null


In my eclipse plugin I need to get the selection in the package explorer.
I found out that this works like this:

IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISelectionService service = window.getSelectionService();
IStructuredSelection structured = (IStructuredSelection) service.getSelection("org.eclipse.jdt.ui.PackageExplorer");
Object selection = structured.getFirstElement();

This works fine in 99% of all cases but I recently ran into a case where the getSelection("org.eclipse.jdt.ui.PackageExplorer"); returns null although I can clearly see that I have something selected in the package explorer...

How can that be?


Solution

  • Check that the view is not the Project Explorer rather than Package Explorer. They can look very similar and both default to appearing in the same place.