|
Eclipse Tracking Changes
How to Track resource changes
In Last chapter (Views) we created a property manager view. Whenever
user click refresh action, this view searches for all the property files
in the workspace and displays it to the user. Wouldn’t it be cool if
this view can refresh automatically whenever a property file is added to
workspace? If you answer yes, then you are at right place.
Eclipse provides IResourceChangeListener to keep track of resource
changes in the workspace. Eclipse generates resource change events
indicating that the files and folders that have been added, modified,
and removed from the workspace. Interested parties can subscribe to
these events and take necessary action. The interface
org.eclipse.core.resources.IResourceChangeListener defines single
method:
public void resourceChanged (IResourceChangeEvent event). In this
chapter we will modify PropertyManagerView so as to implement
IResourceChangeListener and use IResourceChangeEvent to update itself.
Before we move on it is important to understand IResourceChangeEvent.
Note about IResourceChangeEvent
According to Eclipse API Specification there are currently five
different types of resource change events:
- PRE_BUILD
- POST_BUILD
- POST_CHANGE
- PRE_CLOSE
- PRE_DELETE
Please refer to online
Eclipse API Specification to know more about these events.
Note about IResourceDelta
A resource delta represents changes in the state of a resource tree
between two discrete points in time OR we can say it is through delta
object that we come to know about the changes taking place in workspace.
Whenever resource modification is performed by the user delta of
modification is prepared by eclipse and interested parties can use this
delta to update themselves. This interface declares many constants
describing the kind of change.
Please refer to online
Eclipse API Specification to know about methods provided by
IResourceDelta.
Let’s Modify Property Manager View
Best way to learn above mentioned theory is to practice with concrete
example. In this section we will take step by step approach to
modify Property Manager View so that it can keep itself updated with
resource modifications in workspace. Lets start by implementing
IResourceChangeListener. Open com.myplugin.rmp.views.PropertyManagerView
in java editor and implement resource change listener as shown below.
public class PropertyManagerView extends ViewPart implements
IResourceChangeListener
Next, register PropertyManagerView with Work Space so that this view is
notified whenever work space modification event is generated.
Modify createPartControl method as shown below:
public void createPartControl(Composite parent) {
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setInput(getViewSite());
ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
hookContextMenu();
hookDoubleCLickAction();
}
|
Next, Override dispose method of view to remove Listener. Dispose
method of View is called every time the View Part is destroyed.
public void dispose() {
super.dispose();
ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
}
|
Next, Override resourceChanged method from IResourceChangeListener as
shown below:
1. public void resourceChanged(IResourceChangeEvent event) {
2. try{
3. if(event.getType() == IResourceChangeEvent.POST_CHANGE){
4. event.getDelta().accept(new IResourceDeltaVisitor(){
5. public boolean visit(IResourceDelta delta) throws CoreException {
6. if(delta.getResource().getName().endsWith(".properties")){
7. initialize();
8. Display.getDefault().asyncExec(new Runnable() {
9. public void run() {
10. viewer.refresh();
11. viewer.expandAll();
13. }
14 });
15. }
16. return true;
17. }
18. });
19. }
20. }catch(CoreException e){
21. // log it
22. e.printStackTrace();
23. }
24. }
|
Line 3: We are only interested in Post change events. So here we are
checking the event type. If the event is a POST_CHANGE event then we
proceed ahead.
Line 4: getDelta() method in IResourceChangeEvent returns the delta i.e.
information about the modifications which triggered this event.
Line 5: IResourceDeltaVisitor is an object that visits (visitor pattern)
resource deltas. It visits resource deltas that are ADDED, CHANGED, or
REMOVED. In case visitor returns true - the resource delta's children
are also visited.
Line 6: Here we are retrieving the resource which has been modified and
checking if it is a .properties file.
Line 7: If the modified resource is a .properties file then we are
initializing our model object again.
Line 8 – 13: Next we want to refresh viewer so that it updates itself
and resource changes are visible in Property Manager View. The code
Display.getDefault().asyncExec(new Runnable(){… makes sure that we don’t
get illegal thread access exception. If we try to refresh viewer outside
of this code (say immediately after initialize is called) then we will
get illegal thread access exception. Reason? Basically the
resourceChanged Method is called in a non UI thread and when we try to
modify UI by calling viewer.refresh() in this non UI thread it throws
exception. In order to overcome this problem we have redirected the
execution to UI Thread by using Display object.
Let’s Test Property Manager View
Launch eclipse application from plug-in manifest editor. Open up
Property manager View (Window > show view > other). Create property
files in Project under folder (Property Files) and notice that Property
manager View Refreshes on every addition/deletion of property file
resource.

|