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

回眸只為那壹抹淺笑 提交于 2019-12-30 10:43:40

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/1030682/eclipse-plugin-notification-of-when-an-editor-is-opened-in-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!