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

Ant*_*nÓg 3 eclipse swt command

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

Ale*_*x_M 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");
        }

    }
});
Run Code Online (Sandbox Code Playgroud)