Search in sources :

Example 11 with ParameterizedCommand

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);
        }
    }
}
Also used : IHandlerService(org.eclipse.ui.handlers.IHandlerService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) EvaluationContext(org.eclipse.core.expressions.EvaluationContext) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ICommandService(org.eclipse.ui.commands.ICommandService)

Example 12 with ParameterizedCommand

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);
    }
}
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 13 with ParameterizedCommand

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);
}
Also used : KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) Binding(org.eclipse.jface.bindings.Binding) KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) BindingElement(org.eclipse.ui.internal.keys.model.BindingElement) ConflictModel(org.eclipse.ui.internal.keys.model.ConflictModel) Collection(java.util.Collection) KeySequence(org.eclipse.jface.bindings.keys.KeySequence) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Example 14 with ParameterizedCommand

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);
        }
    }
}
Also used : KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) BindingElement(org.eclipse.ui.internal.keys.model.BindingElement) Iterator(java.util.Iterator) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Example 15 with ParameterizedCommand

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()]));
}
Also used : IWorkbench(org.eclipse.ui.IWorkbench) BindingManager(org.eclipse.jface.bindings.BindingManager) KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) Binding(org.eclipse.jface.bindings.Binding) IBindingService(org.eclipse.ui.keys.IBindingService) BindingService(org.eclipse.ui.internal.keys.BindingService) ArrayList(java.util.ArrayList) IBindingService(org.eclipse.ui.keys.IBindingService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Aggregations

ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)17 Binding (org.eclipse.jface.bindings.Binding)11 KeyBinding (org.eclipse.jface.bindings.keys.KeyBinding)9 Command (org.eclipse.core.commands.Command)7 BindingElement (org.eclipse.ui.internal.keys.model.BindingElement)7 ICommandService (org.eclipse.ui.commands.ICommandService)6 Iterator (java.util.Iterator)4 NotDefinedException (org.eclipse.core.commands.common.NotDefinedException)4 IHandlerService (org.eclipse.ui.handlers.IHandlerService)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 EvaluationContext (org.eclipse.core.expressions.EvaluationContext)3 IEvaluationContext (org.eclipse.core.expressions.IEvaluationContext)3 IBindingService (org.eclipse.ui.keys.IBindingService)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 TriggerSequence (org.eclipse.jface.bindings.TriggerSequence)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2