java - eclipse rcp 4 对话框添加绑定(bind)上下文

标签 java eclipse binding key rcp

如何将绑定(bind)上下文添加到 Eclipse RCP4 中的对话框。

我定义了多个绑定(bind)上下文来监听 FKey。部件对按下的键使用react,但我不知道如何向对话框添加绑定(bind)。

我用

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

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

最佳答案

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

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

在对话框中注入(inject) MApplication:

@Inject
private MApplication _app;

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

添加此方法以获得对话框的正确 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”是绑定(bind)上下文 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();
}

关于java - eclipse rcp 4 对话框添加绑定(bind)上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25869363/

相关文章:

xaml - 有没有比在 XAML 中使用 ObservableCollection 进行快速绑定(bind)更好的方法?

java - Android自定义数字时钟,while循环运行出错 "Unhandled exception type InterruptedException"

java - 在 Java 中的 JLabel 上使用 JScrollPane

java - 如何使用eclipse在java中的类中添加默认字符串

java - 在 Eclipse 中调试时查看完整的字符串

wpf - 为什么选项卡控件在更改选项卡时重用 View 实例

银光装订

java - 使用 Criteria API 通过 Hibernate 选择没有父级的记录

java - Android Firestore 将文档引用数组转换为 List<Pojo>

java - 如何通过将鼠标悬停在变量上来显示变量值