IContributionItem 中的工具栏
ToolBar in IContributionItem
我有以下代码片段可以使用并创建一个 ToolBar
,其中包含从 plugin.xml:
中的 URI 中获取的项目
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);
我将代码段放在 IContributionItem
中,但它停止工作了。有一个例外:
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)
据我所知,没有处理任何控件(为什么要处理?在应用程序启动时抛出异常)。
控制台还显示以下消息:
!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
这是怎么回事?我该如何解决?如何将 ToolBar
添加到 IContributionItem
?
我在
中回答了这个问题
WorkbenchMenuService.populateContributionManager(ContributionManager, String)
使用当前活动的 workbench 部分来注册菜单。所以在这个例子中,工具栏没有要创建的活动部分。
我找到了修复这个巨大的 E4 错误的解决方法。必须将对 populateContributionManager
的调用替换为以下行:
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$
我有以下代码片段可以使用并创建一个 ToolBar
,其中包含从 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);
我将代码段放在 IContributionItem
中,但它停止工作了。有一个例外:
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)
据我所知,没有处理任何控件(为什么要处理?在应用程序启动时抛出异常)。
控制台还显示以下消息:
!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
这是怎么回事?我该如何解决?如何将 ToolBar
添加到 IContributionItem
?
我在
WorkbenchMenuService.populateContributionManager(ContributionManager, String)
使用当前活动的 workbench 部分来注册菜单。所以在这个例子中,工具栏没有要创建的活动部分。
我找到了修复这个巨大的 E4 错误的解决方法。必须将对 populateContributionManager
的调用替换为以下行:
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$