Responding to resource changes in the Eclipse workspace

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值