Como adicionar IResourceChangeListener no plugin eclipse?
Estou tentando adicionar IResourceChangeListener no meu plug-in eclipse, usando o seguinte tutorial:
https://eclipse.org/articles/Article-Resource-deltas/resource-deltas.html
No entanto, nunca encontrei nenhum lugar, onde devo adicionar esses códigos de ouvinte. Descobri que eles estão apenas criando uma nova classe onde adicionaram o código do ouvinte. Se eu incluí-lo em qualquer classe java, como o eclipse saberá qual classe acionar quando os eventos ocorrerem? Tentei colocar o código em activator.java da seguinte forma (eu o adicionei porque mantém o ciclo de vida do plug-in).
Eu modifiquei o método start e stop.
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);
}
}
Mas não está funcionando. Quando altero meu editor atual pelo check-out externo do MKS, ele não está imprimindo "Algo mudou" para consolar ou simplesmente não está funcionando.
Como posso fazê-lo funcionar? Onde devo adicionar o código realmente? Quero modificar o editor de trabalho (pode ser o editor java padrão) no eclipse sem criar nenhum novo editor com este ouvinte.
Muito obrigado antecipadamente.