Add Custom Actions to the Contextual Menu?
Question
How do I add my own custom actions to the contextual menu using an API?
Answer
The API methods WSAuthorEditorPageBase.addPopUpMenuCustomizer
and
WSTextEditorPage.addPopUpMenuCustomizer
allow you to customize the
contextual menu shown either in the Author or
Text modes. The API is available both in the standalone application
and in the Eclipse plugin.
Here is an elegant way to add actions to the Author page from your Eclipse plugin extension:
- Create a pop-up menu customizer
implementation:
import org.eclipse.jface.action.ContributionManager; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.menus.IMenuService; import ro.sync.ecss.extensions.api.AuthorAccess; import ro.sync.ecss.extensions.api.structure.AuthorPopupMenuCustomizer; /** * This class is used to create the possibility to attach certain * menuContributions to the {@link ContributionManager}, which is used for the * popup menu in the Author Page of the Oxygen Editor.<br /> * You just need to use the org.eclipse.ui.menus extension and add a * menuContribution with the locationURI: <b>menu:oxygen.authorpage</b> */ public class OxygenAuthorPagePopupMenuCustomizer implements AuthorPopupMenuCustomizer { @Override public void customizePopUpMenu(Object menuManagerObj, AuthorAccess authoraccess) { if (menuManagerObj instanceof ContributionManager) { ContributionManager contributionManager = (ContributionManager) menuManagerObj; IMenuService menuService = (IMenuService) PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getService(IMenuService.class); menuService.populateContributionManager(contributionManager, "menu:oxygen.authorpage"); contributionManager.update(true); } } }
- Add a workbench listener and add the pop-up customizer when an editor is opened in
the
Author
page:
Workbench.getInstance().getActiveWorkbenchWindow().getPartService() .addPartListener( new IPartListener() { @Override public void partOpened(IWorkbenchPart part) { if(part instanceof ro.sync.exml.workspace.api.editor.WSEditor) { WSEditorPage currentPage = ((WSEditor)part).getCurrentPage(); if(currentPage instanceof WSAuthorEditorPage) { ((WSAuthorEditorPage)currentPage).addPopUpMenuCustomizer (new OxygenAuthorPagePopupMenuCustomizer()); } } } ........ });
- Implement the extension point in your
plugin.xml
:<extension point="org.eclipse.ui.menus"> <menuContribution allPopups="false" locationURI="menu:oxygen.authorpage"> <command commandId="eu.doccenter.kgu.client.tagging.removeTaggingFromOxygen" style="push"> </command> </menuContribution> </extension>