Search in sources :

Example 1 with Parameterization

use of org.eclipse.core.commands.Parameterization 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);
    }
}
Also used : IParameter(org.eclipse.core.commands.IParameter) IHandlerService(org.eclipse.ui.handlers.IHandlerService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) Parameterization(org.eclipse.core.commands.Parameterization) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ICommandService(org.eclipse.ui.commands.ICommandService)

Example 2 with Parameterization

use of org.eclipse.core.commands.Parameterization 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);
        }
    }
}
Also used : IParameter(org.eclipse.core.commands.IParameter) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Parameterization(org.eclipse.core.commands.Parameterization) ICommandService(org.eclipse.ui.commands.ICommandService) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) IHandlerService(org.eclipse.ui.handlers.IHandlerService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) ISelection(org.eclipse.jface.viewers.ISelection) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) EvaluationContext(org.eclipse.core.expressions.EvaluationContext) Map(java.util.Map) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Example 3 with Parameterization

use of org.eclipse.core.commands.Parameterization 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);
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IParameter(org.eclipse.core.commands.IParameter) IHandlerService(org.eclipse.ui.handlers.IHandlerService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) Parameterization(org.eclipse.core.commands.Parameterization) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ICommandService(org.eclipse.ui.commands.ICommandService)

Example 4 with Parameterization

use of org.eclipse.core.commands.Parameterization 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);
        }
    }
}
Also used : IParameter(org.eclipse.core.commands.IParameter) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Parameterization(org.eclipse.core.commands.Parameterization) ICommandService(org.eclipse.ui.commands.ICommandService) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) IHandlerService(org.eclipse.ui.handlers.IHandlerService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) ISelection(org.eclipse.jface.viewers.ISelection) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) EvaluationContext(org.eclipse.core.expressions.EvaluationContext) Map(java.util.Map) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Example 5 with Parameterization

use of org.eclipse.core.commands.Parameterization 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());
    }
}
Also used : IParameter(org.eclipse.core.commands.IParameter) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) ArrayList(java.util.ArrayList) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) Parameterization(org.eclipse.core.commands.Parameterization) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Aggregations

Command (org.eclipse.core.commands.Command)5 IParameter (org.eclipse.core.commands.IParameter)5 Parameterization (org.eclipse.core.commands.Parameterization)5 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)5 ICommandService (org.eclipse.ui.commands.ICommandService)4 IHandlerService (org.eclipse.ui.handlers.IHandlerService)4 NotDefinedException (org.eclipse.core.commands.common.NotDefinedException)3 Map (java.util.Map)2 EvaluationContext (org.eclipse.core.expressions.EvaluationContext)2 IEvaluationContext (org.eclipse.core.expressions.IEvaluationContext)2 ISelection (org.eclipse.jface.viewers.ISelection)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 ArrayList (java.util.ArrayList)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1