use of org.eclipse.core.commands.IParameter 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.IParameter in project dbeaver by dbeaver.
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);
}
}
}
use of org.eclipse.core.commands.IParameter in project org.csstudio.display.builder by kasemir.
the class RCPUtil method doExecuteEclipseCommand.
private static final void doExecuteEclipseCommand(String commandId, String... parameters) {
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
final IHandlerService handlerService = window.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 {
final ICommandService commandService = window.getService(ICommandService.class);
final Parameterization[] params = new Parameterization[parameters.length / 2];
final Command c = commandService.getCommand(commandId);
for (int i = 0; i < parameters.length / 2; i++) {
final String key = parameters[2 * i];
final String value = parameters[2 * i + 1];
final IParameter p = c.getParameter(key);
final Parameterization pp = new Parameterization(p, value);
params[i] = pp;
}
final ParameterizedCommand pc = new ParameterizedCommand(c, params);
handlerService.executeCommand(pc, null);
}
} catch (Exception ex) {
logger.log(Level.WARNING, "Failed to execute eclipse command '" + commandId + "' " + Arrays.toString(parameters), ex);
}
}
use of org.eclipse.core.commands.IParameter 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);
}
}
}
use of org.eclipse.core.commands.IParameter in project portfolio by buchen.
the class WelcomePart method executeCommand.
private void executeCommand(String command, String... parameters) {
try {
Command cmd = commandService.getCommand(command);
List<Parameterization> parameterizations = new ArrayList<>();
if (parameters != null) {
for (int ii = 0; ii < parameters.length; ii = ii + 2) {
IParameter p = cmd.getParameter(parameters[ii]);
parameterizations.add(new Parameterization(p, parameters[ii + 1]));
}
}
ParameterizedCommand pCmd = new ParameterizedCommand(cmd, parameterizations.toArray(new Parameterization[0]));
if (handlerService.canExecute(pCmd))
handlerService.executeHandler(pCmd);
} catch (NotDefinedException e) {
PortfolioPlugin.log(e);
MessageDialog.openError(Display.getDefault().getActiveShell(), Messages.LabelError, e.getMessage());
}
}
Aggregations