在您的 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();
}