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

Bug 394336

Summary: File->Close menu item is disabled even when current editor is activated
Product: [Eclipse Project] Platform Reporter: Keith Chong <keith.chong.ca>
Component: UIAssignee: Paul Elder <pelder.eclipse>
Status: VERIFIED FIXED QA Contact:
Severity: major    
Priority: P3 CC: bsd, daniel_megert, euthanasia_waltz, jpitman, pelder.eclipse, pwebster, xucxuc, yiming.sun
Version: 4.2.1Keywords: helpwanted
Target Milestone: 4.3 M7   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Bug Depends on:    
Bug Blocks: 407085    
Attachments:
Description Flags
Proposed fix none

Description Keith Chong CLA 2012-11-14 16:36:28 EST
Using Eclipse 4.2.1.

Stpes to reproduce
1. Create a project and import some files (XML, HTML, etc.)
2. Open a file using the default editor
3. Open the File menu and notice that the Close menu action is disabled
4. If you can't reproduce right away, repeat steps 2 and 3 until you see the problem.
Comment 1 Brian de Alwis CLA 2012-11-23 11:20:33 EST
I'm unable to reproduce on MacOS X and Linux using the latest milestone build towards 4.2.2.  

1. I created a Java project and then imported 10 different XML files.
2. I opened each file using Open With > Default Editor.  The Plug-in Spy (Shift-Alt-F1) reports the editor is a XMLMultiPageEditorPart from org.eclipse.wst.xml.ui 1.1.301.v201208170345 (editor id: 
org.eclipse.wst.xml.ui.internal.tabletree.XMLMultiPageEditorPart)
3. File > Close remains enabled.

Could you try the Plug-in Spy and report back what editor type these files are opening with?
Comment 2 John Pitman CLA 2012-12-05 16:30:20 EST
I'm seeing this problem on both Windows and Mac OS X for other kinds of files as well.  I've tried HTML and XML files with WTP installed, and Java and plain text files in an unmodified Eclipse SDK install, and they all have this problem.

I used the M20121128-1200 4.2.2 build.

1. Created a Java project
2. Created a Java class.  It automatically opened in the Java Editor.
   File > Close was enabled
3. Closed the Java file
4. Double-clicked to open the Java file.  File > Close is now disabled.
Comment 3 Paul Webster CLA 2012-12-10 11:25:28 EST
I can reproduce this on linux in 4.3 as well.

PW
Comment 4 Paul Webster CLA 2013-01-09 14:33:58 EST
*** Bug 397787 has been marked as a duplicate of this bug. ***
Comment 5 Brian de Alwis CLA 2013-01-22 15:19:48 EST
The org.eclipse.ui.file.close handler has the following enabledWhen expression:

   <with
         variable="activeEditor">
      <instanceof
         value="org.eclipse.ui.IEditorPart">
      </instanceof>
   </with>

I suspect the issue arises from an ordering issue with how the contexts propagate changes to the active variables.  Changing the active editor changes causing WorkbenchPage.updateActiveEditorSources() to update the window context's activeEditor.  In tracing through the execution, the EvaluationService evaluates the enableWhen in the Workbench's context while the corresponding HandlerProxy#setEnabled() evaluates the same expression relative to the active-part's context *before* the active variant of the activeEditor variable has been been propagated to the active-part's context.  So the expression evaluates to false for HandlerProxy and thus the menu item is disabled.
Comment 6 Paul Elder CLA 2013-01-22 16:38:32 EST
I am seeing the same as comment 5. I've gone a little further and taken a look at the EvaluationService, and how it retrieves 'activeEditor' from the context. Ultimately, it calls EclipseContext.getActive("activeEditor"). I did a couple of investigations of getActive():

1) The Javadoc says it is the equivalent of getActiveLeaf().get(name), so I created a debug version of getActive() that compares that getActiveLeaf().get() with the value calculated by getActive().
2) I instrumented the two calculations, to compare execution times, and to record any inconsistencies between the two calculation methods.

A typical result:

getActive() calls: 15680, 22ms (optimized), 15ms (unoptimized)
getActive() inconsistencies: 782, names: [activeWorkbenchWindow.activePerspective, org.eclipse.ui.selection, activeEditor, activePart, activeWorkbenchWindow, activeContexts, selection, activeEditorId]

