Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IResourceChangeEvent - how to identify a project delete/rename

How can I properly distinguish, inside an IResourceChangeListener that is added via ResourcePlugin.getWorkspace().addResourceChangeListener(...) that a Project was deleted / renamed?

Through trying things out, it would seem that the IResourceChangeEvent.getDelta() -> IResourceDelta would be the answer.

From Eclipse API:

After-the-fact batch reports of arbitrary creations, deletions and modifications to one or more resources expressed as a hierarchical resource delta. Event type is POST_CHANGE, and getDelta returns the hierarchical delta. The resource delta is rooted at the workspace root. These events are broadcast to interested parties after a set of resource changes and happen whether or not autobuilding is enabled. The workspace is closed for change during notification of these events. The delta reported in this event cycle is identical across all listeners registered for this type of event.

EDIT: adding my findings so far

So, the Event.getType() is POST_CHANGE and there needs to be either

  • a delta that contains a child IResourceDelta that has the getKind() == REMOVED for a deletion, and the delta getResource().getType() == PROJECT so that we know it's a project ( this IResourceDelta should not have any children )

  • a getDelta() that contains two children IResourceDelta that have getKind() == REMOVED and getKind() == ADDED, also the getResource().getType() == PROJECT, and those IResourceDelta contain no children Deltas (I've observed that when a file is renamed/deleted, the IProject the resource belongs to is the one that is on the first row of children of the parent Delta...)

Can someone confirm this supposition? Is if it really necessary to do a tree depth search for children to realize if the Event is of a project rename/delete or a file/folder?

like image 872
Vlad Ilie Avatar asked Oct 07 '14 09:10

Vlad Ilie


1 Answers

  • You need to add a resource change listener via ResourcePlugin.getWorkspace().addResourceChangeListener(listener, IResourceChangeEvent.POST_CHANGE)

  • In your listener, use a IResourceDeltaVisitor to visit all the changes in the delta via event.getDelta().accept(...)

  • The visitor should look into the projects that are REMOVED

  • If you are expecting the project to be removed and created with the same name (as happens to files & folders during build events), then in addition to the REMOVED, also look for REPLACED in the delta

like image 65
Prakash G. R. Avatar answered Nov 18 '22 17:11

Prakash G. R.