Harper
Harper

Reputation: 1984

Eclipse Editor: automatically switch context when parts get active/inactive

I am developing my own eclipse editor and need to switch between different contexts for key binding. Currently I am doing the context activation/deactivation manually upon part activation.

This page https://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fworkbench_advext_contexts.htm says:

If you are activating a more specific Context within your part (either View or Editor) you can use the part site service locator to active your Context. The part's IContextService will take care of activating and deactivating the Context as your part is activated or deactivated. It will also dispose the Context when the part is disposed.

It seems like that is just what I want. But the page did not say how. Can anyone give me a hint what a 'part site service locator' mentioned in the text is and how to use it?

Upvotes: 1

Views: 304

Answers (2)

Harper
Harper

Reputation: 1984

After digging through Eclipse code, here's my answer to my own question.

First thing first, it is JUST enough to invoke

IContextService contextService = part.getSite().getService( IContextService.class );
contextService.activateContext( "your.editor.context.id" );

anywhere after init(where you get PartSite), just as @Rüdiger Herrmann mentioned in his answer.

AND(here's my finding) NOTHING ELSE need to be done. Eclipse will automatically activate/deactivate the context when part is activated /deactivated, just as described in the text I refer to. In addition, when the part site is disposed, all context will be disposed.

If you are interested in how, here's more digging.

Activate/Deactivate

When we invoke getSite().getService(IContextService.class), what we get is an instance of SlaveContextService. When we call activateContext(String contextId) on it, our request will be automatically translate to a request with a default expression ActivePartExpression.

From it's name we can easily guess that this expression will check whether a part is active and do some changes. The changes it does can be seen at ContextService.UpdateExpression.changed. Here's the code(ContextService:124-128)

if (result != EvaluationResult.FALSE) {
    runExternalCode(() -> contextService.activateContext(contextId));
} else if (cached != null) {
    runExternalCode(() -> contextService.deactivateContext(contextId));
}

Whenever Eclipse context changes(activate/deactivate part will trigger context change), UpdateExpression.changed will be invoked and check whether the target part is still active, then activate/deactivate the context accordingly.

Dispose

In SlaveContextService.dispose, all context registered through it will be disposed upon the service's dispose.

Upvotes: 1

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 21005

I would interpret the text so that you should use the service locator of the site that corresponds to your (editor) part. In the following example, part references your editor. By obtaining the context service from the part's site, you get a child context service for that particular part in wich you can activate a specialized editor context.

IContextService contextService = part.getSite().getService( IContextService.class );
contextService.activateContext( "your.editor.context.id" );

Upvotes: 1

Related Questions