【问题标题】:Creating own toolbar leveraging existing org.eclipse.ui.menus extension利用现有的 org.eclipse.ui.menus 扩展创建自己的工具栏
【发布时间】:2011-12-22 09:53:45
【问题描述】:

我有 Eclipse 的自定义编辑器。由于特殊原因,此编辑器提供了两个工具栏区域,它们不是基于 eclipse 为编辑器提供的标准操作栏。这两个地方是专门供其他插件贡献的。我的意图是利用带有自定义menuContribution/locationURI 的“org.eclipse.ui.menus”扩展点,以便其他插件可以使用此扩展并关联toolbar:my.editor.toolbar1toolbar:my.editor.toolbar2 作为locationURI

我的问题是如何将我的工具栏与特定位置“连接”。我尝试了以下方法,但结果并不好。如果我不应该创建自定义ToolbarContributionRoot 事件,我还创建了扩展ExtensionContributionFactoryCustomContributionFactory。它工作得很好,但问题在于子菜单没有正确解决的下拉命令。

    toolbarManager = new ToolBarManager(SWT.FLAT);                                                        
ToolbarContributionRoot toolbarRoot = new ToolbarContributionRoot(toolbarManager);                    

IServiceLocator workbench = PlatformUI.getWorkbench();                                                

IConfigurationElement[] allMenuElements                                                               
        = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.ui.menus");        
for (IConfigurationElement menuContribution : allMenuElements) {                                      
    String locationURI = menuContribution.getAttribute("locationURI");                                

    if ("toolbar:my.editor.toolbar1".equals(locationURI)) {                                                       
        try {                                                                                         
            ExtensionContributionFactory factory = CustomContributionFactory.create(menuContribution);
            factory.createContributionItems(workbench, toolbarRoot);                                  
        } catch (CoreException e) {                                                                   
            e.printStackTrace();                                                                      
        }                                                                                             
    }                                                                                                 
}                                                                                                     


toolbar = toolbarManager.createControl(root);                                                         
GridData gridData = new GridData(GridData.FILL, GridData.FILL, false, false, 1, 1);                   
toolbar.setLayoutData(gridData);                                                                      
toolbar.pack();                                                                                       

“用户”的plugin.xml 如下所示:

<extension point="org.eclipse.ui.menus" id="my.helper.id">
    <menuContribution locationURI="toolbar:my.editor.toolbar1">
        <command commandId="my.editor.special.command1" />...

您对如何将我的自定义工具栏和"org.eclipse.ui.menus" 扩展名混合在一起有什么建议吗?

【问题讨论】:

    标签: eclipse eclipse-plugin


    【解决方案1】:

    正确的做法是:

    toolbarManager = new ToolBarManager(SWT.FLAT);                
    IServiceLocator workbench = PlatformUI.getWorkbench();
    IMenuService menuService = (IMenuService) workbench.getService(IMenuService.class);
    menuService.populateContributionManager(toolbarManager, TOOLBAR_LOCATION);
    
    toolbar = toolbarManager.createControl(root);
    

    【讨论】:

    • 我正在尝试使用此代码,但它不起作用,因为从调用 workbench.getService(IMenuService.class) 返回的 WorkbenchMenuService 版本存在缺陷。缺陷是方法 populateContributionManager(toolbarManager, TOOLBAR_​​LOCATION) 未实现,并且其中包含 //TODO 消息。这个未实现的方法来自 org.eclipse.ui.workbench_3.104.0.v20130204_164612.jar 你是如何让你的代码工作的?
    • 这段代码使用 Eclipse 的 Indigo 版本完美运行。但到目前为止对朱诺不起作用。
    猜你喜欢
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多