Reputation: 1138
I created a command:
<command
id="disableOrEnableCommand"
name="Disable or Enable Command">
</command>
Then I added a button to the toolbar:
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="disableOrEnable">
<command
commandId="disableOrEnableCommand"
label="Disable Me"
style="push">
</command>
</toolbar>
</menuContribution>
</extension>
The next step was binding the command to a handler:
<extension
point="org.eclipse.ui.handlers">
<handler
class="DisableOrEnableHandler"
commandId="disableOrEnableCommand">
</handler>
</extension>
I configured the handler to implement IHandler
and IElementUpdater
( because I want to update the button text):
public class DisableOrEnableHandler implements IHandler, IElementUpdater{
public boolean isEnabled = true;
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
isEnabled = !isEnabled;
// Trigger somehow the updateElement() method
return null;
}
@Override
public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
if ( isEnabled) {
element.setText("Disable me");
} else {
element.setText("Enable me");
}
}
// other overriden methods from IHandler and IElementUpdater
}
I am missing one piece of the puzzle, how can I configure the button to trigger the updateElement when the button is pressed?
Upvotes: 1
Views: 83
Reputation: 111216
You use the ICommandService.refreshElements
call to call update elements. In the handler you can use something like:
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
ICommandService commandService = window.getService(ICommandService.class);
commandService.refreshElements(event.getCommand().getId(), null);
(code abstracted from org.eclipse.ui.internal.handlers.PinEditorHandler
)
Upvotes: 1