Responding to resource changes in the Eclipse workspace

原创 2004年08月10日 12:49:00

Many tools and user interface elements are interested in processing resource changes as they happen. For example, the task list wants to update new or changed markers, the navigator wants to reflect added and deleted resources, and the Java compiler wants to recompile modified Java files. Such notifications are potentially costly to compute, manage and broadcast. The Eclipse Platform resource model includes a series of mechanisms for efficiently notifying clients of resource changes. This article outlines these facilities and gives some examples of their use.

Resource change listeners

A good listener is not only popular everywhere,
but after a while, he knows something.

– Wilson Mizner

The primary method for Eclipse plug-ins to be notified of changes to resources is by installing a resource change listener. These listeners are given after-the-fact notification of what projects, folders and files changed during the last resource changing operation. This provides a powerful mechanism for plug-ins to keep their domain state synchronized with the state of the underlying workspace. Since listeners are told exactly what resources changed (and how they changed), they can update their model incrementally, which ensures that the time taken by the update is proportional to the size of the change, not the size of the workspace.

Listeners must implement the IResourceChangeListener interface, and are registered using the method addResourceChangeListener on IWorkspace. It is also important to remove your resource change listener when it is no longer needed, using IWorkspace.removeResourceChangeListener.

During a resource change notification, the workspace is locked to prevent further modification while the notifications are happening. This is necessary to ensure that all listeners are notified of all workspace changes. Otherwise, a change made by one listener would have to be broadcast to all other listeners, easily creating the possibility of an infinite loop. There is a special exception to this rule for the PRE_AUTO_BUILD and POST_AUTO_BUILD event types that will be discussed later on.

Before we get into the details, let's start with a simple example that shows how to add and remove a resource change listener:

   IWorkspace workspace = ResourcesPlugin.getWorkspace();
   IResourceChangeListener listener = new IResourceChangeListener() {
      public void resourceChanged(IResourceChangeEvent event) {
         System.out.println("Something changed!");
      }
   };
   workspace.addResourceChangeListener(listener);

   //... some time later one ...
   workspace.removeResourceChangeListener(listener);

Below is an example of an operation that is nested using the IWorkspaceRunnable mechanism. In this case, a single resource change event will be broadcast, indicating that one project and ten files have been created. To keep it simple, progress monitoring and exception handling have been omitted from this example.

   IWorkspace workspace = ResourcesPlugin.getWorkspace();
   final IProject project = workspace.getRoot().getProject("My Project");
   IWorkspaceRunnable operation = new IWorkspaceRunnable() {
      public void run(IProgressMonitor monitor) throws CoreException {
         int fileCount = 10;
         project.create(null);
         project.open(null);
         for (int i = 0; i < fileCount; i++) {
            IFile file = project.getFile("File" + i);
            file.create(null, IResource.NONE, null);
         }
      }
   };
   workspace.run(operation, null);
details please visit http://www.eclipse.org/articles/Article-Resource-deltas/resource-deltas.html

github在eclipse上的插件egit官方使用指南

转自:http://wiki.eclipse.org/EGit/User_Guide EGit/User Guide EGit Contents  [h...
  • shanliangliuxing
  • shanliangliuxing
  • 2013年02月04日 16:10
  • 20325

Appium源码分析(4)-控制器模块

上一篇分析appium的文章还要追述到一个月前了,感慨已经荒废了很久了。今天继续开始吧。 控制器模块// Appium webserver controller methods // https://...
  • qhshiniba
  • qhshiniba
  • 2015年04月29日 12:26
  • 2333

eclipse非正常关闭,重新无法启动

2、解决方法 删除文件 [workspace]/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi...
  • ouyewang
  • ouyewang
  • 2014年10月29日 16:19
  • 2201

QML官方系列教程——Use Case - Responding To User Input in QML

附网址:http://qt-project.org/doc/qt-5/qtquick-usecase-userinput.html Supported Types of User Input ——...
  • u011348999
  • u011348999
  • 2014年06月10日 13:32
  • 2843

Changes to the Meta-Object System in Qt 5

There are changes to the meta object-system in Qt 5, both under the hood and on the API side. Some o...
  • deyili
  • deyili
  • 2012年07月19日 10:32
  • 1106

解决Tomcat部署Maven异常:Deployment is out of date due to changes in the underlying project contents

Deployment is out of date due to changes in the underlying project contents,完美解决,本文章基于Maven处理,非Maven...
  • nthack5730
  • nthack5730
  • 2016年04月16日 09:44
  • 26151

Error message when you try to save a table in SQL Server 2008: "Saving changes is not permitted"

SYMPTOMS When you use Data Definition Language (DDL) to modify a table, and then you try to save th...
  • tswisdom
  • tswisdom
  • 2011年11月29日 23:11
  • 1150

nable to load configuration. - bean - jar:file:/F:/workspace/.metadata/.plugins/com.genuitec.eclipse

Unable to load configuration. - bean - jar:file:/F:/workspace/.metadata/.plugins/com.genuitec.eclips...
  • leidijava
  • leidijava
  • 2012年10月19日 16:57
  • 7372

How to insert a resource image, as embedded image in email?

最近项目需要,要在某种情况下,给用户发送邮件,以前的做法就是把邮件内容做成模板,在使用的时候,代码里面组装成HTML格式的邮件,只能是纯文本的。现在需要给邮件加背景图片以及一些图片,找寻了很多,发现很...
  • hustypf
  • hustypf
  • 2013年06月19日 16:05
  • 2459

an auction approach to resource allocation in uplink OFDMA systems

  • 2013年12月18日 21:48
  • 567KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Responding to resource changes in the Eclipse workspace
举报原因:
原因补充:

(最多只允许输入30个字)