Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » [SOLVED] How to synchronize a copy project in the background using JavaElementDeltas?
icon4.gif  [SOLVED] How to synchronize a copy project in the background using JavaElementDeltas? [message #648109] Tue, 11 January 2011 04:20 Go to next message
Kivanc Muslu is currently offline Kivanc Muslu
Messages: 153
Registered: November 2010
Senior Member
For people who are interested with a better sync (without needing to save), please see this post: http://www.eclipse.org/forums/index.php?t=msg&th=203265& amp;S=b5d5c7bdcea0b480fec5983a64c3aa1c

Note: I had posted a very similar message to Eclipse JDT Forums and someone recommended me to post here also.

Hi all,

Let's say that I have created a copy of a project (IProject) using IProject.copy(). I want this copy to be in sync with the original project as the user changes the original project.

Looking at the Eclipse API I have seen
JavaCore.addElementChangedListener(IElementChangedListener listener);

This is pretty interesting and works fine for getting all the changes done to the project as ElementChangedEvents. I can event get JavaElementDeltas (and I presume that Deltas represent the exact change between two versions of the project), however my problem is that I cannot find a way to apply these deltas to the project.

So my question(s):
1-) Is there a way to apply a JavaElementDelta to a project (IProject)? (I assume that if I apply all the deltas I got from the original project to the copy project, they will always be in sync)
2-) If yes to 1, is it possible to apply a JavaElementDelta generated by an IProject (let's say project1) to another IProject (project2, in my case the copy project)?
3-) Is there another way to do what I want to do (i.e., keep 2 projects in sync textually)? (as long as it is doable and uses Eclipse API, it doesn't matter if it is hard, easy, hacky or etc.)?

Thanks a lot in advance, regards,

[Updated on: Thu, 27 January 2011 08:18]

Report message to a moderator

Re: How to synchronize a copy project in the background using JavaElementDeltas? [message #648179 is a reply to message #648109] Tue, 11 January 2011 11:18 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 940
Registered: July 2009
Senior Member
Two answers for you...

1. You may be able to use linked folders instead of actually copying files. With linked folders you can have two projects include the same physical folder on disk. Create the projects. The in the new folder wizard, hit the advanced button and go from there. I have used this feature before to x-compile the same code against different versions of a library (N separate projects).

2. If you do need actual physical copying, take a look at IResourceChangeListener and IWorkspace.addResourceChangeListener() API instead. This API deals with changes at file level, so it sounds more appropriate for your problem. You will still get a delta in your listener.

I have never seen any way to apply a delta, but writing apply logic should be pretty trivial. After all, you are being told exactly what has changed. Several things to keep in mind:

A. Make sure that your plugin is configured to auto-start. You want to get your listener added as quickly as possible; before any changes are made and events could be missed. Even with auto-start, there is a risk that you will miss changes, so I would recommend performing a diff of projects on plugin startup and reconciling changes before going into the listening mode. Make sure to do the initial diff/reconcile in a Job. Don't block plugin startup.

B. You will likely want to filter out certain directories from your synchronization, such as the Java output directory where the class files are written. Filter out project metadata files as well.

C. Make sure that you are using Eclipse resources API (IFile and IFolder) instead of Java IO for all of your operations. Make sure that you understand resource stale state and the refresh operation. When a resource is stale in workspace (has been modified external), reading or writing it will throw an exception. Will need a strategy for dealing with stale resources. May want to just skip them. When user hits refresh, your listener will get a delta with changes.

- Konstantin

[Updated on: Tue, 11 January 2011 11:21]

Report message to a moderator

Re: How to synchronize a copy project in the background using JavaElementDeltas? [message #648221 is a reply to message #648179] Tue, 11 January 2011 16:01 Go to previous messageGo to next message
Kivanc Muslu is currently offline Kivanc Muslu
Messages: 153
Registered: November 2010
Senior Member
Hi Konstantin,

Thank you very much for your response. Just a quick question: when you say

Quote:
I have never seen any way to apply a delta, but writing apply logic should be pretty trivial. After all, you are being told exactly what has changed.


and with the remaining of your suggestions, do you mean that I get the actual change in the deltas (i.e., what exactly changed from two consecutive project points), however, there is no API to apply a delta itself (i.e., calling Eclipse's internal methods) and I have to implement my own apply method.

Moreover, as far as I understand you are suggesting me to apply the change at IFile or IResource level (i.e., recopy the change file or resource). Am I correct? Is there no way to just apply the change? It might not really give me that much overhead to copy the whole file, however if there were a way to apply only the diff that would be great.

Thanks again!
Re: How to synchronize a copy project in the background using JavaElementDeltas? [message #648238 is a reply to message #648221] Tue, 11 January 2011 18:26 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 940
Registered: July 2009
Senior Member
The resource delta just tells you what has changed at the file level. I doesn't give you a diff of file contents. When you see change, just read the file and write it in the other project.

The deltas delivered to listener aren't designed to be applied to anything. They are designed for notification purposes only.

- Konstantin
Re: How to synchronize a copy project in the background using JavaElementDeltas? [message #648239 is a reply to message #648238] Tue, 11 January 2011 18:30 Go to previous messageGo to next message
Kivanc Muslu is currently offline Kivanc Muslu
Messages: 153
Registered: November 2010
Senior Member
Konstantin,

Thank you very much for all help. I think I understand a little better now. I always imagined that there should be something that would give me the changes as a diff that can easily applied to the other project. Now I understand, there is no such thing.

I will go on and try to implement what I want over resource deltas, I will let you know if it works with a reasonable performance.

~ Kivanc
Re: How to synchronize a copy project in the background using JavaElementDeltas? [message #648433 is a reply to message #648238] Wed, 12 January 2011 17:05 Go to previous messageGo to next message
Kivanc Muslu is currently offline Kivanc Muslu
Messages: 153
Registered: November 2010
Senior Member
Konstantin,

I have implemented a prototype to test this suggestion. It works pretty well for now. Though I hadn't tried it on something that would require a lot of performance (like many changed at once), its performance is quite reasonable.

I would like to ask you a question though:
You suggested me to read the contents of the changed file and write it to the copy. What I do at the moment is that I completely delete the copy first and copy the original to the same copy destination second. Would this approach (which basically does the same thing as you said) use more computing than reading the changed file and writing it (over Streams I presume)? Do you have any empirical or theoretical knowledge?

Again, thanks a lot for the suggestions and solution.
Re: How to synchronize a copy project in the background using JavaElementDeltas? [message #648634 is a reply to message #648433] Thu, 13 January 2011 12:56 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 940
Registered: July 2009
Senior Member
Delete followed by a copy would definitely use more computer resources than using IFile.setContents() API. Not only will you be performing more OS-level I/O operations, but Eclipse will likely detect and fire multiple resource change events instead of one.

- Konstantin
Re: How to synchronize a copy project in the background using JavaElementDeltas? [message #651019 is a reply to message #648634] Thu, 27 January 2011 08:18 Go to previous message
Kivanc Muslu is currently offline Kivanc Muslu
Messages: 153
Registered: November 2010
Senior Member
Thanks for all help Konstantin!
Previous Topic:Provisioning Exception - No repository found
Next Topic:tooltips at properties view
Goto Forum:
  


Current Time: Sat Aug 30 00:33:33 EDT 2014

Powered by FUDForum. Page generated in 0.01712 seconds