Bug 466278 - Annotate command should be disabled in context menu when not applicable
Summary: Annotate command should be disabled in context menu when not applicable
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.5   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 4.5 RC1   Edit
Assignee: Stephan Herrmann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-05-04 05:58 EDT by Noopur Gupta CLA
Modified: 2015-05-19 07:48 EDT (History)
2 users (show)

See Also:
stephan.herrmann: review+


Attachments
Screenshot (16.58 KB, image/png)
2015-05-04 05:58 EDT, Noopur Gupta CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Noopur Gupta CLA 2015-05-04 05:58:24 EDT
Created attachment 253129 [details]
Screenshot

Eclipse 4.5 M7 - I20150430-1445

- Create a Java 8 project, open java.util.Map and got to the method #get.
- Place caret before the return type "V" and right-click to get the context menu.
- "Annotate" command is enabled and selecting that does not produce any result.
It should be disabled like the "Copy" command shown in screenshot when it is not applicable.
Comment 1 Eclipse Genie CLA 2015-05-04 16:27:12 EDT
New Gerrit change created: https://git.eclipse.org/r/47094
Comment 2 Stephan Herrmann CLA 2015-05-04 16:29:05 EDT
Thanks for the reference to the "Copy" command, which helped me identify the code location where the fix should go :)
Comment 3 Stephan Herrmann CLA 2015-05-04 19:49:21 EDT
Mh, two consecutive gerrit/hudson runs (using the exact same commit) produced two disjoint sets of regressions.

