What if you want to keep track of all of the changes to the catalog that happen while your plug-in is running? You can register an IResolveChangeListener with the catalog plugin. Your listener will be notified of the changes via an IResolveChangeEvent object, which describes the changes.
Registering a listenerFirst, you must register a resource change listener with the catalog plugin. IResolveChangeListener listener = new MyResolveChangeReporter();
CataqlogPlugin.addResourceChangeListener( listener );
Your listener will be notified after modifications to the resources have been made. Catalog API methods that modify resources trigger these events as part of their documented behavior. The method comment for a Catalog API method explicitly states whether or not it triggers a resource change event. For example, the following is included in the IService.getInfo() comment: When new information is available changes will be reported in a change event, including an indication that this service's contents have been changed. Most catalog resources trigger these events the first time the handle is used. Methods that create, delete, or change a resource typically trigger these events on every use. Resolve change eventsThe resolve change event describes the specifics of the change that have occurred in the catalog. The event contains a "delta" that describes the net effect of the changes. The resolve delta is structured as a tree rooted at the catalog. The resolve delta tree describes these types of changes:
To traverse a resolve delta tree, you may implement the IResolveDeltaVisitor interface or traverse the tree explicitly using IResource.getChildren. Resolve delta visitors implement a visit method that is called by the resource delta as it enumerates each change in the tree. The types of resource change events and when they are reported.
Implementing a resolve change listenerThe following example implements a console-based resolve change listener. A resolve change listener is registered for events and information about these events is printed to the console: IResolveChangeListener listener = new MyResolveChangeReporter();
CatalogPlugin.addListener(listener);
The listener checks for each event type and reports information about the resolve that was changed and the kinds of changes that occurred. import org.eclipse.resources.*; import org.eclipse.runtime.*; static import public class MyResolveChangeReporter implements IResolveChangeListener { public void changed(IResolveChangeEvent event) { IResolve res = event.getResolve(); switch (event.getType()) { case IResolveChangeEvent.PRE_CLOSE: System.out.print(res.getIdentifier()); System.out.println(" is about to close."); break; case IResolveChangeEvent.PRE_DELETE: System.out.print(res.getFullPath()); System.out.println(" is about to be deleted."); break; case IResolveChangeEvent.POST_CHANGE: System.out.println("Resources have changed."); event.getDelta().accept(new DeltaPrinter()); break; } } } The DeltaPrinter class implements the IResolveDeltaVisitor interface to interrogate the resolve delta. The visit() method is called for each resolve change in the resolve delta. The visitor uses a return value to indicate whether deltas for children should be visited. class DeltaPrinter implements IResolveDeltaVisitor { public boolean visit(IResourceDelta delta) { IResource res = delta.getResource(); IResource old = delta.getOldResource(); switch (delta.getKind()) { case IResolveDelta.Kind.ADDED: System.out.print("Resource "); System.out.print(res.getIdentifier()); System.out.println(" was added."); break; case IResolveDelta.Kind.REMOVED" System.out.print("Resource "); System.out.print(res.getIdentifier()); System.out.println(" was removed."); break; case IResolveDelta.Kind.CHANGED: System.out.print("Resource "); System.out.print(res.getIdentifier()); System.out.println(" has changed."); break; case IResolveDelta.Kind.REPLACED: System.out.print("Resource "); System.out.print(res.getIdentifier()); System.out.print(" is replaced by "); System.out.print(res.getNewIdentifier().getIdentifier()); break; } return true; // visit the children } } For a complete description of resolve deltas, visitors, consult the API specification for IResolveDelta and IResolveDeltaVisitor. Note: Resolve change listeners are useful for tracking changes that occur to resources while your plug-in is activated. If your plug-in registers a listener during its startup code, it's possible that many resolve change events have been triggered before the activation of your plug-in. Note: Note most change events are triggered during processing that occurs in a background thread. Resolve change listeners should be thread-safe. |
(c) Copyright (c) 2004,2005 Refractions Research Inc. and others. |