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.

@Inject EHandlerService handlerService;
@Inject ECommandService commandService;

The command can be called by its specified id.

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
    else {
        logger.log(LogService.LOG_ERROR, "Cannot execute command " + commandId);

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

Map<String, String> parameters = new HashMap<String, String>();
parameters.put("command.parameter.connection", connection.getName());
ParameterizedCommand cmd = commandService.createCommand(commandId, parameters);

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.

public void execute(@Optional @Named("command.parameter.connection") String connection) {
    if (connection != null) {

The value of the @Named annotation must be the id specified in the Applicaton Model for the command parameter.
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.