Several observations on the results:
1) There are a significant number of times when the two methods of calculation do not yield the same result, including some calls looking for 'activeEditor'.
2) If my debug version uses the getActiveLeaf.get() result (instead of its calculated result), then the Close menu enables correctly.
3) There is no apparent performance advantage to the current getActive() implementation compared to getActiveLeaf().get(). In fact, the latter may be slightly faster. (The execution times are the total for all calls and are measured via System.nanoTime(), but converted to milliseconds so I can actually read them).
Comment 7 Paul Webster CLA 2013-01-23 13:16:25 EST
Paul, can you reproduce with the patch from Bug 394171?   attachment 225996 [details] 

PW
Comment 8 Paul Elder CLA 2013-01-23 13:58:45 EST
Paul W: No, the patch does not fix this issue.
Comment 9 Paul Elder CLA 2013-02-11 11:23:23 EST
Created attachment 226842 [details]
Proposed fix

The attached patch simplifies EclipseContext.getActive() to do what it's Javadoc comment says: getActiveLeaf().get(). Profiling indicates that there is no significant performance difference between the current implementation and the patch - in fact, the patch is sometimes faster!
Comment 10 Paul Webster CLA 2013-02-14 13:14:05 EST
(In reply to comment #9)
> Created attachment 226842 [details]
> Proposed fix

But it's no longer a tracked value ... so when the active child changes (say switching parts) or the value in the active child changes, any RATs are not re-run

PW
Comment 11 Paul Webster CLA 2013-02-28 13:58:02 EST
I've pushed the change up to Gerrit for us to review there:
https://git.eclipse.org/r/10749

I'll also note, I get a failure in org.eclipse.e4.core.tests.CoreTestSuite from org.eclipse.e4.core.tests.

PW
Comment 12 Paul Webster CLA 2013-03-12 13:32:15 EDT
*** Bug 400960 has been marked as a duplicate of this bug. ***
Comment 13 Paul Webster CLA 2013-04-15 13:43:41 EDT
I've pushed a suggested fix to https://git.eclipse.org/r/#/c/11912/

It's a new changeset because it is in a different repo than the original fix.  I could reproduce the problem fairly easily and then with this patch I haven't been able to reproduce it since (it still needs some more testing).

Paul, could you test this out in some of your scenarios, see that it still works?

PW
Comment 14 Paul Webster CLA 2013-04-17 13:32:09 EDT
I've released this so it can get some broader testing, but I think it's still worth it  us doing some targetted testing if you could.

PW
Comment 16 Paul Webster CLA 2013-04-18 13:11:00 EDT
.
Comment 17 Dani Megert CLA 2013-04-19 03:53:23 EDT
Paul, this still does not work for me.

1. download http://download.eclipse.org/eclipse/downloads/drops4/N20130418-0900/
2. install it
3. start with new workspace
4. close Welcome
5. paste this into the 'Package Explorer':
class Bug {}
==> File > Close and File > Close All are disabled!
Comment 18 Paul Webster CLA 2013-04-19 06:50:41 EDT
(In reply to comment #17)
> 
> 1. download
> http://download.eclipse.org/eclipse/downloads/drops4/N20130418-0900/
> 2. install it
> 3. start with new workspace
> 4. close Welcome
> 5. paste this into the 'Package Explorer':

I can reproduce.  Closing an editor seems to enable it, and it works from then on.  But, closing the last editor doesn't disable it.

PW
Comment 19 Paul Webster CLA 2013-04-19 09:10:52 EDT
Close All is bug 400960

PW
Comment 20 Paul Webster CLA 2013-04-22 11:14:11 EDT
Seems to be interaction with my fix for bug 382839

Fix: https://git.eclipse.org/r/#/c/12098/

PW
Comment 22 Dani Megert CLA 2013-04-23 03:06:28 EDT
(In reply to comment #21)
> Released as
> http://git.eclipse.org/c/platform/eclipse.platform.ui.git/commit/
> ?id=b976b22fca1d2c3b279d920137e2fcd1beccc927
> 
> PW

Verified that it fixes the scenario from comment 0 and comment 17.

Paul, it also fixes the enabling of 'Close All' for me.
Comment 23 Paul Webster CLA 2013-05-23 09:34:53 EDT
*** Bug 392149 has been marked as a duplicate of this bug. ***