如何在 Eclipse 插件中添加 IResourceChangeListener? [英] How to add IResourceChangeListener in eclipse plugin?

查看:19
本文介绍了如何在 Eclipse 插件中添加 IResourceChangeListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

package testPlugin;导入 org.eclipse.core.resources.IResourceChangeEvent;导入 org.eclipse.core.resources.IResourceChangeListener;导入 org.eclipse.core.resources.IWorkspace;导入 org.eclipse.core.resources.ResourcesPlugin;导入 org.eclipse.jface.resource.ImageDescriptor;导入 org.eclipse.ui.plugin.AbstractUIPlugin;导入 org.osgi.framework.BundleContext;/*** 激活器类控制插件生命周期*/公共类激活器扩展 AbstractUIPlugin {//插件IDpublic static final String PLUGIN_ID = "testPlugin";//$非NLS-1$/** URI 上的资源监听器改变 */IWorkspace 工作区 = ResourcesPlugin.getWorkspace();IResourceChangeListener 监听器;//共享实例私有静态激活器插件;/*** 构造函数**/公共激活器(){}/**(非 Javadoc)** @看* org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext* )*/公共无效开始(BundleContext上下文)抛出异常{超级开始(上下文);插件=这个;监听器 = 新的 IResourceChangeListener() {公共无效资源更改(IResourceChangeEvent 事件){System.out.println("有些东西改变了!");}};workspace.addResourceChangeListener(listener);}/**(非 Javadoc)** @看* org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext* )*/公共无效停止(BundleContext上下文)抛出异常{如果(工作区!= null){workspace.removeResourceChangeListener(listener);}插件=空;超级停止(上下文);}/*** 返回共享实例** @return 共享实例*/公共静态激活器 getDefault() {返回插件;}/*** 返回给定插件的图像文件的图像描述符* 相对路径** @param 路径*            路径* @return 图像描述符*/公共静态ImageDescriptor getImageDescriptor(字符串路径){return imageDescriptorFromPlugin(PLUGIN_ID, path);}}

但它不起作用.当我通过外部 MKS 签出更改我当前的编辑器时,它没有将某些内容已更改"打印到 consol,或者根本无法正常工作.

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

非常感谢.

解决方案

有两种方法可以做到这一点.

  1. 通过实现 IResourceChangeListener 编写您自己的类.这将提供良好的控制.
  2. 使用下面的代码

<块引用>

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

I am trying to add IResourceChangeListener in my eclipse plugin, using following tutorial:

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

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.

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.

  1. Write your own class by implementing IResourceChangeListener. This will provide fine control.
  2. Use below code

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

这篇关于如何在 Eclipse 插件中添加 IResourceChangeListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