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 Listener
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 (iResourceChangeeevent 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.Getries (). getProject ("My Project");
IWorkspacerunnable Operation = new iWorkspaceerunnable () {
Public void Run (iProgressMonitor Monitor) throws coreException {
INT filecount = 10;
Project.create (NULL);
Project.open (NULL);
For (int i = 0; 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