【问题标题】:JFace/SWT: What is the best way to add a toolbar with Commands to a Section?JFace/SWT:将带有命令的工具栏添加到部分的最佳方法是什么?
【发布时间】:2014-03-18 02:20:46
【问题描述】:

我有一个部分,想给它添加一个工具栏。我可以使用 Actions 以编程方式执行此操作,但要求是尽可能多地以声明方式(在 plugin.xml 中)执行此操作。所以我想为每个工具栏按钮定义一个命令和一个处理程序,但我不知道如何将它们添加到该部分的工具栏。有什么方法可以在 plugin.xml 中以声明方式进行吗?如果没有,我该如何以编程方式进行?

谢谢!

【问题讨论】:

    标签: java eclipse swt jface plugin.xml


    【解决方案1】:

    我认为您必须编写自己的扩展点来定义 plugin.xml 中的内容,然后编写代码来访问扩展点注册表以获取声明的扩展并根据信息创建工具栏。

    有关更多详细信息,请参阅Eclipse Extension Points and Extensions

    【讨论】:

    • 非常感谢,如果一切都失败了,那将是最后的手段:)
    【解决方案2】:

    你需要看看如何使用org.eclipse.ui.menus extension点。它支持向菜单/弹出/工具栏/修剪添加命令/小部件。

    //contributing to local toolbar
    
    ToolBarManager localToolBarmanager = new ToolBarManager();
    IMenuService menuService = (IMenuService) PlatformUI.getWorkbench().getService(IMenuService.class);
    menuService.populateContributionManager(localToolBarmanager,
        "toolbar:localtoolbar");  //id of your local toolbar
    localToolBarmanager.createControl(control);
    

    【讨论】:

    • 谢谢,确实如此。但是我需要将命令添加到既不是主工具栏也不是视图工具栏而是以编程方式创建的自定义工具栏的工具栏。我可以使用这个扩展以某种方式做到这一点吗?
    • 我刚刚更新了我的答案。看看。它应该读取为具有给定本地工具栏 ID 的菜单扩展定义的贡献
    【解决方案3】:

    这里是一个如何为部分创建工具栏的示例,请确保在section.setClient()之前创建工具栏。

    protected void createToolbar(Section section) {
        ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
        toolBarManager.add(new Action("print") {
            @Override
            public void run() {
                System.out.println("PRINT");
            }
        });
        createSectionToolbar(section, toolBarManager);
    }
    
    /**
     * create a toolbar in the passed section
     * 
     * @param section
     * @param toolBarManager
     */
    protected void createSectionToolbar(Section section, ToolBarManager toolBarManager) {
        Composite toolbarComposite = toolkit.createComposite(section);
        toolbarComposite.setBackground(null);
        toolBarManager.createControl(toolbarComposite);
        section.clientVerticalSpacing = 0;
        section.descriptionVerticalSpacing = 0;
        section.setTextClient(toolbarComposite);
    }
    

    如果您想将声明的命令从plugin.xml 添加到工具栏,请查看CommandContributionItem

    toolBarManager.add(new CommandContributionItem(new CommandContributionItemParameter(getSite(), "id", "commandId", SWT.NONE)));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多