(#243): 9 failures in LeakTestSuite
(#244): 3 failures in CodeCompletionTest

I doubt that either set of failures could have been caused by my change.
Comment 4 Stephan Herrmann CLA 2015-05-05 18:12:43 EDT
This is getting weird: during 4 build runs, hudson strictly alternated between failing in LeakTestSuite and failing in CodeCompletionTest. The failure in CodeCompletionTest I cannot reproduce at all. Running LeakTestSuite locally I did observe one failure: testSearchResultEditorClose(). This I got twice actually. But never the 9 failures seen on hudson.

Noopur, do you have a suggestion how to take this simple fix forward?
Comment 5 Stephan Herrmann CLA 2015-05-05 18:58:18 EDT
Fifth build against the same change succeeded ... go figure ...
Comment 6 Markus Keller CLA 2015-05-06 13:26:23 EDT
Sorry for the random Gerrit failures. This is bug 423416, which we haven't been able to track down.

Regarding this bug: The "Annotate" command should not show up in the context menu of editors in which it's not available (i.e. it should not just be disabled).

There are two reasons why "Copy" is different and is sometimes shown disabled:
- it's a very special command that everybody expects to be there all the time
- its availability depends on whether there's a selection or not
Comment 7 Stephan Herrmann CLA 2015-05-06 20:40:58 EDT
(In reply to Markus Keller from comment #6)
> Regarding this bug: The "Annotate" command should not show up in the context
> menu of editors in which it's not available (i.e. it should not just be
> disabled).

Sure, patch set #3 implements the following 2-stage strategy:

Create and show action only when relevant per this editor:
- do not create action during createActions()
- create action in verifyInput() if 
  - we have source, and
  - null annotations are enabled
  account for editor re-use

Disable action during editorContextMenuAboutToShow() if selection is not a type. This is determined based on the selected IJavaElement (we don't want to compute AST during this method).
Unfortunately, wildcards and arrays don't have an IJavaElement, so we must keep the action enabled, when no IJavaElement is selected (could perhaps improve in 4.6 by further heuristics).
Comment 8 Stephan Herrmann CLA 2015-05-07 10:11:38 EDT
Markus, do you agree to the updated implementation (see comment 7) plus inclusion in RC1?
Comment 9 Markus Keller CLA 2015-05-07 16:48:20 EDT
+1 to fix in RC1. -1 for patch set 3.

The enabling/disabling of the action in editorContextMenuAboutToShow(..) can't work:
- open the menu with a selection for which the action gets disabled
- select a method return type
=> action still disabled and Ctrl+1 doesn't work; need to open menu to re-enable

Furthermore, editor actions can't call codeSelect(..). Multiple such calls would be too expensive. If necessary, the action would have to become a SelectionDispatchAction and make use of the JavaTextSelection etc. But that's too big a change for RC1.

Let's just hide the action from the menu when it's disabled, see patch set 4 (based on your patch set 2). OK to release if you agree.
Comment 10 Stephan Herrmann CLA 2015-05-07 17:35:37 EDT
Thanks for the review.

(In reply to Markus Keller from comment #9)
> +1 to fix in RC1. -1 for patch set 3.
> 
> The enabling/disabling of the action in editorContextMenuAboutToShow(..)
> can't work:
> - open the menu with a selection for which the action gets disabled
> - select a method return type
> => action still disabled and Ctrl+1 doesn't work; need to open menu to
> re-enable

I see, I missed the interaction between menu invocation and Ctrl-1.

> Furthermore, editor actions can't call codeSelect(..). Multiple such calls
> would be too expensive. If necessary, the action would have to become a
> SelectionDispatchAction and make use of the JavaTextSelection etc. But
> that's too big a change for RC1.

I saw JavaTextSelection but wasn't 100% sure about its purpose. Are you saying this mechanism is used for sharing the result of codeSelect among different actions? Sounds cool.

> Let's just hide the action from the menu when it's disabled, see patch set 4
> (based on your patch set 2). OK to release if you agree.

Sure, thanks for preparing the change. Clean and simple.
Comment 12 Stephan Herrmann CLA 2015-05-07 17:38:07 EDT
.
Comment 13 Noopur Gupta CLA 2015-05-08 04:22:20 EDT
Presence of Ctrl+1 to annotate and right-click > Annotate are not always in sync now. For example, create a Java 8 project and enable annotation-based null analysis. Open Map#get => Ctrl+1 not present, but Annotate is present in context menu. Now, add the location for external annotations in project's JRE - both ways to annotate are present.

Also, noticed that if Map#get is already open and then annotation-based null analysis is enabled and location for external annotations is specified on project, then both ways to Annotate don't work. We have to close and reopen the Map editor to make them work.

Another issue related to bug 466299 is that if I have 2 projects in workspace and I enable annotation-based null analysis on only one project and provide location for external annotations, I don't get the Annotate command in any way at Map#get.

And observed that for a project, if two different locations are provided at the JRE and at rt.jar, then the one at JRE is used for .eea file when a type in Map#get is annotated.

Only the first issue is related to this bug.
Comment 14 Stephan Herrmann CLA 2015-05-08 13:07:41 EDT
(In reply to Noopur Gupta from comment #13)
> Presence of Ctrl+1 to annotate and right-click > Annotate are not always in
> sync now. For example, create a Java 8 project and enable annotation-based
> null analysis. Open Map#get => Ctrl+1 not present, but Annotate is present
> in context menu. Now, add the location for external annotations in project's
> JRE - both ways to annotate are present.

What do you mean by "Ctrl+1 not present"?

In this situation (null annotations enabled, but no external annotation location) the coarse grain checking signals "enable", which results in the menu action being shown.

When attempting to perform the action (menu or key) we detect that we cannot create the annotation file (due to missing location) and the action silently aborts.

Does this correspond to your observations?


I'll respond to the other issues in bug 466299.
Comment 15 Stephan Herrmann CLA 2015-05-09 12:21:05 EDT
(In reply to Noopur Gupta from comment #13)
> Also, noticed that if Map#get is already open and then annotation-based null
> analysis is enabled and location for external annotations is specified on
> project, then both ways to Annotate don't work. We have to close and reopen
> the Map editor to make them work.

Right, enablement is currently determined when setting the editor input.

Should we re-check enablement during editorContextMenuAboutToShow()?
This is a cheap operation.

As for the key binding I'm not sure what would be the best hooks for re-checking? Perhaps by overriding isEnabled() right in the action? Of course a dynamic impl. of isEnabled() would address both issues. Would it then be safe to call setEnabled() from within isEnabled() (would that be needed)?  I'm slightly afraid of triggering too much business going on inside firePropertyChange()... 

I don't see this issue as a must-fix for 4.5, though. Do you?

> And observed that for a project, if two different locations are provided at
> the JRE and at rt.jar, then the one at JRE is used for .eea file when a type
> in Map#get is annotated.

Topic for documentation: specify priority of these entries.
Comment 16 Noopur Gupta CLA 2015-05-11 09:14:25 EDT
(In reply to Stephan Herrmann from comment #14)
> Does this correspond to your observations?
Yes, looks correct.

(In reply to Stephan Herrmann from comment #15)
> I don't see this issue as a must-fix for 4.5, though. Do you?
Not an important one to be fixed right now.
Comment 17 Stephan Herrmann CLA 2015-05-12 11:27:02 EDT
I've filed bug 467123 to follow up on loose ends during the 4.6 cycle.
Comment 18 Noopur Gupta CLA 2015-05-19 07:48:04 EDT
Verified as fixed with Eclipse 4.5 RC1 I20150514-1000.