I have the following snippet that works and creates a ToolBar
with items taken from a URI inside the plugin.xml:
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IMenuService menuService = window.getService(IMenuService.class);
ToolBarManager manager = new ToolBarManager(new ToolBar(body, SWT.BORDER));
menuService.populateContributionManager(manager, "toolbar:org.eclipse.ui.main.ribbon");
manager.update(true);
I put the snippet inside a IContributionItem
and it stopped working. There is an exception:
org.eclipse.e4.core.di.InjectionException: org.eclipse.swt.SWTException: Widget is disposed
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:68)
at org.eclipse.e4.core.internal.di.InjectorImpl.processAnnotated(InjectorImpl.java:898)
at org.eclipse.e4.core.internal.di.InjectorImpl.inject(InjectorImpl.java:121)
at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:345)
at org.eclipse.e4.core.internal.di.InjectorImpl.make(InjectorImpl.java:278)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.make(ContextInjectionFactory.java:186)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.createFromBundle(ReflectionContributionFactory.java:107)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.doCreate(ReflectionContributionFactory.java:73)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.create(ReflectionContributionFactory.java:50)
at org.eclipse.e4.ui.workbench.renderers.swt.ToolControlContribution.createControl(ToolControlContribution.java:58)
at org.eclipse.jface.action.ControlContribution.fill(ControlContribution.java:101)
at org.eclipse.jface.action.ToolBarManager.update(ToolBarManager.java:343)
at org.eclipse.e4.ui.workbench.renderers.swt.ToolBarManagerRenderer.subscribeTopicUpdateToBeRendered(ToolBarManagerRenderer.java:212)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
Caused by: org.eclipse.swt.SWTException: Widget is disposed
at org.eclipse.swt.SWT.error(SWT.java:4491)
at org.eclipse.swt.SWT.error(SWT.java:4406)
at org.eclipse.swt.SWT.error(SWT.java:4377)
at org.eclipse.swt.widgets.Widget.error(Widget.java:482)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:354)
at org.eclipse.swt.widgets.Widget.addDisposeListener(Widget.java:224)
at org.eclipse.e4.ui.workbench.renderers.swt.ToolBarManagerRenderer.addCleanupDisposeListener(ToolBarManagerRenderer.java:420)
at org.eclipse.e4.ui.workbench.renderers.swt.ToolBarManagerRenderer.processContribution(ToolBarManagerRenderer.java:406)
at org.eclipse.ui.internal.menus.WorkbenchMenuService.populateContributionManager(WorkbenchMenuService.java:342)
at org.eclipse.ui.internal.menus.SlaveMenuService.populateContributionManager(SlaveMenuService.java:90)
at org.eclipse.ui.internal.menus.SlaveMenuService.populateContributionManager(SlaveMenuService.java:81)
at org.acme.project.RibbonContribution.createControl(RibbonContribution.java:29)
As far as I can tell, no control is disposed (and why should it? The exception gets thrown at the start of the application).
The console also shows the following messages:
!MESSAGE Unable to create class 'org.eclipse.ui.internal.menus.CompatibilityWorkbenchWindowControlContribution' from bundle '87'
!MESSAGE Exception while dispatching event org.osgi.service.event.Event [topic=org/eclipse/e4/ui/model/ui/UIElement/visible/SET] {ChangedElement=org.eclipse.e4.ui.model.application.ui.menu.impl.ToolControlImpl@b606cb6 (elementId: org.eclipse.core.internal.registry.ConfigurationElementHandle@3f, tags: null, contributorURI: null) (widget: null, renderer: org.eclipse.e4.ui.workbench.renderers.swt.ToolControlRenderer@5b9396d3, toBeRendered: true, onTop: false, visible: true, containerData: null, accessibilityPhrase: null) (contributionURI: bundleclass://org.eclipse.ui.workbench/org.eclipse.ui.internal.menus.CompatibilityWorkbenchWindowControlContribution, object: null), Widget=null, AttName=visible, NewValue=true, EventType=SET, OldValue=false} to handler org.eclipse.e4.ui.internal.di.UIEventObjectSupplier$UIEventHandler@24c8d8be
What is going on? How do I fix this? How can I add a ToolBar
to an IContributionItem
?
I answered this problem in NPE in HandledContributionItem.canExecuteItem
WorkbenchMenuService.populateContributionManager(ContributionManager, String)
takes the currently active workbench part to register the menu for. So in this example, the toolbar has no active part to be created for.
I found a workaround to fix this massive E4 bug. The call to populateContributionManager
must be replaced with the following lines:
final MPart part = getSite().getService(MPart.class);
String menuLocation = "toolbar:org.acme.menu";
if (menuService instanceof WorkbenchMenuService) {
((WorkbenchMenuService) menuService).populateContributionManager(part, manager, menuLocation);
} else if (menuService instanceof SlaveMenuService) {
((SlaveMenuService) menuService).populateContributionManager(part, manager, menuLocation);
} else
throw new UnsupportedOperationException("Do not know how to handle " + menuService); //$NON-NLS-1$