Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » [SOLVED] How to get notified when user closed an editor file without saving it?
icon14.gif  [SOLVED] How to get notified when user closed an editor file without saving it? [message #653607] Thu, 10 February 2011 11:47 Go to next message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
Hi all,

I am using IBufferChangeListeners and an IResourceChangeListener to keep track of the textual changes in an eclipse project. My problem is that, whenever the user makes a change, I also do the same change in a copy project (that is in sync with the original one). This works great, however for the implementation reasons I have to save the copy project even if the user does not save the original files.

My problem is that if the user closes the file editor in Eclipse and does not save his/her changes, since I have already saved them in the copy project, the projects got out of sync. Thus, I need to get a notification when user does this (i.e., closes a file editor without saving it). My questions:

1- I don't get any ResourceChangedEvents or BufferChangedEvents when this happens, is this normal? (It seems like)
2- Is there an extension point in Eclipse API to do what I want? If so, could you please point me some code, documentation or a clue?
3- If not, is this doable? Again, could you point me a starting point to dig up the code?

Thanks a lot in advance, best regards,

[Updated on: Wed, 16 February 2011 22:35]

Report message to a moderator

Re: How to get notified when user closed an editor file without saving it? [message #653623 is a reply to message #653607] Thu, 10 February 2011 12:53 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
On 10.02.2011 12:47, Kivanc Muslu wrote:
> Hi all,
>
> I am using IBufferChangeListeners and an IResourceChangeListener to
> keep track of the textual changes in an eclipse project. My problem is
> that, whenever the user makes a change, I also do the same change in a
> copy project (that is in sync with the original one). This works
> great, however for the implementation reasons I have to save the copy
> project even if the user does not save the original files.
> My problem is that if the user closes the file editor in Eclipse and
> does not save his/her changes, since I have already saved them in the
> copy project, the projects got out of sync. Thus, I need to get a
> notification when user does this (i.e., closes a file editor without
> saving it). My questions:
>
> 1- I don't get any ResourceChangedEvents or BufferChangedEvents when
> this happens, is this normal? (It seems like)
> 2- Is there an extension point in Eclipse API to do what I want? If
> so, could you please point me some code, documentation or a clue? 3-
> If not, is this doable?
Yes, either use an IPartListener2 or use file buffers instead of the
IBufferChangeListeners.

Dani
> Again, could you point me a starting point to dig up the code?
>
> Thanks a lot in advance, best regards,
Re: How to get notified when user closed an editor file without saving it? [message #654746 is a reply to message #653623] Wed, 16 February 2011 22:34 Go to previous message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
Hi Dani,

Thanks for the suggestions. Though I could only had little chance to look at the APIs you have mentioned, I have found another solution to my problem and will share the high level algorithm here, in case anybody else faces the same problem.

To apply this solution, you need to have a very similar situation as in my case: you need a shadow file that is always in sync (even if the changes are not saved), and have a buffer listener in the original file.

So, every time a buffer is closed (i.e., file is closed from the editor), it signals a BufferChangeEvent. You can detect this as a special event by checking
event.getBuffer().isClosed()

When you see that the buffer is really closed, then you can go through the resource, compilation unit and get its new buffer to see its updated contents. When the changes are not saved, though no additional BufferChangeEvents are signaled, the new buffer attached to the same resource will have the correct (i.e., correct with discarded changes) contents. Then, you can compare this contents with your shadow file's contents and sync them in case they differ.

Again Dani, thanks for all help!
Previous Topic:Audio for server startup/shutdown events not working
Next Topic:JAR export error
Goto Forum:
  


Current Time: Thu Apr 25 22:18:08 GMT 2024

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

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

Back to the top