{"id":16,"date":"2009-08-13T10:01:23","date_gmt":"2009-08-13T09:01:23","guid":{"rendered":"http:\/\/lexxweb.co.uk\/blog\/?p=16"},"modified":"2009-09-29T15:22:14","modified_gmt":"2009-09-29T14:22:14","slug":"eclipse-plugin-listening-to-marker-deletions","status":"publish","type":"post","link":"https:\/\/blog.lexxweb.co.uk\/?p=16","title":{"rendered":"Eclipse Plugin: Listening to Marker Deletions"},"content":{"rendered":"<p>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.<\/p>\n<p>I found lots of api&#8217;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.<\/p>\n<p>It turns out that you need to fiddle with the IResourceChangeListener and IResourceDeltaVisitor\u00a0to traverse a delta (basically a tree representation of all the changes that have just occurred) and look for your bookmarks.<\/p>\n<p>Below is example code for creating a listener, visitor and how to attach it to the workspace as a change listener.<\/p>\n<p><code><\/p>\n<pre lang=\"java\">\t\/\/ this listener deals with handling changes to markers.\r\n\tprivate static IResourceChangeListener markerListener =  new IResourceChangeListener() {\r\n\t\t\t\tprivate MarkerChangeVisitor fVisitor = new MarkerChangeVisitor();\r\n\r\n\t\t\t\tpublic void resourceChanged(IResourceChangeEvent event) {\r\n\t\t           IResourceDelta delta= event.getDelta();\r\n\t\t           if (delta != null)\r\n\t\t           {\r\n\t\t                try {\r\n\t\t                    delta.accept(fVisitor);\r\n\t\t                } catch (CoreException ce) {\r\n\r\n\t\t                }\r\n\t\t\t\t   }\r\n\t\t\t\t   }\r\n\t\t   };<\/pre>\n<p><!--more--><br \/>\n<\/code><code><\/p>\n<pre lang=\"java\">\/**\r\n * This class deals with navigating the IResourceDelta to locate changes in markers\r\n * i.e. whether they are added, changed or deleted.\r\n *\r\n * @author Stephen Cummins, Durham University, (c) 12 Aug 2009\r\n *\/\r\npublic class MarkerChangeVisitor implements IResourceDeltaVisitor\r\n{\r\n\t   \/**\r\n\t    * The method to be called for each visited delta.\r\n\t    * @return a boolean value\r\n\t    *\/\r\n\t   public boolean visit(IResourceDelta delta) throws CoreException\r\n\t   {\r\n\t\t   if(delta == null)\r\n\t\t\t   \treturn false;\r\n\r\n\t\t   IMarkerDelta[] markerDeltas = delta.getMarkerDeltas();\r\n\r\n\t\t   for (int i= 0; i &lt; markerDeltas.length; i++) {\r\n\t\t\t   IMarkerDelta markerDelta= markerDeltas[i];\r\n\t\t\t   if (markerDelta.isSubtypeOf(IMarker.BOOKMARK)) {\r\n\t\t\t\t   switch (markerDelta.getKind()) {\r\n\t\t\t\t   case IResourceDelta.ADDED :\r\n\t\t\t\t\t   \/\/ Could do something here\r\n\t\t\t\t\t   break;\r\n\t\t\t\t   case IResourceDelta.REMOVED :\r\n\t\t\t\t\t   \/\/Do something here\r\n\t\t\t\t\t   break;\r\n\t\t\t\t   case IResourceDelta.CHANGED:\r\n\t\t\t\t\t   \/\/ Could do something here\r\n\t\t\t\t\t   break;\r\n\t\t\t\t   }\r\n\t\t\t   }\r\n\t\t   }\r\n\t\t   return true;\r\n\t   }\r\n}<\/pre>\n<p><\/code><\/p>\n<p>To attach it to the workspace you must do something like this:<br \/>\n<code><\/p>\n<pre lang=\"java\">\r\n\t\/**\r\n\t * startMarkerListener\r\n\t * This method will add the MarkerListener to the workspace to alert the plugin when a marker is changed\r\n\t * \r\n\t *\/\r\n\tpublic static void startMarkerListener() {\r\n\t\tIWorkspace workspace = ResourcesPlugin.getWorkspace();\t\t\r\n\t\tworkspace.addResourceChangeListener(markerListener);\r\n\t}\r\n\t\r\n\t\/**\r\n\t * shutdownMarkerListener\r\n\t * This method will cause the plugin to stop being notified about marker changes.\r\n\t *\/\r\n\tpublic static void shutdownMarkerListener(){\r\n\t\tIWorkspace workspace = ResourcesPlugin.getWorkspace();\r\n\t\tworkspace.removeResourceChangeListener(markerListener);\r\n\t}\r\n<\/pre>\n<p><\/code><\/p>\n<p>Resources to help:<\/p>\n<ul>\n<li><a href=\"http:\/\/help.eclipse.org\/stable\/index.jsp?topic=\/org.eclipse.platform.doc.isv\/reference\/api\/org\/eclipse\/core\/resources\/IResourceChangeEvent.html\">IResourceChangeEvent Java API<\/a><\/li>\n<li><a href=\"http:\/\/www.eclipse.org\/articles\/Article-Mark%20My%20Words\/mark-my-words.html\">Mark My Words, tutorial<\/a><\/li>\n<li><a href=\"http:\/\/help.eclipse.org\/stable\/index.jsp?topic=\/org.eclipse.platform.doc.isv\/guide\/resAdv_markers.htm\">Resource Markers Guide<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s which was all well and good but finding a code example was a lot harder. Also the guys on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[20,13,25,18,14,19],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-eclipseplugins","tag-delta","tag-eclipse","tag-java","tag-markers","tag-plugin","tag-visitor-pattern"],"_links":{"self":[{"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16"}],"version-history":[{"count":15,"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":32,"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions\/32"}],"wp:attachment":[{"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}