IContributionItem

时间:2017-04-10 06:48:41

标签: java eclipse-plugin e4

我有以下代码段可行,并创建一个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无法创建课程' org.eclipse.ui.internal.menus.CompatibilityWorkbenchWindowControlContribution'来自捆绑' 87'

     

!MESSAGE调度事件时的异常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

1 个答案:

答案 0 :(得分:0)

我在NPE in HandledContributionItem.canExecuteItem

中回答了这个问题

WorkbenchMenuService.populateContributionManager(ContributionManager, String)获取当前活动的工作台部分以注册菜单。因此,在此示例中,工具栏没有要为其创建活动部件。

我找到了解决这个大规模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$