Search code examples
eclipseeclipse-plugineclipse-rcp

Eclipse RCP Handler for custom editor


in my custom editor I want that the handler is only activated when it it is executed within the 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>

Here is the activation in the editor.

   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");
   }

So I think antyhing went wrong in the configuration of the plugin.xml.


Solution

  • In a text editor you don't use the context service for this. Instead you override AbstractDecoratedTextEditor#initializeKeyBindingScopes and set the key binding scope:

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

    Which ends up using IKeyBindingService