【问题标题】:eclipse rcp 4 Dialog add Binding Contexteclipse rcp 4 对话框添加绑定上下文
【发布时间】:2014-11-10 05:42:39
【问题描述】:

如何在 Eclipse RCP4 中向对话框添加绑定上下文。

我已经定义了多个绑定上下文来监听 FKey。部件对按下的键做出反应,但我不知道如何将绑定添加到对话框。

我用

创建我的对话框
ImportDialog dialog = ContextInjectionFactory.make(ImportDialog.class, context);
dialog.open();

因此我可以访问每个服务,如何使它将关键事件发送到绑定上下文?

【问题讨论】:

    标签: java eclipse binding key rcp


    【解决方案1】:

    在您的 application.e4xmi 中为您的对话框定义一个“绑定上下文”,使其成为“对话框中”绑定上下文的子项。

    添加一个新的“BindingTable”,并将上下文 ID 设置为新的绑定上下文。将要在对话框中使用的键绑定添加到此表中。

    在您的对话框中注入 MApplication:

    @Inject
    private MApplication _app;
    

    不要尝试注入其他服务,因为您将无法获得正确的服务实例。

    添加此方法以获得对话框的正确 Eclipse 上下文:

    IEclipseContext getEclipseContext()
    {
      // Must use active leaf from the Application context to get the correct context for key bindings and contexts in dialogs
    
      return _app.getContext().getActiveLeaf();
    }
    

    覆盖对话框getShellListener 以返回一个shell 侦听器,因为我们需要在设置上下文之前等待shell 激活:

    @Override
    protected ShellListener getShellListener()
    {
      return new ActivateShellListener();
    }
    
    private final class ActivateShellListener extends ShellAdapter
    {
      @Override
      public void shellActivated(final ShellEvent e)
      {
        doShellActivated();
      }
    }
    
    void doShellActivated()
    {
      IEclipseContext context = getEclipseContext();
    
      EContextService contextService = context.get(EContextService.class);
    
      contextService.activateContext(ID_CONTEXT);
    
      EHandlerService handlerService = context.get(EHandlerService.class);
    
      handlerService.activateHandler(ID_COMMAND, handler);
    }
    

    其中“ID_CONTEXT”是绑定上下文 ID,“ID_COMMAND”是您要为其激活处理程序的命令。

    覆盖close 进行清理:

    @Override
    public boolean close()
    {
      IEclipseContext context = getEclipseContext();
    
      EHandlerService handlerService = context.get(EHandlerService.class);
    
      handlerService.deactivateHandler(ID_COMMAND, handler);
    
      EContextService contextService = context.get(EContextService.class);
    
      contextService.deactivateContext(ID_CONTEXT);
    
      return super.close();
    }
    

    【讨论】:

    • 谢谢。我让它工作了。没有这两行 // EHandlerService handlerService = context.get(EHandlerService.class); handlerService.activateHandler(ID_COMMAND, handler);我不知道从哪里得到“处理程序”对象,我似乎不需要它?!?您能解释一下从哪里获取该对象以及为什么没有它可以工作吗?
    • 这取决于您如何处理击键。我从中获取的代码有一个未在应用程序模型中定义的处理程序,因此需要手动激活/停用。
    • 好的,我明白了。我的处理程序也在其他地方定义和使用。再次感谢 :)
    猜你喜欢
    • 2017-12-12
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    相关资源
    最近更新 更多