自定义编辑器的 Eclipse RCP 处理程序

Eclipse RCP Handler for custom editor

在我的自定义编辑器中,我希望处理程序仅在编辑器中执行时才被激活。

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.ui.editors">
      <editor
            id="testingpluginproject.editors.XMLEditor"
            name="Sample XML Editor"
            icon="icons/sample.png"
            extensions="xxml"
            class="testingpluginproject.editors.XMLEditor"
            contributorClass="org.eclipse.ui.texteditor.BasicTextEditorActionContributor">
      </editor>
   </extension>
   
   <extension
      point="org.eclipse.ui.contexts">
    <context
         id="com.my.ui.definition.activatedEditorContext"
         name="Editor Context"
         parentId="org.eclipse.ui.textEditorScope">
    </context>
   </extension>
 
  <extension point="org.eclipse.ui.commands">
    <command id="com.my.handler" name="Hello"/>
 </extension>
    
 <extension point="org.eclipse.ui.handlers">
    <handler commandId="com.my.handler" class="com.my.handler.HelloHandler"/>
 </extension>
   
 <extension
       point="org.eclipse.ui.bindings">
    <key
          commandId="com.my.handler"
          schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
          contextId="com.my.ui.definition.activatedEditorContext"
          sequence="Ctrl+7">
    </key>
 </extension>
</plugin>

这里是在编辑器中激活

   public XMLEditor() {
      colorManager = new ColorManager();
      setSourceViewerConfiguration(new XMLConfiguration(colorManager));
      setDocumentProvider(new XMLDocumentProvider());
      IContextService contextService = (IContextService) PlatformUI
               .getWorkbench().getService(IContextService.class);
      contextService.activateContext("com.my.ui.definition.activatedEditorContext");
   }

所以我认为 antyhing 在 plugin.xml 的配置中出错了。

在文本编辑器中,您不会为此使用上下文服务。相反,您覆盖 AbstractDecoratedTextEditor#initializeKeyBindingScopes 并设置键绑定范围:

/*
 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#initializeKeyBindingScopes()
 */
@Override
protected void initializeKeyBindingScopes() {
    setKeyBindingScopes(new String[] { "com.my.ui.definition.activatedEditorContext" });  
}

最终使用 IKeyBindingService