Cegy
Cegy

Reputation: 89

Eclipse plugin - add mouse listener to an editor everytime its opened

I am developing an Eclipse plugin and I need to store the click location in X and Y axis coordinates.

So far, I am doing this by using the org.eclipse.ui.IStartup interface and using this in the class implementing it:

@Override
public void earlyStartup() {
    IWorkbench wb = PlatformUI.getWorkbench();
    wb.addWindowListener(generateWindowListener());
}

And the in the given method I do this:

private IWindowListener generateWindowListener() 
{
    return new IWindowListener() {

        private MouseListener clickMouseListener = new ClickMouseListener();

        @Override
        public void windowOpened(IWorkbenchWindow window) {
            //Nothing
        }

        @Override
        public void windowDeactivated(IWorkbenchWindow window) {
            System.out.println("deactivaed");
            IWorkbenchPage activePage = window.getActivePage();
            activePage.getActiveEditor().getAdapter(org.eclipse.swt.widgets.Control.class).addMouseListener(clickMouseListener);
        }

        @Override
        public void windowClosed(IWorkbenchWindow window) {
            //Nothing
        }

        @Override
        public void windowActivated(IWorkbenchWindow window) {
            System.out.println("activated");
            IWorkbenchPage activePage = window.getActivePage();
            activePage.getActiveEditor().getAdapter(org.eclipse.swt.widgets.Control.class).addMouseListener(clickMouseListener);
        }
    };
}

So I basically register a listener to the general workbench with the intent to register a new MouseListener - where I save the coordinates - on newly opened editors.

This, however, works only if the entire eclipse window is minimized/maximized, as this listener is bound to the main window. If a new editor is opened, nothing happens (as it probably should work).

I would like to register new MouseListeners everytime a new editor is opened/activated/clicked on - so I can register them to the editor right when a new editor is opened. How can I subscribe/listen to any events related to editors (mostly activated/open new editor) so I can then subscribe a MouseListener to the editor and get current X and Y axis of the mouse-clicks?

Thank for any suggestions.

Upvotes: 1

Views: 358

Answers (1)

greg-449
greg-449

Reputation: 111142

Use an IPartListener to listen to part events in a workbench window:

IPartService partService = window.getPartService();

partService.addPartListener(listener);

This will tell you about all editors (and views) being opened, closed, activiated, ....

In the early startup method the workbench window is not yet available. Use Display.asyncExec to delay running your code until the UI initialization is complete:

Display.getDefault().asyncExec(new Runnable() {
   @Override
   public void run() {
     IPartService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService();

     ......
   }
 });

Upvotes: 2

Related Questions