简体   繁体   中英

Eclipse JFace :: How to get rid of key binding conflicts while working in a multipage editor

I have an editor with multipage in which each page has copy paste delete actions and f3 navigation action. I create context and activate and deactivate the actions

Below is how the plugin.xml looks like :

<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>

It is done through activation and deactivation of context. Below is the code snippet.

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

These two methods are called respectively from the page change to activate the context once the page is active and deactivate when the page is not active.

The problem is that it still conflicts with another opened editor as when I switch between editors, pagechange is not even called!! Please tell me what is the best way to implement this.

Add focus listener on sourceViewer of editor

    focusListener = new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            deactivateHandlers();
        }

        @Override
        public void focusGained(FocusEvent arg0) {
            activateHandlers();
        }
    };

   sourceViewer.getTextWidget().addFocusListener(focusListener);

On switch of every page the focusLost and focusGained will be called. Override activateHandlers() and deactivateHandlers() to enable and disable respective handlers for each parts of editor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM