Bug 17102 - [EditorMgmt] User gets prompted to save the same resource twice
Summary: [EditorMgmt] User gets prompted to save the same resource twice
Status: RESOLVED DUPLICATE of bug 131068
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 2.0   Edit
Hardware: All All
: P2 major (vote)
Target Milestone: 3.3   Edit
Assignee: Boris Bokowski CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 64742 94297 (view as bug list)
Depends on:
Blocks:
 
Reported: 2002-05-22 17:40 EDT by Randy Hudson CLA
Modified: 2006-12-18 14:15 EST (History)
5 users (show)

See Also:


Attachments
Fix to SaveablesList reference counting (1.35 KB, patch)
2006-11-08 19:04 EST, Jon Burton CLA
no flags Details | Diff
Update with additional change to DefaultSaveable (3.10 KB, patch)
2006-11-09 23:38 EST, Jon Burton CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Randy Hudson CLA 2002-05-22 17:40:40 EDT
On 0519 build, I was performing a "check out as project" while I had a dirty 
JUnit test case in 2 different Workbench Windows.  I was prompted to save the 
same resource twice.
Comment 1 Nick Edgar CLA 2002-05-27 21:21:49 EDT
Not sure how CVS support does this.  If it just calls the workbench save, 
reassign back to UI.
Comment 2 Randy Hudson CLA 2002-06-07 16:34:14 EDT
F2 - Also happens when performing Synchronize.
Comment 3 James Moody CLA 2002-06-11 13:43:40 EDT
See CVSAction.saveAllEditors().
We loop over all open workbench windows, then loop over all workbench pages in
the windows, calling the Workbench saveAllEditors(boolean) API.

Is there a better recommended way to accomplish this? We have no idea what's in
the editors, hence we don't know if there is a duplicate. Moving back to UI for
comment.
Comment 4 Randy Hudson CLA 2002-06-19 15:54:09 EDT
This also happens during compile.

While it is true that the Editor may open more than one file, can the workbench 
assume that two editors with the same EditorInput therefore operate on the same 
set of resource?  Or perhaps validateEdit can be used to track exactly which 
resources the Editor is working with.

Hope you don't mind Nick, but I changed this to LATER.
Comment 5 Randy Giffen CLA 2002-08-12 10:34:49 EDT
Reopened for investigation
Comment 6 Douglas Pollock CLA 2004-07-13 16:59:28 EDT
*** Bug 64742 has been marked as a duplicate of this bug. ***
Comment 7 Douglas Pollock CLA 2004-07-13 17:01:46 EDT
This also occurs when trying to replace an editor with the latest from HEAD, 
for example.  The editor appears twice in the dirty list. 
 
There seems to be a general problem here with collecting dirty editors from 
multiple windows without removing duplicates. 
Comment 8 Douglas Pollock CLA 2005-05-11 09:35:29 EDT
*** Bug 94297 has been marked as a duplicate of this bug. ***
Comment 9 Michael Van Meekeren CLA 2006-04-21 13:19:43 EDT
Moving Dougs bugs
Comment 10 Jon Burton CLA 2006-11-08 19:04:29 EST
Created attachment 53522 [details]
Fix to SaveablesList reference counting

In 3.2, you're prompted to save the same resource multiple times if there are multiple editors referencing it. There seem to be a couple of causes underlying this.

Firstly, SaveablesList maintains a reference count of open Saveables, but gets the count wrong if the Saveables are equal (ie when saveable1.equals(saveable2) you would expect the reference count to go to 2 when 2 editors are open, but it only goes to 1. When both editors are closed the count goes to -1, etc). 

Secondly, this scheme would only work if the Saveables are equal when the underlying resource is the same, which isn't generally the case for DefaultSaveable as it just delegates .equals() to the EditorPart.

Here's a very small patch which attempts to fix the reference count problem. I'm not sure of the best way to make Saveables equal, but for my RCP application I've just overridden equals and hashCode in the editor. Probably not the best way to do it though.
Comment 11 Kim Horne CLA 2006-11-09 12:42:52 EST
The patch looks reasonable but this area is very sensitive and I've never worked in it before.  Deferring to Boris
Comment 12 Jon Burton CLA 2006-11-09 23:38:36 EST
Created attachment 53603 [details]
Update with additional change to DefaultSaveable

I've thought about this some more and think overriding equals in the Editor was a really bad idea. 

This patch combines my first patch with another possible approach. It adds a check for IEditorPart in the DefaultSaveable and seems to partially fix the issue, at least for the standard Java editors. 

That said, this might not be an approach you would want to take, and the behavior when closing a WorkbenchWindow if two are open still isn't what I would expect. (That case is handled by code in EditorManager.java)
Comment 13 Boris Bokowski CLA 2006-11-10 08:19:23 EST
Thanks for your help.  There is an open bug against JDT for the double prompting, see bug 131068.

The reference counting bug should probably be fixed for 3.2.2.
Comment 14 Markus Keller CLA 2006-11-20 10:05:25 EST
Also see clients of IWorkbenchPage.getDirtyEditors(). Some of them already prune duplicate editor inputs, e.g. compare's CompareUIPlugin.getDirtyEditors() or jdt.ui's EditorUtility.getDirtyEditors().

Others however show all editors for equal inputs, e.g. ui.ide's IDE.saveAllEditors(IResource[], boolean) or debug.ui's SaveScopeResourcesHandler.getScopedDirtyEditors(IProject[])).
Comment 15 Boris Bokowski CLA 2006-11-28 15:12:41 EST
(In reply to comment #10)
> ...
> Firstly, SaveablesList maintains a reference count of open Saveables, but gets
> the count wrong if the Saveables are equal (ie when saveable1.equals(saveable2)
> you would expect the reference count to go to 2 when 2 editors are open, but it
> only goes to 1. When both editors are closed the count goes to -1, etc). 

Jon, did you actually observe this, or is this based on reading the code?  I just checked the code and don't see how the count can go to -1.
Comment 16 Jon Burton CLA 2006-11-28 19:18:37 EST
(In reply to comment #15)
> (In reply to comment #10)
> > ...
> > Firstly, SaveablesList maintains a reference count of open Saveables, but gets
> > the count wrong if the Saveables are equal (ie when saveable1.equals(saveable2)
> > you would expect the reference count to go to 2 when 2 editors are open, but it
> > only goes to 1. When both editors are closed the count goes to -1, etc). 
> 
> Jon, did you actually observe this, or is this based on reading the code?  I
> just checked the code and don't see how the count can go to -1.
> 

(In reply to comment #15)
> (In reply to comment #10)
> > ...
> > Firstly, SaveablesList maintains a reference count of open Saveables, but gets
> > the count wrong if the Saveables are equal (ie when saveable1.equals(saveable2)
> > you would expect the reference count to go to 2 when 2 editors are open, but it
> > only goes to 1. When both editors are closed the count goes to -1, etc). 
> 
> Jon, did you actually observe this, or is this based on reading the code?  I
> just checked the code and don't see how the count can go to -1.
> 

Boris, I did see it go to -1, but looking at it again, I think that this was caused by my code (the EditorInput was changing) and that I misunderstood the intention behind the modelsForSource. I think you're right, and the code as it stands is fine. Sorry if I've wasted your time on this.

Jon
Comment 17 Boris Bokowski CLA 2006-11-29 00:36:22 EST
(In reply to comment #16)
> Sorry if I've wasted your time on this.

No problem at all, it is good to have people like you who look at the code and point to potential problems.
Comment 18 Boris Bokowski CLA 2006-12-18 14:15:05 EST

*** This bug has been marked as a duplicate of bug 131068 ***