use of org.eclipse.core.expressions.EvaluationContext in project dbeaver by serge-rider.
the class ActionUtils method runCommand.
public static void runCommand(String commandId, ISelection selection, Map<String, Object> parameters, IServiceLocator serviceLocator) {
if (commandId != null) {
try {
ICommandService commandService = serviceLocator.getService(ICommandService.class);
IHandlerService handlerService = serviceLocator.getService(IHandlerService.class);
if (commandService != null) {
Command command = commandService.getCommand(commandId);
boolean needContextPatch = false;
if (selection != null) {
needContextPatch = true;
if (serviceLocator instanceof IWorkbenchSite) {
final ISelection curSelection = ((IWorkbenchSite) serviceLocator).getSelectionProvider().getSelection();
if (curSelection instanceof IStructuredSelection && selection instanceof IStructuredSelection) {
if (((IStructuredSelection) curSelection).size() == ((IStructuredSelection) selection).size() && ((IStructuredSelection) curSelection).getFirstElement() == ((IStructuredSelection) selection).getFirstElement()) {
// The same selection
needContextPatch = false;
}
}
}
}
Parameterization[] parametrization = null;
if (!CommonUtils.isEmpty(parameters)) {
parametrization = new Parameterization[parameters.size()];
int paramIndex = 0;
for (Map.Entry<String, Object> param : parameters.entrySet()) {
IParameter parameter = command.getParameter(param.getKey());
if (parameter != null) {
parametrization[paramIndex] = new Parameterization(parameter, CommonUtils.toString(param.getValue()));
} else {
log.debug("Parameter '" + param.getKey() + "' not found in command '" + commandId + "'");
parametrization[paramIndex] = null;
}
paramIndex++;
}
}
if (selection != null && needContextPatch) {
// Create new eval context
IEvaluationContext context = new EvaluationContext(handlerService.createContextSnapshot(false), selection);
if (serviceLocator instanceof IWorkbenchPartSite) {
context.addVariable(ISources.ACTIVE_PART_NAME, ((IWorkbenchPartSite) serviceLocator).getPart());
}
context.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
ParameterizedCommand pc = new ParameterizedCommand(command, parametrization);
handlerService.executeCommandInContext(pc, null, context);
} else if (command != null) {
if (command.isEnabled()) {
ParameterizedCommand pc = new ParameterizedCommand(command, parametrization);
handlerService.executeCommand(pc, null);
} else {
log.warn("Command '" + commandId + "' is disabled");
}
} else {
log.warn("Command '" + commandId + "' not found");
}
}
} catch (Exception e) {
DBWorkbench.getPlatformUI().showError("Error running command", "Can't execute command '" + commandId + "'", e);
}
}
}
Aggregations