简体   繁体   中英

Eclipse Plugin - Notification of when an editor is opened in Eclipse

I want to get notified when an editor is opened in Eclipse. What is the best way to do that?

From this thread

Have your class implement org.eclipse.ui.IPartListener2 .
Then you get notified when a workbench part (an IEditorPart , etc.) just got opened/closed. You can actually filter out which parts you want to pay attention to.

(note: As of 3.5, the IPartListener2 can also implement IPageChangedListener to be notified about any parts that implement IPageChangeProvider and post PageChangedEvents .)

The tricky part (no pun intended) is to register the listener to workbench.

So, the first thing to do is get a valid IWorkbenchPage so that you can call IWorkbenchPage.addPartListener(<your class that implements IPartListener>) .

Here is how to get a workbench page.

IWorkbenchPage page = null;
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window != null)
{
    page = window.getActivePage();
}

if (page == null)
{
    // Look for a window and get the page off it!
    IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
    for (int i = 0; i < windows.length; i++)
    {
        if (windows[i] != null)
        {
            window = windows[i];
            page = windows[i].getActivePage();
            if (page != null)
            break;
        }
    }
}

See also here .


See this class as an example

IPartListener2 partlistener = new IPartListener2(){
        public void partActivated( IWorkbenchPartReference partRef ) {
            if (partRef.getPart(false) == MapEditor.this){
                registerFeatureFlasher();
                ApplicationGIS.getToolManager().setCurrentEditor(editor);
            }
        }
 [...]

Or this generic PartListener for generic usage of a PartListener2.

Or this EditorTracker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM