Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 167607

Summary: [EditorMgmt] Closing compare editor asks to save editor - Saveable should implement IAdaptable
Product: [Eclipse Project] Platform Reporter: Martin Aeschlimann <martinae>
Component: CompareAssignee: Michael Valenta <Michael.Valenta>
Status: VERIFIED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: bokowski, daniel_megert, Michael.Valenta, snorthov
Version: 3.2   
Target Milestone: 3.3 M5   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Bug Depends on: 131068    
Bug Blocks:    
Attachments:
Description Flags
Patch that implements equals and hashCode on Compare's Saveable
none
Updated patch none

Description Martin Aeschlimann CLA 2006-12-12 07:14:16 EST
20061212-0010

1. Open a file that has some entries in the local history
2. Make some modifications to the file but don't save the editor
3. Invoke 'Compare With > Local History'
4. The history view opens. Open some of the entries: Compare editors are shown
5. Close these compare editors again
  > You are prompted to save the file

That shouldn't be needed here. Even if we made changes in the compare editor, these changes should work on the primary file buffer and don't need to be saved as long as it isn't the last editor that is open on it.
Comment 1 Michael Valenta CLA 2006-12-12 12:17:20 EST
The Compare editor is just behaving like any other editor. That is, if I open a Java editor and a Text editor, dirty one and then close one, I get prompted to save the editor. Moving to Platform/Text for comment.
Comment 2 Dani Megert CLA 2006-12-12 12:39:14 EST
The problem regarding textual editors (i.e. inheriting from AbstractTextEditor) is covered by bug 131068 and planned to be resolved for 3.3 via ISavablesSource.

Since the compare and the textual editors won't use the same savables the problem will persist. A solution might be that text makes its savable class public but I didn't yet look to close at that ISavablesSource to tell whether that will work out.
Comment 3 Dani Megert CLA 2006-12-14 13:12:42 EST
Michael and I discussed this today and we found a common ground which allows us to test our two Saveables for equality. To achieve this we need the Saveables to be adaptable (i.e. inherit from IAdaptable).

Just discussed this idea with Boris and hence moving to him to implement this ;-)
Comment 4 Boris Bokowski CLA 2006-12-19 01:37:37 EST
Saveable now implements IAdaptable. This is in I20061219-0800.

However, when implementing equals() and hashCode() based on this, keep the following in mind (copied from Saveable.equals()):
* Two
* saveables should be equal if their dirty state is shared, and saving one
* will save the other. If two saveables are equal, their names, tooltips,
* and images should be the same because only one of them will be shown when
* prompting the user to save.
Comment 5 Dani Megert CLA 2006-12-19 03:28:36 EST
Reopening to do the remaining work.
Comment 6 Dani Megert CLA 2006-12-19 11:49:03 EST
Fixed the TextEditorSavable to use the editor input in equals:
	...
	editorInput=(IEditorInput)((Saveable)obj).getAdapter(IEditorInput.class);
	return fEditorInput.equals(editorInput);

Availabel in builds > N20061219-0010 (excl. this weeks I-build).

Moving to Compare for the last bit.
Comment 7 Boris Bokowski CLA 2006-12-19 12:18:16 EST
I am a little worried that just using the editor input might be unsafe.  Depending on whether the other Saveable uses the same underlying buffer or not is what should be used in equals.
Comment 8 Dani Megert CLA 2006-12-20 05:06:38 EST
Good catch. Fixed by using the document and returning the shared document in the adapter (IDocument.class).
Comment 9 Michael Valenta CLA 2007-01-04 13:40:58 EST
Created attachment 56409 [details]
Patch that implements equals and hashCode on Compare's Saveable
Comment 10 Michael Valenta CLA 2007-01-04 13:51:07 EST
I've attempted to implement this in Compare but I am getting some bad behavior from the Workbench. I can easily get into a state where I have a single dirty Compare editor open but when I close it, I get prompted saying that the model is referenced else where. Through debugging, I determined that the reference counts are not being decremented properly but I haven't been able to pinpoint the cause. This may come from my implementation of hashCode which is technically not correct because it returns a different values when it doesn't have access to the document. 

Regardless of what the problem is, implementing this has made me think that using equals to make two different Saveables equal is a mistake. I think we need to come up with another way to do this. One possibility would be to have a method of Saveable for doing the test. We could then have a convention at the editor level that subclasses of Saveable should adapt the Saveable being compared to the underlying document. Thoughts?
Comment 11 Dani Megert CLA 2007-01-05 05:04:15 EST
If we do this I guess we have to make it backwards compatible since other now make use of the equals/hashCode approach.
Comment 12 Michael Valenta CLA 2007-01-05 10:00:16 EST
The problem is indeed my implementation of equals/hashCode. The problem is that, at the time the saveable is created (and used by the workbench), I don't know whether the merge viewer will use the file buffer or not. This means that I should delay the creation of the Saveable until I know whether the viewer uses the buffer or not (or change the saveable from one that doesn't to one that does once I know the buffer will be used). This may be complicated since I am going through 3 or 4 levels of indirection but I'll have a look.

Dani, is there any possibility that the document for the text editor will ever change? For example, if the file changes in the file system, does the editor obtain another document? If so, this may cause problems since the hashCode of the TextEditorSaveable uses the document and hashCodes should never change.
Comment 13 Michael Valenta CLA 2007-01-05 13:11:56 EST
Created attachment 56473 [details]
Updated patch

This updated patch now works since it implements hashCode and equals in a non-changing way. However, I found a bug in the TextEditorSaveable which causes the wrong prompt to appear so I don;t want to release this until the bug is fixed. The bug number is bug 169707.
Comment 14 Michael Valenta CLA 2007-01-08 10:07:45 EST
Fix released to HEAD. Should work for both the model-based and non-model-based Synchronizations.
Comment 15 Michael Valenta CLA 2007-02-06 09:23:24 EST
Verified using I20070206-0010