Question

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".

Était-ce utile?

La solution

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.

Autres conseils

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.

In rcp E4 the solution seems to be a bit different. Since I first stumbled accross this question, I want to summerize/link the solution for future developers.

The relevant questions, where I found the answer are Switch Image of a Handler in a e4 rcp and RCP 4 Toggle a button in the toolbar.

Basically you need to add the argument MToolItem to your Execute-method like this:

@Execute
public void execute(final MToolItem item)
{
   item.setIconURI("platform:/plugin/......");
   //additional changes e.g. to the tooltip are also possible.
}

If you additionally give your toolbarbutton type checked

 <elements xsi:type="menu:HandledToolItem" type="Check" ...

the item will automatically toggle between selected/unselected with each click. You can get the current state with item.isSelected(). As a side effect, the toolbar icon will be highlighed in the selected-state. If you don't want that, you need to keep track of the state yourself.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top