Creating a context menu entry for projects in the Project Explorer (E4) [message #1818814] |
Mon, 30 December 2019 00:44 |
Eclipse User |
|
|
|
Hello everyone,
I am currently trying to build an E4 plug-in for the Eclipse IDE. It should, however, remain compatible with the 3.x-based UI code that EMF.Edit generates.
The plug-in shall extend the context menu of every project (primarily in the Project Explorer) with an additional menu item. Whenever this menu item is selected, I want a dedicated handler to be called. This handler will need to have access to the origin, i.e., the corresponding IProject object.
From a purely functional perspective, I was able to achieve this as follows:
- Create a 'fragment.e4xmi' that defines (a) a Model Fragment with a Command object and (b) a Model Fragment with the Handler that should be called. Add this fragment to the 'org.eclipse.e4.workbench.model' extension point.
- Add the following 'menuContribution' to the 'org.eclipse.ui.menus' extension point:
<menuContribution allPopups="false" locationURI="popup:org.eclipse.ui.popup.any">
<command commandId="my.command.dummy" label="Create dummy model">
<visibleWhen checkEnabled="false">
<and>
<iterate>
<instanceof value="org.eclipse.core.resources.IProject"></instanceof>
</iterate>
<count value="1"></count>
</and>
</visibleWhen>
</command>
</menuContribution>
- Make use of DI to obtain the IProject reference:
public class DummyHandler {
@Execute
public void execute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection) {
Object firstElement = selection.getFirstElement();
if (firstElement instanceof IProject) {
IProject project = (IProject)firstElement;
// ...
}
}
}
Although this seems to do the job, I feel like I am combining some 'workarounds' here and fear that this approach fails as soon as the usage scenario is a slightly different one. Therefore, I am interested in what the best practice to achieve the above-mentioned goal is.
More specifically:
- Is there a way to define the menu contribution in the Model Fragment as well? Right now, I receive the warning that 'my.command.dummy' cannot be found in the plugin.xml (although it is defined in fragment.e4xmi).
- Is it reasonable to make use of visibleWhen in this situation? Or is there an alternative way that lets me attach a command to explicitly the IProject context menu?
- Is it guaranteed that the ACTIVE_SELECTION is always the one that has the project as its only element?
- Can this approach lead to issues when 3.x-based plug-ins are added to the product?
Any help would be greatly appreciated. Thank you!
|
|
|
Powered by
FUDForum. Page generated in 0.03549 seconds