Vlad Ilie
Vlad Ilie

Reputation: 1409

org.eclipse.ui.startup in use with WindowListener - first activation not caught

Greetings fellow Stackoverflowians,

I am developing an Eclipse RCP application, and I want to add a listener to the ProjectExplorer Eclipse View, and this listener needs to be added before the user does anything, but after the GUI has been generated. Right on startup, though, the PlatformUI.getWorkbench().getActiveWorkbenchWindow() returns null (d'oh, the window isn't activated) so therefore I add to the already created Workbench a WindowListener

PlatformUI.getWorkbench().addWindowListener(new IWindowListener() {
            @Override
            public void windowActivated(IWorkbenchWindow activatedWindow) {
                //do stuff here
                }
            }
            @Override
            public void windowClosed(IWorkbenchWindow arg0) {
                //remove stuff here
            }
            @Override
            public void windowDeactivated(IWorkbenchWindow arg0) {
                // stub
            }
            @Override
            public void windowOpened(IWorkbenchWindow arg0) {
                //stub
            }
        });

Now the problem that I've come across is that even though the ActiveWorkbenchWindow is populated, the windowActivated() method from the WindowListener is not called :(

Funnily enough, when I click on another window, then I click back on the application window, the windowActivated() method is called... therefore the listener was indeed registered.

Any help and suggestions are appreciated!

Upvotes: 2

Views: 882

Answers (2)

Vlad Ilie
Vlad Ilie

Reputation: 1409

I've succeeded in not using the WindowListener anymore on the Workbench, so instead of adding it in the earlyStartup() method of my IStartup implementation, I've done this:

public class StartupHook implements IStartup {
    @Override
    public void earlyStartup() { 
        IWorkbenchWindow window = PlatformUI.getWorkbench().getWorkbenchWindows()[0];
        ISelectionListener projectListener = new ProjectSelectionListener();
        window.getSelectionService().addSelectionListener(projectListener);
    }

}

The trick is that despite there being multiple windows opened on startup, only one is represented, that includes all the views, hence it is reasonable to access: PlatformUI.getWorkbench().getWorkbenchWindows()[0]

Presto, worked around the active window not being in the getActiveWindow() method of the Workbench

Upvotes: 1

greg-449
greg-449

Reputation: 111217

You could use overrides of the postWindowCreate or postWindowOpen methods of WorkbenchWindowAdvisor to set this up.

Upvotes: 3

Related Questions