Bug 151604 - [Contributions] Enablement not updated when menu shown, only when selection changes
Summary: [Contributions] Enablement not updated when menu shown, only when selection c...
Status: CLOSED WONTFIX
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Platform UI Triaged CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2006-07-24 12:27 EDT by Nick Edgar CLA
Modified: 2021-09-20 15:47 EDT (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nick Edgar CLA 2006-07-24 12:27:06 EDT
3.2 RC5

If the enablement logic of a contributed action (object contribution or viewer contribution) depends on some state of the selected object, and some of the actions can affect that state, then it's possible to get incorrect enablement showing in the context menu if the user pops up the menu twice without the selection changing in between.

For example, imagine that the Close Project action was contributed declaratively:
- select an open project
- pop up the menu
- Close Project is enabled
- select Close Project
- the project gets closed
- pop up the menu again
- Close Project is still enabled, even though the logic says it should be disabled

Recommendation:
- call selectionChanged on the PluginActions when the menu is shown, not just when the selection changes.

        getSite().registerContextMenu(viewer, contextMenuManager);
        // after registering the menu manager, add a menu listener 
        // that will go through an force the enablement of contributed 
        // actions to be updated
        contextMenuManager.addMenuListener(new IMenuListener() {
            public void menuAboutToShow(IMenuManager manager) {
                ISelection selection = viewer.getSelection();
                IContributionItem[] items = manager.getItems();
                for (int i = 0; i < items.length; i++) {
                    IContributionItem item = items[i];
                    if (item instanceof ActionContributionItem) {
                        IAction action = ((ActionContributionItem) item).getAction();
                        if (action instanceof PluginAction) {
                            ((PluginAction) action).selectionChanged(selection);
                        }
                    }
                }
            }
Comment 1 Nick Edgar CLA 2006-07-24 12:30:39 EDT
This is another example where computing enablement on-demand (rather than requiring selection and other kinds of listeners to be hooked for any state that can affect enablement) would be simpler.
That's effectively what this workaround achieves, but unfortunately it requires an internal reference to PluginAction.
Comment 2 Nick Edgar CLA 2006-07-24 12:51:44 EDT
Note to self: corresponding Jazz bug is #7454.
Comment 3 Nick Edgar CLA 2006-08-28 12:21:10 EDT
In 3.2, it appears this is only an issue for viewer contributions.  Object contributions are added each time the menu is shown, and their selection is updated.
Comment 4 Nick Edgar CLA 2006-08-28 12:27:15 EDT
The suggested workaround is no good.  It can cause problems for object contributions that use adapters.  Normally the workbench passes the converted selection, but the workaround does not.

A better workaround is:
        getSite().registerContextMenu(viewer, contextMenuManager);
        // after registering the menu manager, add a menu listener 
        // that will fire a selection changed event, in order
        // to update the selection in contributed actions
        contextMenuManager.addMenuListener(new IMenuListener() {
           public void menuAboutToShow(IMenuManager manager) {
              viewer.setSelection(viewer.getSelection());
           }
Comment 5 Markus Keller CLA 2007-03-01 10:09:53 EST
See also bug 48326.
Comment 6 Paul Webster CLA 2007-04-05 19:05:20 EDT
Assigning to component owner
PW
Comment 7 Paul Webster CLA 2008-09-22 10:37:55 EDT
I'll take a look at this in M3 (hopefully, or M4)

PW
Comment 8 Paul Webster CLA 2009-01-19 12:40:00 EST
I guess M6 it is :-)
PW
Comment 9 Eclipse Webmaster CLA 2019-09-06 16:09:59 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.
Comment 10 Eclipse Genie CLA 2021-09-20 15:47:39 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.