9.1. IResourceChangeListener
Eclipse uses the interface org.eclipse.core.resources.IResourceChangeListener to notify registered listeners when a resource has changed. The FavoritesManager (see Section 7.2.3, View model, on page 265) needs to keep its list of Favorites items synchronized with Eclipse. This is done by implementing the org.eclipse.core.resources.IResourceChangeListener interface and registering for resource change events.
In addition, the FavoritesPlugin stop() method must be modified to call the new FavoritesManager shutdown() method so that the manager is no longer notified of resource changes once the plug-in has been shut down. Now, whenever a resource change occurs, Eclipse will call the resourceChanged() method.
public class FavoritesManager
implements IResourceChangeListener
{
private FavoritesManager() {
ResourcesPlugin.getWorkspace().addResourceChangeListener (
this, IResourceChangeEvent.POST_CHANGE);
}
public static void shutdown() {
if (manager != null) {
ResourcesPlugin.getWorkspace()
.removeResourceChangeListener(manager);
manager.saveFavorites();
manager = null;
}
}
public void resourceChanged(IResourceChangeEvent e) {
// Process events here.
}
... existing code from Section 7.2.3, View model, on page 265 ...
}
9.1.1. IResourceChangeEvent
FavoritesManager is only interested in changes that have already occurred and therefore uses the IResourceChangeEvent.POST_CHANGE constant when subscribing to change events. Several IResourceChangeEvent constants that can be used in combination to specify when an interested object should be notified of resource changes are provided by Eclipse. Below is the list of valid constants as they appear in the IResourceChangeEvent Javadoc.
PRE_BUILD Before-the-fact report of builder activity (see Section 14.1, Builders, on page 499). PRE_CLOSE Before-the-fact report of the impending closure of a single project as returned by getresource(). PRE_DELETE Before-the-fact report of the impending deletion of a single project as returned by geTResource(). POST_BUILD After-the-fact report of builder activity (see Section 14.1, Builders, on page 499). POST_CHANGE After-the-fact report of creations, deletions, and modifications to one or more resources expressed as a hierarchical resource delta as returned by getdelta().
The IResourceChangeEvent interface also defines several methods that can be used to query its state.
findMarkerDeltas(String, boolean)Returns all marker deltas of the specified type that are associated with resource deltas for this event. Pass true as the second parameter if you want to include subtypes of the specified type.
getBuildKind() Returns the kind of build that caused the event. geTDelta() Returns a resource delta, rooted at the workspace, describing the set of changes that happened to resources in the workspace. getresource() Returns the resource in question. getSource() Returns an object identifying the source of this event. getType() Returns the type of event being reported.
9.1.2. IResourceDelta
Each individual change is encoded as an instance of a resource delta that is represented by the IResourceDelta interface. Eclipse provides several different constants that can be used in combination to identify the resource deltas handled by the system. Below is the list of valid constants as they appear in the IResourceDelta Javadoc.
ADDDED Delta kind constant indicating that the resource has been added to its parent. ADDED_PHANTOM Delta kind constant indicating that a phantom resource has been added at the location of the delta node. ALL_WITH_PHANTOMS The bit mask that describes all possible delta kinds, including those involving phantoms. CHANGED Delta kind constant indicating that the resource has been changed. CONTENT Change constant indicating that the content of the resource has changed. DESCRIPTION Change constant indicating that a project's description has changed. ENCODING Change constant indicating that the encoding of the resource has changed. MARKERS Change constant indicating that the resource's markers have changed. MOVED_FROM Change constant indicating that the resource was moved from another location. MOVED_TO Change constant indicating that the resource was moved to another location. NO_CHANGE Delta kind constant indicating that the resource has not been changed in any way. OPEN Change constant indicating that the resource was opened or closed. REMOVED Delta kind constant indicating that the resource has been removed from its parent. REMOVED_PHANTOM Delta kind constant indicating that a phantom resource has been removed from the location of the delta node. REPLACED Change constant indicating that the resource has been replaced by another at the same location (i.e., the resource has been deleted and then added). SYNC Change constant indicating that the resource's sync status has changed. TYPE Change constant indicating that the type of the resource has changed.
The IResourceDelta class also defines numerous useful APIs as follows:
accept(IResourceDeltaVisitor) Visits resource deltas that are ADDED, CHANGED, or REMOVED. If the visitor returns true, the resource delta's children are also visited. accept(IResourceDeltaVisitor, boolean) Same as above but optionally includes phantom resources. accept(IResourceDeltaVisitor, int) Same as above but optionally includes phantom resources and/or team private members. findMember(IPath) Finds and returns the descendent delta identified by the given path in this delta, or null if no such descendent exists. getAffectedChildren() Returns resource deltas for all children of this resource that were ADDED, CHANGED, or REMOVED. getAffectedChildren(int) Returns resource deltas for all children of this resource whose kind is included in the given mask. getFlags() Returns flags that describe in more detail how a resource has been affected. getFullPath() Returns the full, absolute path of this resource delta. getKind() Returns the kind of this resource delta. getMarkerDeltas() Returns the changes to markers on the corresponding resource. getMovedFromPath() Returns the full path (in the "before" state) from which this resource (in the "after" state) was moved. getMovedToPath() Returns the full path (in the "after" state) to which this resource (in the "before" state) was moved. getProjectRelativePath() Returns the project-relative path of this resource delta. getresource() Returns a handle for the affected resource.
|