Question

In my BundleActivator's start method, I need to access the active IWorkbenchWindow to add an IPartListener to it. However, when the start() method is called,

Workbench.getInstance().getActiveWorkbenchWindow()

returns null. I tried adding a IWindowListener to Workbench.getInstance(), but a window open event is never fired. Only a window activated event is fired when I switch to another program and back to eclipse.

How do I add the IPartListener properly?

Was it helpful?

Solution 2

I found a way to do it:

final IWorkbench workbench = PlatformUI.getWorkbench();
workbench.getDisplay().asyncExec(new Runnable() {
   public void run() {
     IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
     if (window != null) {
       // do something
     }
   }
 });

(from Eclipse documentation)

OTHER TIPS

Workbench is an internal class and you should not be using it (Eclipse API Rules of Engagement). Internal classes may be changed without notice (Workbench in fact was completely rewritten between Eclipse 3 or 4).

The official way to get the IWorkbench interface is:

IWorkbench workbench = PlatformUI.getWorkbench();

However this may also return null if it is called too early in the Eclipse startup.

It is not usual to add a part listener in an activator, usually this is done in a view or editor part initialization or in a command handler or action.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top