4

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

Ben Hocking
  • 7,790
  • 5
  • 37
  • 52

3 Answers3

7

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.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • 1
    Excellent, although I also had to add code to trigger the event, using what I found here: http://www.robertwloch.net/2011/01/eclipse-tips-tricks-label-updating-command-handler/ – Ben Hocking May 20 '14 at 11:34
  • I have a `class DeployHandler` which `extends AbstractHandler` and `implements IElementUpdater` with a implementation of `updateElement`: `if (proj == null) element.setTooltip("Work with selected project"); else element.setTooltip("Work with project \"" + proj.getName() + "\"");` But it's not being called by the framework, although the handler gets properly registered and called. Could you help me to find out why? – Danny Lo Nov 30 '15 at 14:46
  • Apparently I am also missing some code to trigger the update event. In which method should I call `ICommandService#refreshElements`? – Danny Lo Nov 30 '15 at 15:01
  • @DannyLo Don't try and add follow up questions in comments - ask a new question. – greg-449 Nov 30 '15 at 15:04
  • 2
    Thanks @BenHocking - While the original link no longer works, there is a copy of the last version of the page at [archive.org](https://web.archive.org/web/20180311233946/http://www.robertwloch.net:80/2011/01/eclipse-tips-tricks-label-updating-command-handler/) – Mark Booth Feb 20 '19 at 15:11
3

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.

Ben Cox
  • 1,393
  • 10
  • 28
  • I am using the toggleState, but I see no way of changing the behavior of the tooltip text or button icon based on that. – Ben Hocking May 15 '14 at 11:29
  • 2
    I see. In general, the Eclipse ecosystem tends not to do that kind of thing. By using toggle buttons, you negate the need to change the icon or the tooltip. Instead, they should refer to the behaviour when the button is toggled on. For example, see the "Skip All Breakpoints" button in any Eclipse that includes the Debug perspective. Its icon (a crossed-out breakpoint) refers to what happens when it's toggled on, as does its tooltip. If, instead, the icon were to change to a not-crossed-out breakpoint and the tooltip changed to "Don't Skip All Breakpoints", the behaviour would be confusing. – Ben Cox May 15 '14 at 16:05
  • Your explanation is a good one, indigomonkey. I have updated my question accordingly. – Ben Hocking May 18 '14 at 19:32
0

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.

Hannah
  • 1