简体   繁体   English

如何在 eclipse 插件中添加 IResourceChangeListener?

[英]How to add IResourceChangeListener in eclipse plugin?

I am trying to add IResourceChangeListener in my eclipse plugin, using following tutorial:我正在尝试使用以下教程在我的 eclipse 插件中添加 IResourceChangeListener:

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

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?如果我将它添加到任何 java 类中,那么 eclipse 将如何知道在事件发生时触发哪个类? I tried to put the code in activator.java as following (I added it there because it maintains the plugin life cycle).我尝试将代码放在 activator.java 中,如下所示(我在那里添加它是因为它维护了插件生命周期)。

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);
    }
}

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.当我通过外部 MKS 检查更改我当前的编辑器时,它不会将“某些内容已更改”打印到 consol,或者只是它不工作。

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.我想在 Eclipse 中修改工作编辑器(可以是默认的 Java 编辑器),而无需使用此侦听器创建任何新编辑器。

Thanks a lot in advance.非常感谢。

There are 2 ways to do this.有两种方法可以做到这一点。

  1. Write your own class by implementing IResourceChangeListener.通过实现 IResourceChangeListener 编写您自己的类。 This will provide fine control.这将提供精细控制。
  2. Use below code使用下面的代码

workspace.addResourceChangeListener(listener,IResourceChangeEvent.POST_CHANGE|IResourceChangeEvent.PRE_BUILD);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM