Context
I'm building a plugin for eclipse 3.4 and more. I have a view with id mrp.view
with a menuContribution set to toolbar:mrp.view
. This menuContribution has some command, and I have this one:
<handler
class="mrp.handlers.export"
commandId="mrp.commands.export">
</handler>
<command
commandId="mrp.commands.export"
label="My command"
style="push">
</command>
My handler, mrp.handlers.export
has a dynamic ìsEnabled()` method, looking like that :
@Override
public boolean isEnabled() {
return !getMySelection().isEmpty();
}
Question
How can I refresh the button on my toolbar when data changed ? (refresh is done automatically if I click anothr button of the toolbar, but if I don't...)
I tried..
ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
service.refreshElements("mrp.commands.export", null);
But it doesn't seems to do anything.
Also this one:
public class Export extends AbstractHandler implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
setBaseEnabled(!getSelection().isEmpty());
}
// ....
}
It is called, but the icon on my view's menu is not refreshed (on eclipse 3.7). Did I do something wrong ?
Your handler must fire an event when it's enablement changes. If you have the change update your handler using org.eclipse.core.commands.AbstractHandler.setBaseEnabled(boolean) it will fire the required event.
Thanls to Paul Webster's answer, I got it working.
public class Export extends AbstractHandler implements PropertyChangeListener {
public Export() {
Activator.getDefault().AddListener(this);
setBaseEnabled(!getMySelection().isEmpty());
}
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// My handler
return null;
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(Activator.EVENT_SELECTION_CHANGED)) {
boolean before = isEnabled();
boolean after = !getMySelection().isEmpty();
if (after != before) {
setBaseEnabled(after);
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.