codecitrus
codecitrus

Reputation: 709

getActiveWorkbenchWindow returns null in WorkbenchAdvisor.openWindows()

I have the following code in my WorkbenchAdvisor subclasse's openWindows() override:

public class MyWorkbenchAdvisor extends WorkbenchAdvisor {
    ...
    @Override
    public boolean openWindows() {
        super.openWindows();
        PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
            @Override
            public void run() {
                IWorkbenchWindow win = 
                    PlatformUI.getWorkbench().getActiveWorkbenchWindow();   
            }
        });
    }
}

When I debug the code, I am seeing that after the call to getActiveWorkbenchWindow(), win is null. I did notice that by using asyncExec instead of syncExec, I can get win to initialize properly. However, I do believe I need syncExec in my particular context.

As a side note, I have also tried not using asyncExec/syncExec altogether as well (a.k.a just put call to getActiveWorkbenchWindow() directly in the openWindows() method), but to no avail.

Help appreciated. Thanks.

Upvotes: 1

Views: 659

Answers (1)

flavio.donze
flavio.donze

Reputation: 8100

Is it possible that you use postStartup() instead of openWindows()? In postStartup() win will not be null.

openWindows() is called too early there is no active workbench around, the reason why you got an active window using asyncExec() is because it is executed delayed. Which would be very buggy code, since it is not guaranteed that the active workbench is ready and so win could be null at one startup and not null in another.

Upvotes: 3

Related Questions