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

后端 未结 1 1814
温柔的废话
温柔的废话 2021-01-14 03:14

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

1条回答
  •  隐瞒了意图╮
    2021-01-14 04:03

    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().

    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

    0 讨论(0)
提交回复
热议问题