Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 221980 - [Contributions] [Contributions] MenuItems with style radio are not initialized in context menu
Summary: [Contributions] [Contributions] MenuItems with style radio are not initialize...
Status: VERIFIED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.3.1   Edit
Hardware: PC Windows XP
: P3 normal with 3 votes (vote)
Target Milestone: 3.5 M6   Edit
Assignee: Prakash Rangaraj CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 267409
  Show dependency tree
 
Reported: 2008-03-09 09:52 EDT by chris CLA
Modified: 2009-06-03 13:37 EDT (History)
4 users (show)

See Also:


Attachments
Patch 01 for toggle state (11.00 KB, patch)
2009-03-05 05:27 EST, Prakash Rangaraj CLA
pwebster: iplog+
Details | Diff
toggle state v02 (12.39 KB, patch)
2009-03-05 14:24 EST, Paul Webster CLA
no flags Details | Diff
Radio state patch 01 (17.09 KB, patch)
2009-03-06 05:03 EST, Prakash Rangaraj CLA
no flags Details | Diff
Radio State patch v02 (16.56 KB, patch)
2009-03-06 12:00 EST, Prakash Rangaraj CLA
pwebster: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description chris CLA 2008-03-09 09:52:28 EDT
I have a class implementing IHandler and IElementUpdater, which is bound to 2 command entries which are contributed using the extension point org.eclipse.ui.menus. The command entries have radio style and are located within a popup menu. Now, the very first time I right-click an element in my editor, the context menu shows up, but none of the MenuEntries is selected. After clicking one of the entries, the correct entry is selected (correct with respect to my implementation of IElementUpdater.updateElement()), not only for this element, but also for all other elements I right-click later.

I've set a conditional breakpoint in method CommandService.registerElement(IElementReference) and tracked this down to the following:

The method is called when the context menu is created, but at this time, the proxy handler's handler attribute (i.e., the instance of my IHandler) is still null. Therefore, the call to command.getHandler().updateElement(*) does not result in any action - the initialization does not happen.

A simple workaround was to add code to my plug-ins Activator.start(*) method. I create an UIJob which gets my command from the commandservice and then executes it (with no effect, though). This forces the ProxyHandler to instantiate my handler. The context menu items are then correctly initialized.

If anybody knows a more elegant way to "initialize" my Command, please let me know :-)
Comment 1 Tom Seidel CLA 2008-05-25 07:42:26 EDT
(In reply to comment #0)
> If anybody knows a more elegant way to "initialize" my Command, please let me
> know :-)
> 

Asking for the Enabled-State of the command should also work (IMHO better than executing the command), see http://wiki.eclipse.org/Menu_Contributions/Radio_Button_Command#Initializing_the_Handler

In my Usecase I additionally had to define an empty org.eclipse.ui.startup Extension that forces the bundle startup for initializing the handler.
Comment 2 chris CLA 2008-06-05 05:07:50 EDT
(In reply to comment #1)
> > If anybody knows a more elegant way to "initialize" my Command, please let me
> > know :-)
> > 
> 
> Asking for the Enabled-State of the command should also work (IMHO better than
> executing the command), see
> http://wiki.eclipse.org/Menu_Contributions/Radio_Button_Command#Initializing_the_Handler

Thanks for pointing that out - in fact, somebody else (was it Paul?) already gave me the hint to check if the commands are enabled, and I wrote the wiki entry you linked to :-)

I should have provided that link here myself as a reference, though...
Comment 3 Paul Webster CLA 2008-11-25 13:43:45 EST
We need to consider how to integrate checked state or radio state into org.eclipse.ui.menus.

Commands has a partially implemented mechanism that involves subclasses of org.eclipse.core.commands.State which can be used to tell the handler what state a command believe it is in.  It also would save/restore that state to the command over session restarts.

