Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » Creating a context menu entry for projects in the Project Explorer (E4)
Creating a context menu entry for projects in the Project Explorer (E4) [message #1818814] Mon, 30 December 2019 00:44
Eclipse UserFriend
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:

  1. 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.
  2. 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>
    

  3. 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:

  1. 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).
  2. 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?
  3. Is it guaranteed that the ACTIVE_SELECTION is always the one that has the project as its only element?
  4. 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!
Previous Topic:Plugin code can't find "javax.inject.Inject" in rpc 2019-12
Next Topic:Dependency required and class I could extend for warning signs in import statments
Goto Forum:
  


Current Time: Sun Apr 28 07:37:05 GMT 2024

Powered by FUDForum. Page generated in 0.03549 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top