我有一个多页面编辑器,其中每个页面都有复制粘贴删除操作和f3导航操作。我创建上下文并激活和停用操作
下面是plugin.xml的样子:
<extension
point="org.eclipse.ui.bindings">
<key
commandId="com.sap.adt.wda.controller.ui.navigate"
contextId="com.sap.adt.wda.controller.ui.contextTabScope"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="F3">
</key>
</extension>
<extension
point="org.eclipse.ui.contexts">
<context
description="%context_navigateToController_ymsg"
id="com.sap.adt.wda.controller.ui.contextTabScope"
name="%context_navigateToController_yins"
parentId="org.eclipse.ui.contexts.window">
</context>
</extension>
通过激活和停用上下文来完成。以下是代码段。
private void activateHandlers() {
IContextService contextService = (IContextService) (PlatformUI.getWorkbench().getService(IContextService.class));
if (contextService != null) {
activation = contextService.activateContext(IControllerConstants.CONTEXT_TAB_ECLIPSE_CONTEXT_ID);
}
IEditorSite site = getEditor().getEditorSite();
IHandlerService service = (IHandlerService) site.getService(IHandlerService.class);
IHandlerActivation navigateHandlerActivation = service.activateHandler(NavigationHandler.COMMAND_ID, new NavigationHandler());
activatedHandlers = new ArrayList<IHandlerActivation>();
activatedHandlers.add(navigateHandlerActivation);
}
public void deactivateHandlers() {
IContextService contextService = (IContextService) (PlatformUI.getWorkbench().getService(IContextService.class));
contextService.deactivateContext(activation);
IHandlerService service = (IHandlerService) getEditor().getEditorSite().getService(IHandlerService.class);
if (activatedHandlers != null) {
service.deactivateHandlers(activatedHandlers);
activatedHandlers = null;
}
}
这两种方法分别从页面更改中调用,以在页面处于活动状态时激活上下文,并在页面未处于活动状态时停用。
问题是它仍然与另一个打开的编辑器冲突,因为当我在编辑器之间切换时,甚至没有调用pagechange !!请告诉我实现此目的的最佳方法是什么。
答案 0 :(得分:1)
在编辑器的sourceViewer上添加焦点侦听器
focusListener = new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
deactivateHandlers();
}
@Override
public void focusGained(FocusEvent arg0) {
activateHandlers();
}
};
sourceViewer.getTextWidget().addFocusListener(focusListener);
在每页切换时,将调用focusLost和focusGained。 覆盖activateHandlers()和deactivateHandlers()以启用和禁用编辑器各部分的相应处理程序