Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » resource change listener
resource change listener [message #1092404] Thu, 22 August 2013 19:04 Go to next message
Kevin Regan is currently offline Kevin Regan
Messages: 33
Registered: May 2013
Member
I am noticing that I only receive ResourceChangeEvents when an item that is open in the workspace gets modified. I am looking to get notified if the underlying file gets modified regardless of whether it is actually open in an editor in the workspace. Is this not possible? None of the flags seem relevant so I'm starting to believe that I cannot use this mechanism to track updates to a file that are made by an external system? Basically, I have a file that is used to communicate to my eclipse plugin from an external program - when it gets changed, I need to detect this and then do things within the eclipse workspace to reflect the update. My intended approach was to programmatically add this file into my eclipse project so that I could detect changes to it using the resource change listener but it appears to only work if the file is open in an editor. Please let me know if I'm trying to do something that is not possible and optionally suggest an alternative (other than a completely homegrown solution I kick off in a thread as part of my plugin init).

		/************* RESOURCE CHNAGE LISTENER TEST *********************/
				
		IResourceChangeListener listener = new IResourceChangeListener() {
			public void printDelta(IResourceDelta d, String indent) { 
				
				IResource resource = d.getResource();
				IJavaElement javaElement = JavaCore.create(resource);
				String resourceName = null;
				if (javaElement instanceof IJavaProject) { 
					resourceName = "Java project " + resource.getName();
				} else if (javaElement instanceof IPackageFragmentRoot) { 
					resourceName = "PFR " + resource.getName();
				} else if (javaElement instanceof IPackageFragment) { 
					resourceName = "PF " + resource.getName();
					
				} else if (javaElement instanceof ICompilationUnit) { 
					resourceName = "CU " + resource.getName();
				} else {
					resourceName = resource.getName();
				}
				
				System.out.println(indent.concat("Delta " + d.getKind() + " for resource " + resourceName));
								
				for (IResourceDelta childDelta : d.getAffectedChildren()) {
					printDelta(childDelta, indent.concat("\t"));
				}
			}
			public void resourceChanged(IResourceChangeEvent e) { 
			
				
				if (e.getType() == IResourceChangeEvent.POST_CHANGE) {
					System.out.println("Post change event");
					
					
					IResourceDelta delta = e.getDelta();
					printDelta(delta, "");
					
				} else {
					System.out.println("Event type was " + e.getType());
				}
			}
		};
		
		ResourcesPlugin.getWorkspace().addResourceChangeListener(listener);
		
		/************* ***************************************************/
Re: resource change listener [message #1105414 is a reply to message #1092404] Mon, 09 September 2013 20:59 Go to previous message
Kevin Regan is currently offline Kevin Regan
Messages: 33
Registered: May 2013
Member
So there is a preference in Windows->Preferences->General->Workspace called "Refresh using native hooks or polling" which does what I want. It causes whatever service is running in the background looking for resource changes to poll the file system for changes to resources in the workspace in addition to detecting changes made from within the workspace itself. This allows me to detect changes to files in my project made externally, outside of eclipse.

So the problem now is that I need my startup plugin to enable this preference so that I can detect such changes. I have found that you can modify preferences this way:

		IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(Startup.PLUGIN_ID);
		prefs.putBoolean(ResourcesPlugin.PREF_AUTO_REFRESH, true);



The preference I am trying to modify is identified by ResourcesPlugin.PREF_AUTO_REFRESH which is equal to "refresh.enabled" - the above method does not work. I thinks it's because I'm trying to modify that preference in my Plugin-specific preferences which is not the one that the refresh service cares about - I need to modify the one in my workspace settings I believe. By specifying the "org.eclipse.core.resources" node I can get it to work:

			IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode("org.eclipse.core.resources");
			prefs.putBoolean(ResourcesPlugin.PREF_AUTO_REFRESH, true);


However, I suspect this is not right because it gets persisted to subsequent workspace sessions, because it is being written into the prefs file within the workspace directory structure:

me@localhost ~/runtime-EclipseApplication/.metadata/.plugins
$ find . -name "*.prefs" | xargs grep -i refresh
./org.eclipse.core.runtime/.settings/org.eclipse.core.resources.prefs:refresh.en
abled=true



I'm hoping someone here can shed some light on this - I feel like there must be a more appropriate way to do this so that I can override that preference just for the duration of my plugin. Is it not possible to override a core preference by specifying it in your plugin's preference node?
Previous Topic:[target] Unnecessary remote fetch of locally available artifacts during target resolution
Next Topic:Create server programmatically - SOLVED
Goto Forum:
  


Current Time: Mon Sep 22 04:34:14 GMT 2014

Powered by FUDForum. Page generated in 0.01694 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software