如何在eclipse插件中添加IResourceChangeListener? [英] How to add IResourceChangeListener in eclipse plugin?
问题描述
我正在尝试使用以下教程在eclipse插件中添加IResourceChangeListener:
I am trying to add IResourceChangeListener in my eclipse plugin, using following tutorial:
https://eclipse.org/articles/Article-Resource-deltas/resource-deltas.html
但是,我从未在任何地方找到我应该在哪里添加这些侦听器代码的地方。我发现他们只是在创建一个新类,在其中添加了侦听器代码。如果我仅将其添加到任何Java类中,那么蚀将如何知道,事件发生时将触发哪个类?我试图按如下方式将代码放入activator.java中(我在其中添加了它,因为它维护了插件的生命周期)。
However, I never found anywhere, where should I add these listener code. I found that they are just creating a new class where they added the listener code. If I add it just in any java class, then how eclipse will know, which class to trigger when the events occur? I tried to put the code in activator.java as following (I added it there because it maintains the plugin life cycle).
我修改了开始和停止方法。
I modified the start and stop method.
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签出更改当前的编辑器时,它无法打印东西已更改为consol,或者根本不起作用。
But its not working. When I change my current editor by external MKS check out, its not printing "Something has changed" to consol, or simply its not working.
我如何使其正常工作?我应该在哪里实际添加代码?我想在eclipse中修改工作编辑器(可以是默认的Java编辑器),而不用此侦听器创建任何新的编辑器。
How can I make it working? Where should I add the code actually? I want to modify the working editor (can be default java editor) in eclipse without creating any new editor with this listener.
非常感谢。
Thanks a lot in advance.
推荐答案
有两种方法可以做到这一点。
There are 2 ways to do this.
- 自己编写类通过实现IResourceChangeListener来实现。
- 使用以下代码
workspace.addResourceChangeListener(listener,IResourceChangeEvent.POST_CHANGE | IResourceChangeEvent.PRE_BUILD);
这篇关于如何在eclipse插件中添加IResourceChangeListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!