Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I toggle the menu button icon and tooltip text for an Eclipse plugin?

I'm developing an Eclipse plugin that has a toolbar menu item that will be enabling/disabling a feature. I know how to set the icon and tooltip text for it using the plugin.xml file, but I want to be able to change the icon and especially the tooltip text for it depending on the state of the project being worked on. I.e., if the feature is not enabled, I want an icon that shows turning the feature on along with tooltip text that says "enable feature", but if the feature is enabled, I want an icon that shows turning the feature off along with tooltip text that says "disable feature". Right now, the only option I can see is a generic icon and tooltip text that says "enable/disable feature", but that feels clumsy to me.

Edit to add: In response to indigomonkey's answer, which is a good one based on what I originally wrote, I would like to clarify that the behavior that is being enabled by the toolbar button is one that we really discourage ever disabling once it has been enabled (and a dialog pops up both to when enabling it to verify that they want to enable it because it should not be started lightly as well as to discourage them from disabling it once it is enabled). Because of this, I would like the icon to change to one that suggests "don't click on me".

like image 353
Ben Hocking Avatar asked Dec 26 '22 09:12

Ben Hocking


2 Answers

If your command Handler implements IElementUpdater the updateElement method will be called whenever the command is run.

The UIElement parameter of updateElement has setIcon and setTooltip methods.

like image 180
greg-449 Avatar answered Jan 27 '23 07:01

greg-449


I would suggest that you choose to create your toolbar contribution as a 'checkbox' type, rather than the standard 'push'. This will mean that the button will toggle between a selected and unselected state, allowing you to equate this behaviour to the enabled and disabled state of your feature.

If you're using org.eclipse.ui.commands and org.eclipse.ui.handlers along with the org.eclipse.ui.menus extension point to contribute your button with a <command> element, you'll need to set its style attribute to check. You can then read and toggle the command's selection from inside the handler.

For more information, see http://blog.eclipse-tips.com/2009/03/commands-part-6-toggle-radio-menu.html.

like image 25
Ben Cox Avatar answered Jan 27 '23 09:01

Ben Cox