在E4中使用代码调用command,hanlder

10 篇文章 0 订阅

在Eclipse RCP开发中,并不是所有时候都可以使用配置文件完成我们想要的工作;有些时候我们想要复用Eclipse rcp程序中已经定义好的Command和handler,这样可以保持实现一致同时也省去很多的代码工作,维护起来也很方便了。下面的代码提供一个思路:

private void showXXXView() {
        if (PlatformUI.isWorkbenchRunning()) {
            IWorkbenchWindow window = PlatformUI.getWorkbench()
                    .getActiveWorkbenchWindow();
            if (window == null)
                return;

            ECommandService commandService = window.getService(ECommandService.class);
            EHandlerService handlerService = window.getService(EHandlerService.class);
            if (commandService == null || handlerService == null)
                return;

            Command command = commandService.getCommand(SHOW_GANTT_COMMAND);
            Map<String, Object> paramters = new HashMap<String, Object>();
            paramters.put(COMMAND_NAME, COMMAND_VALUE);
            if (!command.isDefined())
                return;

            ParameterizedCommand myCommand = commandService
                    .createCommand(SHOW_GANTT_COMMAND, paramters);
            handlerService.activateHandler(SHOW_GANTT_COMMAND,
                    new DialogPartHandler());
            if (!handlerService.canExecute(myCommand))
                return;

            handlerService.executeHandler(myCommand);
        }
    }

ECommandService 和EHandlerService 属于Eclipse4中的,这两个对象在E4的Part中是可以通过@Inject进行注入的,若是在E3的View中使用的话,我们就可以使用上面的这样的方式,一般这种写法在E3和E4混合的RCP程序中常见;

  • 通过ECommandService获取Command;
  • 传递Command参数;
  • 创建Command;
  • 激活handler;
  • 通过HandlerSerice执行刚才激活的handler;

下面是非E4的方式:

 final IHandlerService handlers = (IHandlerService) page.getWorkbenchWindow().getService(IHandlerService.class);
        final ICommandService commands = (ICommandService) page.getWorkbenchWindow().getService(ICommandService.class);
        if (handlers == null || commands == null)
            return;
        final Command command = commands.getCommand("org.xmind.ui.command.deleteResource"); 
        if (command == null || !command.isDefined())
            return;
        ParameterizedCommand pc = new ParameterizedCommand(command, null);
        handlers.executeCommand(pc, null);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值