如何在eclipse插件中添加IResourceChangeListener?

我正在尝试使用以下教程在我的eclipse插件中添加IResourceChangeListener:

https://eclipse.org/articles/Article-Resource-deltas/resource-deltas.html

但是,我从来没有找到任何地方,我应该在哪里添加这些监听器代码。 我发现他们只是创建了一个新类,他们添加了监听器代码。 如果我只是在任何java类中添加它,那么eclipse将如何知道,当事件发生时触发哪个类? 我试着把代码放在activator.java中,如下所示(我在那里添加了它,因为它维护了插件的生命周期)。

我修改了启动和停止方法。

package testPlugin; import org.eclipse.core.resources.IResourceChangeEvent; import org.eclipse.core.resources.IResourceChangeListener; import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; /** * The activator class controls the plug-in life cycle */ public class Activator extends AbstractUIPlugin { // The plug-in ID public static final String PLUGIN_ID = "testPlugin"; //$NON-NLS-1$ /** the resource listener on URI changes */ IWorkspace workspace = ResourcesPlugin.getWorkspace(); IResourceChangeListener listener; // The shared instance private static Activator plugin; /** * The constructor * */ public Activator() { } /* * (non-Javadoc) * * @see * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext * ) */ public void start(BundleContext context) throws Exception { super.start(context); plugin = this; listener = new IResourceChangeListener() { public void resourceChanged(IResourceChangeEvent event) { System.out.println("Something changed!"); } }; workspace.addResourceChangeListener(listener); } /* * (non-Javadoc) * * @see * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext * ) */ public void stop(BundleContext context) throws Exception { if (workspace != null) { workspace.removeResourceChangeListener(listener); } plugin = null; super.stop(context); } /** * Returns the shared instance * * @return the shared instance */ public static Activator getDefault() { return plugin; } /** * Returns an image descriptor for the image file at the given plug-in * relative path * * @param path * the path * @return the image descriptor */ public static ImageDescriptor getImageDescriptor(String path) { return imageDescriptorFromPlugin(PLUGIN_ID, path); } } 

但它不起作用。 当我通过外部MKS检查更改我当前的编辑器时,它不打印“Something has changed”到consol,或者只是它不起作用。

我怎样才能使它工作? 我应该在哪里添加代码呢? 我想在eclipse中修改工作编辑器(可以是默认的java编辑器)而不用这个监听器创建任何新的编辑器。

非常感谢提前。