IContributionItem中的ToolBar

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IContributionItem中的ToolBar相关的知识,希望对你有一定的参考价值。

我有以下代码片段,可以创建一个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无法从包'87'创建类'org.eclipse.ui.internal.menus.CompatibilityWorkbenchWindowControlContribution'

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

答案

我在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$

以上是关于IContributionItem中的ToolBar的主要内容,如果未能解决你的问题,请参考以下文章

android-基础编程-ToolBar

jQuery 可拖动 IFrameFix

安卓Design包之Toolbar控件的使用

vuejs-framework7工具栏路由

CRM WEB UI 04明细界面添加按钮

带有appbar_scrolling_behaviour的appbar下方的BottomSheet