JFace / SWT:添加带有命令的工具栏的最佳方法是什么? [英] JFace/SWT: What is the best way to add a toolbar with Commands to a Section?

查看:334
本文介绍了JFace / SWT:添加带有命令的工具栏的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Section and want to add a toolbar to it. I'm able to do it programmatically using the Actions but the requirement is to do it as much declaratively (in plugin.xml) as I can. So I'd like to define a Command and a Handler for each toolbar button but I don't know how to add them to the section's toolbar. Is there any way to do it declaratively in plugin.xml? If not, how can I do it programmatically?

谢谢!

推荐答案

p>下面是一个如何创建一个工具栏的示例,确保工具栏在 section.setClient()之前创建。

Here is a sample of how to create a toolbar for a section, make sure the toolbar is created before 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);
}

如果要从插件中添加声明的命令.xml 到工具栏,看看 CommandContributionItem

If you want to add declared commands from the plugin.xml to the toolbar, have a look at CommandContributionItem.

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

这篇关于JFace / SWT:添加带有命令的工具栏的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