如何将绑定(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/