Differences

This shows you the differences between two versions of the page.

Link to this comparison view

parameterized_command [2014/06/23 11:21]
parameterized_command [2021/04/05 11:23] (current)
Line 1: Line 1:
 +====== Parameterized Commands ======
  
 +Most of the time commands are called from the framework and not directly. But sometimes it is necessary to call a command by hand. The Eclipse framework offers ready to use services for this, HandlerService and CommandService. Both can be injected with e4 dependency injection.
 +
 +<sxh java>
 +@Inject EHandlerService handlerService;
 +@Inject ECommandService commandService;
 +</sxh>
 +
 +The command can be called by its specified id.
 +
 +<sxh java>
 +Command command = commandService.getCommand(commandId);
 +
 +if (command.isDefined()) {
 +
 +    // Activate Handler
 +    handlerService.activateHandler(OpenConnectionHandler.ID, new OpenConnectionHandler());
 +
 +    ParameterizedCommand cmd = commandService.createCommand(command, null);
 +
 +    // Check if the command can get executed
 +    if (handlerService.canExecute(cmd)) {
 +        // Execute the command
 +        handlerService.executeHandler(cmd);
 +    }
 +    else {
 +        logger.log(LogService.LOG_ERROR, "Cannot execute command " + commandId);
 +    }
 +}
 +</sxh>
 +
 +From time to time a parameter needs to be passed to the handler. There are at least three positions which need to be adjusted.
 +
 +  * Application Model - Command Definition
 +  * ParameterizedCommand Call
 +  * Handler
 +
 +Parameters are passed within a Map which takes String for keys and values, ''Map<String,String>''.
 +
 +<sxh java>
 +Map<String, String> parameters = new HashMap<String, String>();
 +parameters.put("command.parameter.connection", connection.getName());
 +ParameterizedCommand cmd = commandService.createCommand(commandId, parameters);
 +</sxh>
 +
 +With the Eclipse 3.x framework the handlers needed to implement ''AbstractHandler'' and its ''execute()'' Method. Through the ''ExecutionEvent'' parameter of the method the handler has access to the parameter passed to the command, ''event.getParameter(parameterId)''.
 +
 +But with the e4 framework there is no event. The parameters must be declared in the commands in the Application Model and in the handler ''execute()'' method as a ''@Named'' parameter.
 +
 +Parameters can be declared as ''@Optional''. Optional parameters should be checked for ''null''.
 +
 +<sxh java>
 +@Execute
 +public void execute(@Optional @Named("command.parameter.connection") String connection) {
 +    if (connection != null) {
 +       ...
 +    }
 +}
 +</sxh>
 +
 +<note>The value of the @Named annotation must be the id specified in the Applicaton Model for the command parameter.</note>
 +
 +<note tip>Parameterized commands can be used to pass the current selection to a handler because the ''ESelectionService'' does not return the correct value when a double click event is processed.</note>
 +
 +{{tag>devel e4 eclipse java}}