问题
So, I have 2 commands, which are identified by PLAY_COMMAND_ID
and STOP_COMMAND_ID
. Each of command have each handler, respectively playHandler
and stopHandler
(these are extending AbstractHandler
class).
These commands are contributed to my view's toolbar in Button style. Basically what I want is initially the PLAY_COMMAND
is active but the STOP_COMMAND
not. When the PLAY_COMMAND
is clicked, then it will activate the STOP_COMMAND
then deactivate itself(PLAY_COMMAND
). And vice versa when the STOP_COMMAND
clicked.
So what I do is like this. At first it works (I clicked play-button, then stop-button is activated and play-button disabled. I clicked stop-button, then play-button is active and stop-button is disabled. But when I clicked the play-button again, the play-button is still active when the stop-button is active too). So what's wrong with my code here:
private AbstractHandler playHandler, stopHandler, pauseHandler, stepHandler;
private IHandlerActivation playActivation, stopActivation, pauseActivation, stepActivation;
private void createHandlers(){
final IHandlerService handlerService = (IHandlerService)getSite().getService(IHandlerService.class);
playHandler = new AbstractHandler() {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
handlerService.deactivateHandler(playActivation);
if(stopActivation == null){
stopActivation = handlerService.activateHandler(STOP_COMMAND_ID, stopHandler);
} else {
handlerService.activateHandler(stopActivation);
}
return null;
}
};
stopHandler = new AbstractHandler() {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
handlerService.deactivateHandler(stopActivation);
handlerService.activateHandler(playActivation);
return null;
}
};
playActivation = handlerService.activateHandler(PLAY_COMMAND_ID, playHandler);
}
}
The createHandlers()
method is called at the end of createPartControl(Composite parent)
method in my View.
回答1:
Okay, so here what I found. The IHandlerActivation
, that is returned when calling activateHandler(IHandlerActivation)
method, when it is deactivated, can't be used again in activating the same handler. So the solution is try calling handlerService.activateHandler(commandID, playHandler)
instead of calling handlerService.activateHandler(playActivation)
.
来源:https://stackoverflow.com/questions/10385959/what-are-wrong-with-this-code-on-activating-deactivating-ihandleractivation-of-c