Search in sources :

Example 16 with Command

use of org.eclipse.core.commands.Command in project yamcs-studio by yamcs.

the class OPIShell method sendUpdateCommand.

/**
 * Alert whoever is listening that a new OPIShell has been created.
 */
private static void sendUpdateCommand() {
    IServiceLocator serviceLocator = PlatformUI.getWorkbench();
    ICommandService commandService = (ICommandService) serviceLocator.getService(ICommandService.class);
    try {
        Command command = commandService.getCommand(OPI_SHELLS_CHANGED_ID);
        command.executeWithChecks(new ExecutionEvent());
    } catch (ExecutionException | NotHandledException | NotEnabledException | NotDefinedException e) {
        log.log(Level.WARNING, "Failed to send OPI shells changed command", e);
    }
}
Also used : NotHandledException(org.eclipse.core.commands.NotHandledException) Command(org.eclipse.core.commands.Command) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) IServiceLocator(org.eclipse.ui.services.IServiceLocator) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) ExecutionException(org.eclipse.core.commands.ExecutionException) NotEnabledException(org.eclipse.core.commands.NotEnabledException) ICommandService(org.eclipse.ui.commands.ICommandService)

Example 17 with Command

use of org.eclipse.core.commands.Command 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 18 with Command

use of org.eclipse.core.commands.Command in project translationstudio8 by heartsome.

the class CommandsPropertyTester method test.

public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
    if (receiver instanceof IServiceLocator && args.length == 1 && args[0] instanceof String) {
        final IServiceLocator locator = (IServiceLocator) receiver;
        if (TOGGLE_PROPERTY_NAME.equals(property)) {
            final String commandId = args[0].toString();
            final ICommandService commandService = (ICommandService) locator.getService(ICommandService.class);
            final Command command = commandService.getCommand(commandId);
            final State state = command.getState(RegistryToggleState.STATE_ID);
            if (state != null) {
                return state.getValue().equals(expectedValue);
            }
        }
    }
    return false;
}
Also used : Command(org.eclipse.core.commands.Command) RegistryToggleState(org.eclipse.ui.handlers.RegistryToggleState) State(org.eclipse.core.commands.State) IServiceLocator(org.eclipse.ui.services.IServiceLocator) ICommandService(org.eclipse.ui.commands.ICommandService)

Example 19 with Command

use of org.eclipse.core.commands.Command in project translationstudio8 by heartsome.

the class NewHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    String parameter = event.getParameter("wizardParameter");
    Command command = null;
    ICommandService commandService = (ICommandService) HandlerUtil.getActiveSite(event).getService(ICommandService.class);
    if (parameter == null || parameter.equalsIgnoreCase("project")) {
        command = commandService.getCommand("net.heartsome.cat.ts.ui.command.newProject");
    } else if (parameter.equalsIgnoreCase("folder")) {
        command = commandService.getCommand("net.heartsome.cat.ts.ui.command.newFolder");
    } else if (parameter.equalsIgnoreCase("tm")) {
        command = commandService.getCommand("net.heartsome.cat.database.ui.tm.command.newTM");
    } else if (parameter.equalsIgnoreCase("tb")) {
        command = commandService.getCommand("net.heartsome.cat.database.ui.tb.command.newTB");
    }
    if (command == null) {
        return null;
    }
    IEvaluationService evalService = (IEvaluationService) HandlerUtil.getActiveSite(event).getService(IEvaluationService.class);
    IEvaluationContext currentState = evalService.getCurrentState();
    ExecutionEvent executionEvent = new ExecutionEvent(command, Collections.EMPTY_MAP, this, currentState);
    try {
        command.executeWithChecks(executionEvent);
    } catch (NotDefinedException e) {
        LOGGER.error("", e);
    } catch (NotEnabledException e) {
        LOGGER.error("", e);
    } catch (NotHandledException e) {
        LOGGER.error("", e);
    }
    return null;
}
Also used : NotHandledException(org.eclipse.core.commands.NotHandledException) Command(org.eclipse.core.commands.Command) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) NotEnabledException(org.eclipse.core.commands.NotEnabledException) ICommandService(org.eclipse.ui.commands.ICommandService) IEvaluationService(org.eclipse.ui.services.IEvaluationService)

Example 20 with Command

use of org.eclipse.core.commands.Command in project translationstudio8 by heartsome.

the class AbstractShieldCommandStartup method earlyStartup.

public void earlyStartup() {
    Set<String> unusedCommand = getUnusedCommandSet();
    IWorkbench workbench = PlatformUI.getWorkbench();
    ICommandService commandService = (ICommandService) workbench.getService(ICommandService.class);
    Command command;
    for (String commandId : unusedCommand) {
        command = commandService.getCommand(commandId);
        command.undefine();
    }
}
Also used : IWorkbench(org.eclipse.ui.IWorkbench) Command(org.eclipse.core.commands.Command) ICommandService(org.eclipse.ui.commands.ICommandService)

Aggregations

Command (org.eclipse.core.commands.Command)31 ICommandService (org.eclipse.ui.commands.ICommandService)24 IHandlerService (org.eclipse.ui.handlers.IHandlerService)8 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)7 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)7 NotDefinedException (org.eclipse.core.commands.common.NotDefinedException)7 IEvaluationContext (org.eclipse.core.expressions.IEvaluationContext)7 State (org.eclipse.core.commands.State)5 HashMap (java.util.HashMap)4 ExecutionException (org.eclipse.core.commands.ExecutionException)4 EvaluationContext (org.eclipse.core.expressions.EvaluationContext)4 IEvaluationService (org.eclipse.ui.services.IEvaluationService)4 NotEnabledException (org.eclipse.core.commands.NotEnabledException)3 NotHandledException (org.eclipse.core.commands.NotHandledException)3 Test (org.junit.Test)3 Binding (org.eclipse.jface.bindings.Binding)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)2 IWorkbench (org.eclipse.ui.IWorkbench)2 RegistryToggleState (org.eclipse.ui.handlers.RegistryToggleState)2