package com.anjlab.eclipse.tapestry5.watchdog;

import org.eclipse.ui.IPageListener;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

public class WindowSelectionListener implements IWindowListener
{
    private ISelectionListener selectionListener;
    
    public WindowSelectionListener(ISelectionListener selectionListener)
    {
        this.selectionListener = selectionListener;
    }
    
    private IPageListener pageListener = new IPageListener()
    {
        @Override
        public void pageOpened(IWorkbenchPage page)
        {
            //  Prevent double-add
            page.removeSelectionListener(selectionListener);
            
            page.addSelectionListener(selectionListener);
        }
        
        @Override
        public void pageClosed(IWorkbenchPage page)
        {
            page.removeSelectionListener(selectionListener);
        }
        
        @Override
        public void pageActivated(IWorkbenchPage page)
        {
        }
    };

    @Override
    public void windowOpened(IWorkbenchWindow window)
    {
        //  Prevent double-add
        window.removePageListener(pageListener);
        
        window.addPageListener(pageListener);
        
        //  windowOpened might be fired manually and the page was already open at this moment,
        //  so trigger pageOpened event manually now to setup handlers
        pageListener.pageOpened(window.getActivePage());
    }

    @Override
    public void windowDeactivated(IWorkbenchWindow window)
    {
    }

    @Override
    public void windowClosed(IWorkbenchWindow window)
    {
        window.removePageListener(pageListener);
    }

    @Override
    public void windowActivated(IWorkbenchWindow window)
    {
    }
    
    protected void dispose()
    {
        IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
        for (IWorkbenchWindow window : windows)
        {
            IWorkbenchPage[] pages = window.getPages();
            for (IWorkbenchPage page : pages)
            {
                pageListener.pageClosed(page);
            }
            windowClosed(window);
        }
    }

    public WindowSelectionListener addListener()
    {
        //  Prevent double-add
        PlatformUI.getWorkbench().removeWindowListener(this);
        
        PlatformUI.getWorkbench().addWindowListener(this);
        
        //  This plug-in activated after the window opened, so should notify manually here
        windowOpened(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
        
        return this;
    }

    public void removeListener()
    {
        dispose();
        
        PlatformUI.getWorkbench().removeWindowListener(this);
    }
}
@@ -2505,32 +2505,183 @@
 this);%0A        %0A
+        IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();%0A        %0A        if (activeWindow != null)%0A        %7B%0A    
         //  This
@@ -2759,24 +2759,28 @@
 ere%0A        
+    
 windowOpened
@@ -2784,62 +2784,32 @@
 ned(
-PlatformUI.getWorkbench().getActiveWorkbenchWindow());
+activeWindow);%0A        %7D
 %0A   

Fix NPE in WindowSelectionListener #11