AbstractContributionFactory for creating menuContributions [message #675278] |
Tue, 31 May 2011 02:45 |
John Steele Messages: 33 Registered: June 2010 Location: Seattle, WA |
Member |
|
|
Hello,
I'm trying to understand how the MenuService, MenuManger, CommandContributionItem, and AbstractContributionFactories work together.
I am fully aware of, and understand how to add a menuContribution via the plugin.xml, and how to add commands and handlers to it.
I appreciate the extension point, but I'd like to learn how to do it programmatically, and the different ways of doing it.
Here is one scenario where I add a menu to a menu programmatically:
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
...
MenuManager fileMenu;
MenuManager searchSubMenu;
...
protected void fillMenuBar(IMenuManager menuBar) {
fileMenu = new MenuManager("&File", "file.menu.id");
fileMenu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
menuBar.add(fileMenu);
IMenuService menuService = (IMenuService) PlatformUI.getWorkbench().getService(IMenuService.class);
AbstractContributionFactory searchContribution = new AbstractContributionFactory
("menu:file.menu.id?after=additions",Activator.PLUGIN_ID) {
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
searchSubMenu = new MenuManager("Sea&rch", "mymenu");
CommandContributionItemParameter itemParam =
new CommandContributionItemParameter(serviceLocator, "my.id", distributed.dev.ide.commands.newwizardcommand",
SWT.PUSH);
CommandContributionItem item = new CommandContributionItem(itemParam);
searchSubMenu.add(item);
additions.addContributionItem(searchSubMenu, null);
};
};
menuService.addContributionFactory(searchContribution);
}
@Override
public void dispose() {
/**
* MenuManagers are not disposed, so dispose it.
* See http://hexapixel.com/2009/05/22/menumanagers-and-you
*/
if (searchSubMenu != null) {
searchSubMenu.dispose();
searchSubMenu = null;
}
if (fileMenu != null) {
fileMenu.dispose();
fileMenu = null;
}
super.dispose();
}
} // end ApplicationActionBarAdvisor
This is my understanding so far (I would really appreciate feedback on where I go wrong):
- The IMenuService from the workbench allows me to add contribution manager factories (e.g., AbstractContributionFactory, ExtensionContributionFactory). These factories are then managed by the MenuService via their id (not sure about this, but in the above example it would be Activator.PLUGIN_ID).
- Within the ContributionFactory.createContributionItems(IServiceLocator, IContributionRoot) is where I add all the sub-menus, commands, etc, within the location-URI of the ContributionFactory.
- The IMenuManager passed to protected void fillMenuBar(IMenuManager menuBar) is reponsible for managing any ContibutionItems that are added.
When I create a AbstractContributionFactory and add contributions as I did above, is this the same thing as if I were to add the extension org.eclipse.ui.menus to the plugin.xml and add menus, etc to?
I'm still don't fully understand how the MenuManger, MenuService, ContributionFactories interact and work.
I would really appreciate feedback.
Thanks.
|
|
|
Powered by
FUDForum. Page generated in 0.06684 seconds