Would it be feasible to initialize CommandContributionItems from that state?  Is it a good idea?  Maybe we could also provide a "PreferenceState" object so that the command state would really be backed by the preference store (I'm not sure how useful this would be).

PW
Comment 4 Prakash Rangaraj CLA 2009-03-05 05:27:13 EST
Created attachment 127633 [details]
Patch 01 for toggle state

Attached is the patch & tests that handles the toggle state.

Still working on the radio state
Comment 5 Paul Webster CLA 2009-03-05 14:24:36 EST
Created attachment 127695 [details]
toggle state v02

Looks good.  I've made some minor mods (added a constant, etc).

What happens when you add a toggle command to the toolbar twice (i.e. 2 separate menu contributions for the same command)?  When you click on one, both should toggle.

PW
Comment 6 Paul Webster CLA 2009-03-05 14:25:37 EST
(In reply to comment #5)
> Created an attachment (id=127695) [details]
> toggle state v02

Released to HEAD >20090305
PW
Comment 7 Prakash Rangaraj CLA 2009-03-06 00:17:07 EST
(In reply to comment #5)

> What happens when you add a toggle command to the toolbar twice (i.e. 2
> separate menu contributions for the same command)?  When you click on one, both
> should toggle.

  Yes. it works that way.
Comment 8 Prakash Rangaraj CLA 2009-03-06 05:03:41 EST
Created attachment 127790 [details]
Radio state patch 01

Patch for Radio state.
Comment 9 Paul Webster CLA 2009-03-06 09:22:14 EST
(In reply to comment #8)
> Created an attachment (id=127790) [details]
> Radio state patch 01
> 

Comments:

The API of your RadioState is fine,  but we should probably implement it by extending TextState and use the same pattern as RegistryToggleState ... support persist and default in the IExecutableExtension part.  I wish we could have called it RegistryRadioState but that's already taken :-)


Change call isInUpdatedRadioState(*) something like matchesRadioState(event)

In HandlerProxy we don't need private IStateListener radioStateListener, as AbstractHandlerWithState adds HandlerProxy as the listener to every State on the Command, so you should implement what you want in the handleStateChange we already have.

Also, your test has values="org.eclipse.bugs.commandstate.handlers.RadioStateParameterValues"> in your plugin.xml ... that class doesn't exist.

PW
Comment 10 Prakash Rangaraj CLA 2009-03-06 12:00:48 EST
Created attachment 127836 [details]
Radio State patch v02

(In reply to comment #9)

> The API of your RadioState is fine,  but we should probably implement it by
> extending TextState and use the same pattern as RegistryToggleState ... support
> persist and default in the IExecutableExtension part.  I wish we could have
> called it RegistryRadioState but that's already taken :-)

    Problem with TextState is that when it loads from the preference and there is no preference exists, it sets the value to "". This will overwrite the value from the plugin.xml. Further, the load method is frozen to final, so it cannot be overridden. So which means, if we subclass from TextState, then we cannot do initialization from plugin.xml at all

> Change call isInUpdatedRadioState(*) something like matchesRadioState(event)
    Done.


> In HandlerProxy we don't need private IStateListener radioStateListener, as
> AbstractHandlerWithState adds HandlerProxy as the listener to every State on
> the Command, so you should implement what you want in the handleStateChange we
> already have.
   Done.

> Also, your test has
> values="org.eclipse.bugs.commandstate.handlers.RadioStateParameterValues"> in
> your plugin.xml ... that class doesn't exist.
   Sorry for that. Copy paste error. Deleted it now.
Comment 11 Paul Webster CLA 2009-03-06 12:14:38 EST
(In reply to comment #10)
> Created an attachment (id=127836) [details]
> Radio State patch v02
> 
>     Problem with TextState is that when it loads from the preference and there
> is no preference exists, it sets the value to "". This will overwrite the value
> from the plugin.xml. Further, the load method is frozen to final, so it cannot
> be overridden. So which means, if we subclass from TextState, then we cannot do
> initialization from plugin.xml at all

While it's up to you, you could just remove the final and add javadoc + @nooverride to the method

But I'm not really that interested, and I think that's something we could change after M6 (assuming we can convince the PMC that final == @nooverride)

PW
Comment 12 Paul Webster CLA 2009-03-06 12:38:44 EST
(In reply to comment #10)
> Created an attachment (id=127836) [details]
> Radio State patch v02

Released to HEAD with minor mods and some javadocs >20090306
PW
Comment 13 Prakash Rangaraj CLA 2009-03-11 02:55:05 EDT
Verified in I20090310-0100