use of org.eclipse.core.commands.ParameterizedCommand in project linuxtools by eclipse.
the class CommandUtils method execute.
/**
* Executes the command identified by the given {@code id} on a context
* based on the given selection
*
* @param id
* the id of the command to execute
* @param selection
* the selection to use as a context to run the command
*/
public static void execute(final String id, final IStructuredSelection selection) {
final ICommandService service = PlatformUI.getWorkbench().getService(ICommandService.class);
final Command command = service != null ? service.getCommand(id) : null;
if (command != null && command.isDefined()) {
try {
ParameterizedCommand pCmd = ParameterizedCommand.generateCommand(command, null);
IHandlerService handlerSvc = PlatformUI.getWorkbench().getService(IHandlerService.class);
IEvaluationContext ctx = handlerSvc.getCurrentState();
ctx = new EvaluationContext(ctx, selection);
ctx.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
handlerSvc.executeCommandInContext(pCmd, null, ctx);
} catch (Exception e) {
Activator.log(e);
}
}
}
use of org.eclipse.core.commands.ParameterizedCommand in project yamcs-studio by yamcs.
the class ScriptUtil method executeEclipseCommand.
/**
* Execute an Eclipse command with optional parameters. Any parameters must be defined along with the command in
* plugin.xml.
*
* @param commandId
* the Eclipse command id
* @param parameters
* a list of further String arguments alternating key, value: * executeEclipseCommand("id", ["pkey",
* "pvalue"])
*/
public static final void executeEclipseCommand(String commandId, String[] parameters) {
IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(IHandlerService.class);
try {
if (parameters.length % 2 != 0) {
throw new IllegalArgumentException("Parameterized commands must have " + "an equal number of keys and values");
}
if (parameters.length == 0) {
handlerService.executeCommand(commandId, null);
} else {
ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(ICommandService.class);
Parameterization[] params = new Parameterization[parameters.length / 2];
Command c = commandService.getCommand(commandId);
for (int i = 0; i < parameters.length / 2; i++) {
String key = parameters[2 * i];
String value = parameters[2 * i + 1];
IParameter p = c.getParameter(key);
Parameterization pp = new Parameterization(p, value);
params[i] = pp;
}
ParameterizedCommand pc = new ParameterizedCommand(c, params);
handlerService.executeCommand(pc, null);
}
} catch (Exception e) {
ErrorHandlerUtil.handleError("Failed to execute eclipse command: " + commandId, e);
}
}
use of org.eclipse.core.commands.ParameterizedCommand in project translationstudio8 by heartsome.
the class BindingModel2 method remove.
/**
* Removes the <code>bindingElement</code> binding.
* @param bindingElement
*/
public void remove(BindingElement bindingElement) {
if (bindingElement == null || !(bindingElement.getModelObject() instanceof Binding)) {
return;
}
KeyBinding keyBinding = (KeyBinding) bindingElement.getModelObject();
if (keyBinding.getType() == Binding.USER) {
bindingManager.removeBinding(keyBinding);
} else {
KeySequence keySequence = keyBinding.getKeySequence();
// Add the delete binding
bindingManager.addBinding(new KeyBinding(keySequence, null, keyBinding.getSchemeId(), keyBinding.getContextId(), null, null, null, Binding.USER));
// Unbind any conflicts affected by the delete binding
ConflictModel conflictModel = controller.getConflictModel();
conflictModel.updateConflictsFor(bindingElement);
Collection conflictsList = conflictModel.getConflicts();
if (conflictsList != null) {
Object[] conflicts = conflictsList.toArray();
for (int i = 0; i < conflicts.length; i++) {
BindingElement be = (BindingElement) conflicts[i];
if (be == bindingElement) {
continue;
}
Object modelObject = be.getModelObject();
if (modelObject instanceof Binding) {
Binding binding = (Binding) modelObject;
if (binding.getType() != Binding.SYSTEM) {
continue;
}
ParameterizedCommand pCommand = binding.getParameterizedCommand();
be.fill(pCommand);
commandToElement.put(pCommand, be);
}
}
}
}
ParameterizedCommand parameterizedCommand = keyBinding.getParameterizedCommand();
bindingElement.fill(parameterizedCommand);
commandToElement.put(parameterizedCommand, bindingElement);
controller.firePropertyChange(this, PROP_CONFLICT_ELEMENT_MAP, null, bindingElement);
}
use of org.eclipse.core.commands.ParameterizedCommand in project translationstudio8 by heartsome.
the class BindingModel2 method restoreBinding.
/**
* Restores the currently selected binding.
* @param contextModel
*/
public void restoreBinding(ContextModel contextModel) {
BindingElement element = (BindingElement) getSelectedElement();
if (element == null) {
return;
}
restoreBinding(element);
refresh(contextModel);
Object obj = element.getModelObject();
ParameterizedCommand cmd = null;
if (obj instanceof ParameterizedCommand) {
cmd = (ParameterizedCommand) obj;
} else if (obj instanceof KeyBinding) {
cmd = ((KeyBinding) obj).getParameterizedCommand();
}
boolean done = false;
Iterator i = bindingElements.iterator();
// Reselects the command
while (i.hasNext() && !done) {
BindingElement be = (BindingElement) i.next();
obj = be.getModelObject();
ParameterizedCommand pcmd = null;
if (obj instanceof ParameterizedCommand) {
pcmd = (ParameterizedCommand) obj;
} else if (obj instanceof KeyBinding) {
pcmd = ((KeyBinding) obj).getParameterizedCommand();
}
if (cmd.equals(pcmd)) {
done = true;
setSelectedElement(be);
}
}
}
use of org.eclipse.core.commands.ParameterizedCommand in project translationstudio8 by heartsome.
the class KeyController2 method filterDupliteBind.
@SuppressWarnings("restriction")
public void filterDupliteBind() {
IWorkbench workbench = PlatformUI.getWorkbench();
IBindingService bindingService = (IBindingService) workbench.getService(IBindingService.class);
BindingService service = (BindingService) bindingService;
BindingManager bindingManager = service.getBindingManager();
//service.getBindingManager().
Binding[] bindings = bindingManager.getBindings();
List<Binding> bindTemp = new ArrayList<Binding>();
List<String> ids = new ArrayList<String>();
for (Binding bind : bindings) {
if (null == bind) {
continue;
}
ParameterizedCommand command = bind.getParameterizedCommand();
if (null == command) {
continue;
}
String id = command.getId();
if (!ids.contains(id)) {
ids.add(id);
bindTemp.add(bind);
}
}
bindingManager.setBindings(bindTemp.toArray(new Binding[ids.size()]));
}
Aggregations