Skip to content

Instantly share code, notes, and snippets.

@lucacesari
Created October 15, 2014 07:39
Show Gist options
  • Save lucacesari/e76bdd66bbcf7ce08e33 to your computer and use it in GitHub Desktop.
Save lucacesari/e76bdd66bbcf7ce08e33 to your computer and use it in GitHub Desktop.
Eclipse parameterized Command builder
/*
* This content is released under the MIT License (http://opensource.org/licenses/MIT).
* Copyright (c) 2014 Luca Cesari <luc@cesari.me>
*/
package lucacesari.gists;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IParameter;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.Parameterization;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.IHandlerService;
public class CommandBuilder {
private static final Parameterization[] EMPTY_PARAMETERIZATION_ARRAY = new Parameterization[0];
private Command command;
private List<Parameterization> parameterizations;
public CommandBuilder setCommand(final String commandId) throws NotDefinedException {
final ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
command = commandService.getCommand(commandId);
if (command == null) {
throw new NotDefinedException("Command ID '" + commandId + "' not found.");
}
return this;
}
public CommandBuilder setParameter(final String parameterId, final String value) throws NotDefinedException {
if (parameterizations == null && value != null) {
parameterizations = new ArrayList<Parameterization>();
}
final IParameter parameter = command.getParameter(parameterId);
final Parameterization parameterization = new Parameterization(parameter, value);
parameterizations.add(parameterization);
return this;
}
public ParameterizedCommand getCommand() {
if (command == null) {
return null;
}
if (parameterizations == null || parameterizations.isEmpty()) {
return new ParameterizedCommand(command, null);
} else {
return new ParameterizedCommand(command, parameterizations.toArray(EMPTY_PARAMETERIZATION_ARRAY));
}
}
public void execute() throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
final ParameterizedCommand parameterizedCommand = getCommand();
if (parameterizedCommand == null) {
throw new ExecutionException("The command has not been configured yet.");
}
final IHandlerService service = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
service.executeCommand(parameterizedCommand, null);
}
public static CommandBuilder build(final String commandId) throws NotDefinedException {
return new CommandBuilder().setCommand(commandId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment