Fabio C.
Fabio C.

Reputation: 1300

How to activate my eclipse plugin when a specific editors opens

My plugin needs to listen to changes (selection and content) of xtext based editors provided by another third-party plugin.

Edit1

The issue is not how to listen to the specific events in general. Instead the issue is how to trigger the listener registration, since there's no code of my plugin executed (lazy loading) unless it is used by the user through a command for instance.

Edit2

Using org.eclipse.ui.IStartup extension point the issue is that in IStartup.earlyStartup() PlatformUI.getWorkbench().getActiveWorkbenchWindow(); is returning null. It looks like this is too early in the startup phase to register the listeners.

Upvotes: 1

Views: 264

Answers (1)

greg-449
greg-449

Reputation: 111217

You can use an org.eclipse.ui.IPartListener2 listener to be told about all parts opening, closing, being activated ....

IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

IPartService partService = window.getPartService();

partService.addPartListener(your listener);

The

public void partOpened(IWorkbenchPartReference partRef)

method of the listener will be called when a part (editor or view) is opened. The partRef.getId() method will give you the id of the part.

Use the org.eclipse.ui.startup extension point to declare that your plugin needs to be started early. This lets you declare a class implementing org.eclipse.ui.IStartup that is called during Eclipse startup.

Note that the startup runs quite early so not everything is set up. Use Display.asyncExec to schedule code to run later:

Display.getDefault().asyncExec(runnable);

Upvotes: 1

Related Questions