Eclipse Plugin: Listening to Marker Deletions

Today I was desperately trying to figure out how to get eclipse to tell me when a Bookmark had been deleted, so that I could update my plugins internal data representation.

I found lots of api’s which was all well and good but finding a code example was a lot harder. Also the guys on the irc channel were too busy to help, so I kept searching.

It turns out that you need to fiddle with the IResourceChangeListener and IResourceDeltaVisitor to traverse a delta (basically a tree representation of all the changes that have just occurred) and look for your bookmarks.

Below is example code for creating a listener, visitor and how to attach it to the workspace as a change listener.

	// this listener deals with handling changes to markers.
	private static IResourceChangeListener markerListener =  new IResourceChangeListener() {
				private MarkerChangeVisitor fVisitor = new MarkerChangeVisitor();

				public void resourceChanged(IResourceChangeEvent event) {
		           IResourceDelta delta= event.getDelta();
		           if (delta != null)
		           {
		                try {
		                    delta.accept(fVisitor);
		                } catch (CoreException ce) {

		                }
				   }
				   }
		   };


/**
 * This class deals with navigating the IResourceDelta to locate changes in markers
 * i.e. whether they are added, changed or deleted.
 *
 * @author Stephen Cummins, Durham University, (c) 12 Aug 2009
 */
public class MarkerChangeVisitor implements IResourceDeltaVisitor
{
	   /**
	    * The method to be called for each visited delta.
	    * @return a boolean value
	    */
	   public boolean visit(IResourceDelta delta) throws CoreException
	   {
		   if(delta == null)
			   	return false;

		   IMarkerDelta[] markerDeltas = delta.getMarkerDeltas();

		   for (int i= 0; i < markerDeltas.length; i++) {
			   IMarkerDelta markerDelta= markerDeltas[i];
			   if (markerDelta.isSubtypeOf(IMarker.BOOKMARK)) {
				   switch (markerDelta.getKind()) {
				   case IResourceDelta.ADDED :
					   // Could do something here
					   break;
				   case IResourceDelta.REMOVED :
					   //Do something here
					   break;
				   case IResourceDelta.CHANGED:
					   // Could do something here
					   break;
				   }
			   }
		   }
		   return true;
	   }
}

To attach it to the workspace you must do something like this:

	/**
	 * startMarkerListener
	 * This method will add the MarkerListener to the workspace to alert the plugin when a marker is changed
	 * 
	 */
	public static void startMarkerListener() {
		IWorkspace workspace = ResourcesPlugin.getWorkspace();		
		workspace.addResourceChangeListener(markerListener);
	}
	
	/**
	 * shutdownMarkerListener
	 * This method will cause the plugin to stop being notified about marker changes.
	 */
	public static void shutdownMarkerListener(){
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		workspace.removeResourceChangeListener(markerListener);
	}

Resources to help:

, , , , ,

  1. #1 by sandraraven15 on 9 September 2009 - 13:18

    Hi! I was surfing and found your blog post… nice! I love your blog. 🙂 Cheers! Sandra. R.

  2. #2 by immifebon on 10 September 2009 - 01:23

    Спасибо за пост, весьма интересно. Я согласен с предыдущим комментарием.