Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » dynamically changing menu and toolbar icons
dynamically changing menu and toolbar icons [message #510898] Fri, 29 January 2010 04:00 Go to next message
Alex Ignácio da Silva is currently offline Alex Ignácio da SilvaFriend
Messages: 23
Registered: July 2009
Junior Member
Hi all,

I have a command contributed through the command framework extension
points to both a menu item and a toolbar item. In order to dynamically
change the image associated with the menu and toolbar items, I let the
command handler implement the org.eclipse.ui.commands.IElementUpdater
interface:

public class MyHandler extends AbstractHandler implements IElementUpdater {

...

@Override
public void updateElement(UIElement element, Map parameters) {
element.setIcon(...);
}


}

In my application I use 16x16 icons for menu items, and 24x24 icons for
toolbar icons, so I need to set a different image for each UIElement.
The problem is that the UIElement interface has no API to extract
information from the element being configured, so I have no way to tell
whether it's a menu or a toolbar item.

Can anyone suggest a way to work around this limitation?

Thanks,

Alex
Re: dynamically changing menu and toolbar icons [message #510935 is a reply to message #510898] Fri, 29 January 2010 08:50 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 29.01.2010 05:00, Alex Ignácio da Silva wrote:
> Hi all,
>
> I have a command contributed through the command framework extension
> points to both a menu item and a toolbar item. In order to dynamically
> change the image associated with the menu and toolbar items, I let the
> command handler implement the org.eclipse.ui.commands.IElementUpdater
> interface:
>
> public class MyHandler extends AbstractHandler implements IElementUpdater {
>
> ...
>
> @Override
> public void updateElement(UIElement element, Map parameters) {
> element.setIcon(...);
> }
>
>
> }
>
> In my application I use 16x16 icons for menu items, and 24x24 icons for
> toolbar icons, so I need to set a different image for each UIElement.
> The problem is that the UIElement interface has no API to extract
> information from the element being configured, so I have no way to tell
> whether it's a menu or a toolbar item.
>
> Can anyone suggest a way to work around this limitation?

You should be able to do that quite cleanly as follows: First
add to your org.eclipse.ui.commands extension a command
parameter:

<extension
point="org.eclipse.ui.commands">
<command
id="/your-command-id/" name="bla-bla">
<commandParameter id="/your-parameter-id/"
name="%blub"
optional="true">
</commandParameter>
</command>
</extension>

Now ensure that your org.eclipse.ui.menus menu contribution with
different representations set a different parameter *value*,
in the following contribution definition this value is
"/param-value-1/":

<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="/whatever/">
<command
commandId="/your-command-id/"
label="bla-bla"
style="push">
<parameter name="/your-parameter-id/"
value="/param-value-1/">
</parameter>
</command>
</menuContribution>
</extension>

Finally, in your updateElement method, you use the "Map parameters"
argument to check the current command parameter value by accessing
parameters.get("/your-parameter-id/") and comparing the result
against your different values, i.e against "/param-value-1/"
etc.

Remark: I believe this should work. A similar variant of this
idea is that you can define different execution behavior
of your Object execute(ExecutionEvent event) implementation
by checking the corresponding parameter value via
event.getParameter("/your-parameter-id/") to decide for
the parameter-specific "execution policy".

HTH & Greetings from Bremen,

Daniel Krügler
Re: dynamically changing menu and toolbar icons [message #511102 is a reply to message #510935] Fri, 29 January 2010 15:46 Go to previous message
Alex Ignácio da Silva is currently offline Alex Ignácio da SilvaFriend
Messages: 23
Registered: July 2009
Junior Member
Hi Daniel,

That works perfectly!

Thank you very much for pointing out the solution,

Alex

Daniel Krügler wrote:
>
> You should be able to do that quite cleanly as follows: First
> add to your org.eclipse.ui.commands extension a command
> parameter:
>
> <extension
> point="org.eclipse.ui.commands">
> <command
> id="/your-command-id/" name="bla-bla">
> <commandParameter id="/your-parameter-id/"
> name="%blub"
> optional="true">
> </commandParameter>
> </command>
> </extension>
>
> Now ensure that your org.eclipse.ui.menus menu contribution with
> different representations set a different parameter *value*,
> in the following contribution definition this value is
> "/param-value-1/":
>
> <extension
> point="org.eclipse.ui.menus">
> <menuContribution
> locationURI="/whatever/">
> <command
> commandId="/your-command-id/"
> label="bla-bla"
> style="push">
> <parameter name="/your-parameter-id/"
> value="/param-value-1/">
> </parameter>
> </command>
> </menuContribution>
> </extension>
>
> Finally, in your updateElement method, you use the "Map parameters"
> argument to check the current command parameter value by accessing
> parameters.get("/your-parameter-id/") and comparing the result
> against your different values, i.e against "/param-value-1/"
> etc.
>
> Remark: I believe this should work. A similar variant of this
> idea is that you can define different execution behavior
> of your Object execute(ExecutionEvent event) implementation
> by checking the corresponding parameter value via
> event.getParameter("/your-parameter-id/") to decide for
> the parameter-specific "execution policy".
>
> HTH & Greetings from Bremen,
>
> Daniel Krügler
Previous Topic:problem with setSelection() for Graphical view
Next Topic:Save view as jpg image
Goto Forum:
  


Current Time: Wed Apr 24 14:03:01 GMT 2024

Powered by FUDForum. Page generated in 0.03205 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top