Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » How to sync radio-menuitems with the toolbar
How to sync radio-menuitems with the toolbar [message #666534] Wed, 20 April 2011 14:35 Go to next message
Carlo Salinari is currently offline Carlo Salinari
Messages: 66
Registered: October 2010
Member
Hi,

In my app, a cad-like program, I have a menu called Tools containing a
group of drawing-related commands (Line, Rectangle, Polygon..) with
style radio (only one tool at a time can be selected).

I also have a toolbar with the same drawing commands.

How do I keep the menu and the toolbar radio groups in sync?

I mean, when I select Line in the menu I'd want to see it reflected also
in the toolbar (Line button pushed down) and vice-versa, if I push a
toolbar button (say, I press the Rectangle) see it selected in the menu.

I thought that, since they share the same command.ids and handlers, it
would work automatically, but it doesn't (they behave as two unrelated
radio groups).

Cheers,
Carlo
Re: How to sync radio-menuitems with the toolbar [message #666535 is a reply to message #666534] Wed, 20 April 2011 15:01 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

You can use the handlers IElementUpdater and its Radio state to update
all elements into the correct checked and unchecked state.

You just have to call ICommandService refreshElements(*)

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Re: How to sync radio-menuitems with the toolbar [message #666546 is a reply to message #666535] Wed, 20 April 2011 16:33 Go to previous messageGo to next message
Carlo Salinari is currently offline Carlo Salinari
Messages: 66
Registered: October 2010
Member
On 4/20/2011 9:01 PM, Paul Webster wrote:
> You can use the handlers IElementUpdater and its Radio state to update
> all elements into the correct checked and unchecked state.
>
> You just have to call ICommandService refreshElements(*)

mmm... I think I miss some steps.

This is my understanding of your suggestion:


public class Line extends AbstractHandler implements IElementUpdater {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
ICommandService service = (ICommandService) HandlerUtil
..getActiveSiteChecked(event).getService(ICommandService.cla ss);

for (String id : (Collection<String>) service.getDefinedCommandIds()) {
service.refreshElements(id, null);
}
return null;
}

@Override
public void updateElement(UIElement element, Map parameters) {
// element.setChecked(same-as-Radio-state);
}
}


Am I on the right track? How do I get access to the Radio state from
updateElement?

Also, I'm not explicitly defining Radio states in plugin.xml. Could this
be part of the problem?
Re: How to sync radio-menuitems with the toolbar [message #666655 is a reply to message #666546] Thu, 21 April 2011 09:22 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 04/20/2011 04:33 PM, Carlo Salinari wrote:
> for (String id : (Collection<String>) service.getDefinedCommandIds()) {
> service.refreshElements(id, null);
> }
> return null;

This is in effect trying to refresh every command in the system. You
can find your command id from the event, and only refresh that (at the
end of your handler).

>
> Also, I'm not explicitly defining Radio states in plugin.xml. Could this
> be part of the problem?

Using commands with radio buttons involves creating a radio state as
part of the command definition, and then creating the radio parameter as
part of the menuContribution/command elements.

When actually updating elements, you then compare the radio parameter
that would be passed into updateElement(*) and set checked appropriately.

Have a look at the org.eclipse.example.commands plugin in
https://github.com/paulweb515/commandsEclipseCon2011

That has an example of setting up a group of radio buttons with
commands. Once you have that, then you can look at managing 2 of them.

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Re: How to sync radio-menuitems with the toolbar [message #666865 is a reply to message #666655] Sat, 23 April 2011 12:02 Go to previous messageGo to next message
Carlo Salinari is currently offline Carlo Salinari
Messages: 66
Registered: October 2010
Member
On 4/21/2011 3:22 PM, Paul Webster wrote:
> On 04/20/2011 04:33 PM, Carlo Salinari wrote:
>> for (String id : (Collection<String>) service.getDefinedCommandIds()) {
>> service.refreshElements(id, null);
>> }
>> return null;
>
> This is in effect trying to refresh every command in the system. You
> can find your command id from the event, and only refresh that (at the
> end of your handler).
>
>>
>> Also, I'm not explicitly defining Radio states in plugin.xml. Could this
>> be part of the problem?
>
> Using commands with radio buttons involves creating a radio state as
> part of the command definition, and then creating the radio parameter as
> part of the menuContribution/command elements.
>
> When actually updating elements, you then compare the radio parameter
> that would be passed into updateElement(*) and set checked appropriately.
>
> Have a look at the org.eclipse.example.commands plugin in
> https://github.com/paulweb515/commandsEclipseCon2011
>
> That has an example of setting up a group of radio buttons with
> commands. Once you have that, then you can look at managing 2 of them.
>

Thanks a lot for the pointers. The presentation is really helpful, even
if a bit terse. I'll probably harass you in the near future about the
5/6 patterns whose existence I totally ignored! :-)

For now, I have a much dumber problem: I can't run the
org.eclipse.example.commands plugin.

When I try to export it (so that I can put it in my eclipse\plugins dir)
I get an error in the builder @dot.log saying:
"Syntax error, annotations are only available if source level is 1.5"

I've (more than) triple checked that the compiler compliance level is
set to 1.6 everywhere. I've even deleted .metadata and reinstalled
eclipse (helios), but no luck. Do you have any idea how to solve this?

Carlo
Re: How to sync radio-menuitems with the toolbar [message #666997 is a reply to message #666865] Mon, 25 April 2011 08:17 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 04/23/2011 12:02 PM, Carlo Salinari wrote:
>
> For now, I have a much dumber problem: I can't run the
> org.eclipse.example.commands plugin.
>
> When I try to export it (so that I can put it in my eclipse\plugins dir)
> I get an error in the builder @dot.log saying:
> "Syntax error, annotations are only available if source level is 1.5"

There was a spurious build.properties line (I have no idea where that
came from) that told it to use java 1.3 ... sigh. I've pushed a fix
(just remove the line :-)

Later,
PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Re: How to sync radio-menuitems with the toolbar [message #667016 is a reply to message #666997] Mon, 25 April 2011 10:05 Go to previous messageGo to next message
Carlo Salinari is currently offline Carlo Salinari
Messages: 66
Registered: October 2010
Member
On 4/25/2011 2:17 PM, Paul Webster wrote:
> On 04/23/2011 12:02 PM, Carlo Salinari wrote:
>>
>> For now, I have a much dumber problem: I can't run the
>> org.eclipse.example.commands plugin.
>>
>> When I try to export it (so that I can put it in my eclipse\plugins dir)
>> I get an error in the builder @dot.log saying:
>> "Syntax error, annotations are only available if source level is 1.5"
>
> There was a spurious build.properties line (I have no idea where that
> came from) that told it to use java 1.3 ... sigh. I've pushed a fix
> (just remove the line :-)

Ok, everything works now. I'm studying the command patterns now.
Re: How to sync radio-menuitems with the toolbar [message #667023 is a reply to message #667016] Mon, 25 April 2011 10:41 Go to previous messageGo to next message
Carlo Salinari is currently offline Carlo Salinari
Messages: 66
Registered: October 2010
Member
On 4/25/2011 4:05 PM, Carlo Salinari wrote:
> On 4/25/2011 2:17 PM, Paul Webster wrote:
>> There was a spurious build.properties line (I have no idea where that
>> came from) that told it to use java 1.3 ... sigh. I've pushed a fix
>> (just remove the line :-)
>
> Ok, everything works now. I'm studying the command patterns now.

by chance, is there any video/audio recording of your presentations?
Re: How to sync radio-menuitems with the toolbar [message #667024 is a reply to message #667023] Mon, 25 April 2011 11:19 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 04/25/2011 10:41 AM, Carlo Salinari wrote:
>
> by chance, is there any video/audio recording of your presentations?

I had a look at http://www.eclipsecon.org/2011/ and didn't see a link to
presentation recordings, they probably didn't make recordings of the
sessions.

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Re: How to sync radio-menuitems with the toolbar [message #667025 is a reply to message #667024] Mon, 25 April 2011 12:55 Go to previous messageGo to next message
Carlo Salinari is currently offline Carlo Salinari
Messages: 66
Registered: October 2010
Member
On 4/25/2011 5:19 PM, Paul Webster wrote:
> On 04/25/2011 10:41 AM, Carlo Salinari wrote:
>>
>> by chance, is there any video/audio recording of your presentations?
>
> I had a look at http://www.eclipsecon.org/2011/ and didn't see a link to
> presentation recordings, they probably didn't make recordings of the
> sessions.

Pity.

First quick question about pattern 4 (radio buttons):

I see that the first time the plugin is loaded the choice "page" is
selected, as indicated by the state element

<state
class="org.eclipse.ui.handlers.RadioState:page"
id="org.eclipse.ui.commands.radioState">
</state>

in the declaration of the org.eclipse.example.commands.viewPage command.

Then, if I select "Outline" and then restart the ide, the state is
evidently persisted and then restored, because when I reopen the View
menu I see "Outline" selected.

So, the persisted state (outline) wins over the declaration-based
initial state (page).

How can I change this behavior, so that, no matter what, when the plugin
starts the initial command state is always set to "page"?
Re: How to sync radio-menuitems with the toolbar [message #667027 is a reply to message #667025] Mon, 25 April 2011 13:32 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 04/25/2011 12:55 PM, Carlo Salinari wrote:
>
> How can I change this behavior, so that, no matter what, when the plugin
> starts the initial command state is always set to "page"?

See
org.eclipse.ui.handlers.RadioState.setInitializationData(ICo nfigurationElement,
String, Object)

You can use the other format for executable extension and supply
multiple parameters:

<state id="org.eclipse.ui.commands.radioState">
<class class="org.eclipse.ui.handlers.RadioState">
<parameter name="default" value="page"/>
<parameter name="persisted" value="false"/>
</class>
</state>

or something similar to above (I'm not 100% sure about the syntax).

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Re: How to sync radio-menuitems with the toolbar [message #667028 is a reply to message #667027] Mon, 25 April 2011 15:26 Go to previous message
Carlo Salinari is currently offline Carlo Salinari
Messages: 66
Registered: October 2010
Member
On 4/25/2011 7:32 PM, Paul Webster wrote:
> On 04/25/2011 12:55 PM, Carlo Salinari wrote:
>>
>> How can I change this behavior, so that, no matter what, when the plugin
>> starts the initial command state is always set to "page"?
>
> See
> org.eclipse.ui.handlers.RadioState.setInitializationData(ICo nfigurationElement,
> String, Object)
>
> You can use the other format for executable extension and supply
> multiple parameters:
>
> <state id="org.eclipse.ui.commands.radioState">
> <class class="org.eclipse.ui.handlers.RadioState">
> <parameter name="default" value="page"/>
> <parameter name="persisted" value="false"/>
> </class>
> </state>
>
> or something similar to above (I'm not 100% sure about the syntax).

Yep, it works.

For the record, I re-created my use-case (radio commands shared by both
menu and toolbar) inside your example plugin and it worked (making me
happy as a lark :-) ).

Here is how I proceeded:
1. I created four commands in the toolbar menuContribution (Page,
Outline, Notes, Master)

2. Set the commandId to org.eclipse.example.commands.viewPage

3. Set the style to "radio"

4. Manually edited the contributions' command-element to include the
radioStateParameter, here is one for reference:

<command
commandId="org.eclipse.example.commands.viewPage"
label="Page"
mode="FORCE_TEXT"
style="radio">
<parameter
name="org.eclipse.ui.commands.radioStateParameter"
value="page">
</parameter>
</command>

To my surprise, this even works if your radio group spans more than one
menu, or if it contains separators (this is different from swt's normal
behavior).
Previous Topic:Can different instances of a view part have different content in toolbars?
Next Topic:Patching a third party bundle with fragment
Goto Forum:
  


Current Time: Thu Jul 31 03:38:42 EDT 2014

Powered by FUDForum. Page generated in 0.02408 seconds