I am writing an Eclipse plugin that will display a menu item in the context menu for a Java Project. I have written the plugin.xml as follows:
<plugin>
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="popup:org.eclipse.jdt.ui.PackageExplorer">
<dynamic
class="uk.co.dajohnston.plugin.classpathswitcher.menu.MenuContribution"
id="uk.co.dajohnston.plugin.classpathSwitcher.switchMenuContribution">
<visibleWhen>
<with
variable="activeMenuSelection">
<iterate>
<adapt
type="org.eclipse.jdt.core.IJavaProject">
</adapt>
</iterate>
<count
value="1">
</count>
</with>
</visibleWhen>
</dynamic>
</menuContribution>
</extension>
</plugin>
So I am now trying to write the MenuContribution
class which extends CompoundContributionItem
so that I can create a dynamic menu and the contents of this menu are going to be based on a set of files that exist in the Java Project's root directory. But I am stuck trying to get the path to the root directory from within the getContributionItems
method.
Based on the plugin.xml file I can be guarenteed that the method will only be called if a single Java Project is selected, so all I need to do is get the current selection and then get its absolute path. Any ideas? Or is there a better way to do this?
This should get you closer (if not solve it completely for you ;))
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window != null)
{
IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
Object firstElement = selection.getFirstElement();
if (firstElement instanceof IAdaptable)
{
IProject project = (IProject)((IAdaptable)firstElement).getAdapter(IProject.class);
IPath path = project.getFullPath();
System.out.println(path);
}
}