![](/img/trans.png)
[英]Eclipse plugin: Return affected class from IResourceChangeListener
[英]How to add IResourceChangeListener in eclipse plugin?
我正在嘗試使用以下教程在我的 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 檢查更改我當前的編輯器時,它不會將“某些內容已更改”打印到 consol,或者只是它不工作。
我怎樣才能讓它工作? 我應該在哪里添加代碼? 我想在 Eclipse 中修改工作編輯器(可以是默認的 Java 編輯器),而無需使用此偵聽器創建任何新編輯器。
非常感謝。
有兩種方法可以做到這一點。
workspace.addResourceChangeListener(listener,IResourceChangeEvent.POST_CHANGE|IResourceChangeEvent.PRE_BUILD);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.