Eclipse插件 - 在Eclipse中打开编辑器时的通知

时间:2022-09-11 19:47:50

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

我希望在Eclipse中打开编辑器时收到通知。最好的方法是什么?

1 个解决方案

#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.

让你的类实现org.eclipse.ui.IPartListener2。然后,当工作台部件(IEditorPart等)刚刚打开/关闭时,您会收到通知。您实际上可以过滤掉您想要注意的部分。

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

(注意:从3.5开始,IPartListener2还可以实现IPageChangedListener,以通知有关实现IPageChangeProvider和发布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>).

因此,首先要做的是获取有效的IWorkbenchPage,以便可以调用IWorkbenchPage.addPartListener( <实现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.

或者这个通用的PartListener用于PartListener2的通用用法。

Or this EditorTracker

或者这个EditorTracker

#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.

让你的类实现org.eclipse.ui.IPartListener2。然后,当工作台部件(IEditorPart等)刚刚打开/关闭时,您会收到通知。您实际上可以过滤掉您想要注意的部分。

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

(注意:从3.5开始,IPartListener2还可以实现IPageChangedListener,以通知有关实现IPageChangeProvider和发布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>).

因此,首先要做的是获取有效的IWorkbenchPage,以便可以调用IWorkbenchPage.addPartListener( <实现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.

或者这个通用的PartListener用于PartListener2的通用用法。

Or this EditorTracker

或者这个EditorTracker