Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Getting non-saved changes using IResourceChangeListener
icon4.gif  Getting non-saved changes using IResourceChangeListener [message #649601] Wed, 19 January 2011 21:09 Go to next message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
Hi all,

I need to get all files modified (added, changed or deleted) in a project immediately after the change is done. For this, I am now using a custom implementation of IResourceChangeListener, which works pretty good in general.

I can get the DELETED and ADDED events as soon as they are performed on the project I am interested in, however I cannot get the CHANGED events immediately. I only get them (as a bunch of events some times) when user saves the related file(s).

Is there a way to get these events before the user saves the file? If no, is there another way to do what I want to do (i.e., get events as soon as they are done)?

Thanks in advance, regards,
Re: Getting non-saved changes using IResourceChangeListener [message #649644 is a reply to message #649601] Thu, 20 January 2011 03:23 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
On 1/20/2011 2:39 AM, Kivanc Muslu wrote:
> Hi all,
>
> I need to get all files modified (added, changed or deleted) in a
> project immediately after the change is done. For this, I am now using a
> custom implementation of IResourceChangeListener, which works pretty
> good in general.
>
> I can get the DELETED and ADDED events as soon as they are performed on
> the project I am interested in, however I cannot get the CHANGED events
> immediately. I only get them (as a bunch of events some times) when user
> saves the related file(s).
A resource - a file on the file system - is not changed until the file
is saved. Till the file is saved the changes remain in the editor and
its underlying buffer.

> Is there a way to get these events before the user saves the file? If
> no, is there another way to do what I want to do (i.e., get events as
> soon as they are done)?
>
> Thanks in advance, regards,
Are you trying to do this in Java editor ?
Re: Getting non-saved changes using IResourceChangeListener [message #649645 is a reply to message #649644] Thu, 20 January 2011 03:30 Go to previous messageGo to next message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
Hi Deepak,

Thanks again for the answer.

I am still trying to implement my plug-in where a copy project should be in sync with the project I am monitoring. So I need to use Eclipse API to get the notifications.

If I remember correctly you suggested me to use IResourceChangeListener, which works great, but as I mentioned the problem is that I cannot get the changes unless user actually saves them.

On the other hand the eclipse itself, can show the result of the changes before the user saves them (think that you are using the Eclipse to write code). When you write something that would generate an error (I think you also need autobuild to be selected), the thing you write is automatically underlined and a quick fix on the left automatically appears before you even save the file.

I need to mimic this behavior (somehow) on the copy project, so I need to get the changes that are not saved (so that I can apply them to the copy). As far as I can see IElementChangedListener can do this, however as you also suggested, the events generated by this listener is just huge. So if it does the trick I really do need a way to filter some of the related events I am interested about (I am not sure if it really does the job).

I hope the question is more clear now, please ask if any part does not make sense.

Thanks a lot again, regards,
Re: Getting non-saved changes using IResourceChangeListener [message #649650 is a reply to message #649645] Thu, 20 January 2011 04:39 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
On 1/20/2011 9:00 AM, Kivanc Muslu wrote:
> Hi Deepak,
>
> Thanks again for the answer.
>
> I am still trying to implement my plug-in where a copy project should be
> in sync with the project I am monitoring. So I need to use Eclipse API
> to get the notifications.
> If I remember correctly you suggested me to use IResourceChangeListener,
> which works great, but as I mentioned the problem is that I cannot get
> the changes unless user actually saves them.
I can't imagine why you would need a 'copy project', but I am sure you
have good reasons for this. :)

> On the other hand the eclipse itself, can show the result of the changes
> before the user saves them (think that you are using the Eclipse to
> write code). When you write something that would generate an error (I
> think you also need autobuild to be selected), the thing you write is
> automatically underlined and a quick fix on the left automatically
> appears before you even save the file.
As I said only the editor knows about these changes. The reconciling
happens in
org.eclipse.jdt.internal.ui.text.JavaReconciler.ElementChang edListener.elementChanged(ElementChangedEvent).
See
org.eclipse.jdt.internal.ui.text.JavaReconciler.ElementChang edListener.canIgnore(IJavaElementDelta[])
for the filtering bit.

> I need to mimic this behavior (somehow) on the copy project, so I need
> to get the changes that are not saved (so that I can apply them to the
> copy). As far as I can see IElementChangedListener can do this, however
> as you also suggested, the events generated by this listener is just
> huge. So if it does the trick I really do need a way to filter some of
> the related events I am interested about (I am not sure if it really
> does the job).
>
> I hope the question is more clear now, please ask if any part does not
> make sense.
>
> Thanks a lot again, regards,
Re: Getting non-saved changes using IResourceChangeListener [message #649655 is a reply to message #649601] Thu, 20 January 2011 06:49 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
On 19.01.2011 22:09, Kivanc Muslu wrote:
> Hi all,
>
> I need to get all files modified (added, changed or deleted) in a
> project immediately after the change is done. For this, I am now using
> a custom implementation of IResourceChangeListener, which works pretty
> good in general.
>
> I can get the DELETED and ADDED events as soon as they are performed
> on the project I am interested in, however I cannot get the CHANGED
> events immediately. I only get them (as a bunch of events some times)
> when user saves the related file(s).
> Is there a way to get these events before the user saves the file?
See org.eclipse.core.filebuffers.IFileBufferListener.

Dani
> If no, is there another way to do what I want to do (i.e., get events
> as soon as they are done)?
>
> Thanks in advance, regards,
Re: Getting non-saved changes using IResourceChangeListener [message #649658 is a reply to message #649650] Thu, 20 January 2011 07:15 Go to previous messageGo to next message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
Hi Deepak,

Thanks for the explanation. Sorry I am not very familiar with the term 'reconciling'. Can you explain that part a little?

Hi Dani,

Thank you for your suggestion. Could you please show me an example code in how I can register a IFileBufferListener to my plug-in? (no need to implement the inside of the IFileBufferListener)

Thanks all!
Re: Getting non-saved changes using IResourceChangeListener [message #649662 is a reply to message #649658] Thu, 20 January 2011 07:31 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
On 20.01.2011 08:15, Kivanc Muslu wrote:
> Hi Deepak,
>
> Thanks for the explanation. Sorry I am not very familiar with the term
> 'reconciling'. Can you explain that part a little?
>
> Hi Dani,
>
> Thank you for your suggestion. Could you please show me an example
> code in how I can register a IFileBufferListener to my plug-in? (no
> need to implement the inside of the IFileBufferListener)
You can do a little bit of Javadoc reading yourself ;-). Besides that
you can take a look at how it's used inside the SDK.

Dani
>
> Thanks all!
Re: Getting non-saved changes using IResourceChangeListener [message #651017 is a reply to message #649662] Thu, 27 January 2011 13:16 Go to previous messageGo to next message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
Hi Dani,

Thanks a lot for the suggestion it works excellent. I have some sub-question:

1- I am getting a lot of events (almost one for each keystroke), is this normal? I guess it is...
2- I had to add a new IBufferChangedListener for each .java file in the project. I tried adding one listener to the IJavaProject's buffer, however it returned null, so didn't work. Is this the way I should do (i.e., use one listener for each files in the project)? Or is it possible to use IBufferChangedListener in project level (i.e., use one listener to get every buffer change inside the project files)?
3- If I apply refactoring to the original project (that I am listening), applying deltas (i.e., particular events) to the other project does not seem to work. Is there an exception (with respect to buffers) for refactoring? Does refactoring delete and re-create the file?

The best part of IBufferChangedListener is that I can get the exact difference between two files and only apply that to the second file (which is I think better than copying the whole file every time).

Thanks in advance, regards,

~ Kivanc
Re: Getting non-saved changes using IResourceChangeListener [message #651197 is a reply to message #651017] Fri, 28 January 2011 10:35 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
On 27.01.2011 14:16, Kivanc Muslu wrote:
> Hi Dani,
>
> Thanks a lot for the suggestion it works excellent. I have some
> sub-question:
>
> 1- I am getting a lot of events (almost one for each keystroke), is
> this normal? I guess it is...
Yes.
> 2- I had to add a new IBufferChangedListener for each .java file in
> the project. I tried adding one listener to the IJavaProject's buffer,
> however it returned null, so didn't work. Is this the way I should do
> (i.e., use one listener for each files in the project)? Or is it
> possible to use IBufferChangedListener in project level (i.e., use one
> listener to get every buffer change inside the project files)?
No.
> 3- If I apply refactoring to the original project (that I am
> listening), applying deltas (i.e., particular events) to the other
> project does not seem to work. Is there an exception (with respect to
> buffers) for refactoring? Does refactoring delete and re-create the file?
Not sure what you mean.

Dani
>
> The best part of IBufferChangedListener is that I can get the exact
> difference between two files and only apply that to the second file
> (which is I think better than copying the whole file every time).
>
> Thanks in advance, regards,
> ~ Kivanc
Re: Getting non-saved changes using IResourceChangeListener [message #651201 is a reply to message #651197] Fri, 28 January 2011 10:55 Go to previous messageGo to next message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
Hi Dani,

What I am trying to say is the following:

So my main goal is to sync to files always. When I apply the first file a refactoring (from Eclipse menu), then IBufferChangeListener gets tons of events related to this refactoring (which might be true), however after I apply all of these events, it throws an Exception (I might provide you what it is, if it would help) and change both files to an old stage. Since the only sync problem I had was during refactoring, I wondered if there is something special about it (that it messes up with the buffer events). To me it seemed that after the refactoring is calculated, the original file is deleted, then recreated and the content (which was calculated as the result of the refactoring) is written into it. I know that this is quite weird explanation, however do you know if it is true, or refactoring is actually a sophisticated text edit?

Thank you,
Re: Getting non-saved changes using IResourceChangeListener [message #651323 is a reply to message #651197] Sat, 29 January 2011 02:40 Go to previous message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
Hi Dani,

Thank you for your replied.
Here is the exception I get when trying to monitor (and sync) refactoring.

!ENTRY org.eclipse.jdt.ui 4 10001 2011-01-28 18:36:26.028
!MESSAGE Internal Error
!STACK 1
Java Model Exception: Java Model Status [highlyTemp [in RemoveMethodParameter [in [Working copy] RemoveMethodParameter.java [in <default> [in src [in Quickfixes]]]]] does not exist]
	at org.eclipse.jdt.internal.core.JavaElement.newNotPresentException(JavaElement.java:492)
	at org.eclipse.jdt.internal.core.JavaElement.openWhenClosed(JavaElement.java:526)
	at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(JavaElement.java:252)
	at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(JavaElement.java:238)
	at org.eclipse.jdt.internal.core.Member.getFlags(Member.java:164)
	at org.eclipse.jdt.internal.core.SourceField.isEnumConstant(SourceField.java:141)
	at org.eclipse.jdt.internal.ui.refactoring.reorg.RenameLinkedMode.createRenameDescriptor(RenameLinkedMode.java:505)
	at org.eclipse.jdt.internal.ui.refactoring.reorg.RenameLinkedMode.undoAndCreateRenameSupport(RenameLinkedMode.java:444)
	at org.eclipse.jdt.internal.ui.refactoring.reorg.RenameLinkedMode.doRename(RenameLinkedMode.java:352)
	at org.eclipse.jdt.internal.ui.refactoring.reorg.RenameLinkedMode$EditorSynchronizer.left(RenameLinkedMode.java:119)
	at org.eclipse.jface.text.link.LinkedModeModel.exit(LinkedModeModel.java:341)
	at org.eclipse.jface.text.link.LinkedModeUI$4.run(LinkedModeUI.java:1194)
	at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
	at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:134)
	at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3586)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3279)
	at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
	at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
	at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
	at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
	at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1407)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1383)
!SUBENTRY 1 org.eclipse.jdt.core 4 969 2011-01-28 18:36:26.031


Does this explain my problem better? Regards,
Previous Topic:Use Eclipse to run Java web start application
Next Topic:[SOLVED] How to open an IJavaElement programatically?
Goto Forum:
  


Current Time: Thu Mar 28 23:00:25 GMT 2024

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

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

Back to the top