1

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

谢谢!

4

3 回答 3

1

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

有关更多详细信息,请参阅Eclipse 扩展点和扩展。

于 2014-02-18T11:46:14.890 回答
1

这是一个如何为部分创建工具栏的示例,请确保工具栏是在之前创建的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)));
于 2014-02-25T15:33:08.027 回答
1

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

//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);
于 2014-02-18T21:31:58.933 回答