将Eclipse命令绑定/绑定到swt按钮

时间:2011-01-19 16:47:34

标签: eclipse command swt

在Eclipse中,您可以使用菜单贡献来添加将调用命令的工具栏按钮和菜单。除了以编程方式调用命令onclick之外,有没有办法对普通的swt按钮执行此操作?

3 个答案:

答案 0 :(得分:5)

button.addSelectionListener(new SelectionAdapter() {

    @Override
    public void widgetSelected(SelectionEvent e) {
        IHandlerService handlerService = (IHandlerService) getSite()
                .getService(IHandlerService.class);
        try {
            handlerService.executeCommand("my command id", null);
        } catch (Exception ex) {
            throw new RuntimeException("command with id \"my command id\" not found");
        }

    }
});

答案 1 :(得分:2)

没有。您必须侦听按钮事件并以编程方式调用命令。

答案 2 :(得分:0)

您可以在视图或向导中使用CommandContributionItems,如下所示:

CommandContributionItemParameter param = new CommandContributionItemParameter(getSite(),
            "myCommand", "com.voo.myCommand", CommandContributionItem.STYLE_PUSH);
param.label = "My Label";
CommandContributionItem item = new CommandContributionItem(param);
item.fill(parent);