Search in sources :

Example 1 with ParameterizedCommand

use of org.eclipse.core.commands.ParameterizedCommand in project dbeaver by serge-rider.

the class ActionUtils method findCommandDescription.

@Nullable
public static String findCommandDescription(String commandId, IServiceLocator serviceLocator, boolean shortcutOnly) {
    String commandName = null;
    String shortcut = null;
    ICommandService commandService = serviceLocator.getService(ICommandService.class);
    if (commandService != null) {
        Command command = commandService.getCommand(commandId);
        if (command != null && command.isDefined()) {
            try {
                commandName = command.getName();
            } catch (NotDefinedException e) {
                log.debug(e);
            }
        }
    }
    IBindingService bindingService = serviceLocator.getService(IBindingService.class);
    if (bindingService != null) {
        TriggerSequence sequence = null;
        for (Binding b : bindingService.getBindings()) {
            ParameterizedCommand parameterizedCommand = b.getParameterizedCommand();
            if (parameterizedCommand != null && commandId.equals(parameterizedCommand.getId())) {
                sequence = b.getTriggerSequence();
            }
        }
        if (sequence == null) {
            sequence = bindingService.getBestActiveBindingFor(commandId);
        }
        if (sequence != null) {
            shortcut = sequence.format();
        }
    }
    if (shortcutOnly) {
        return shortcut == null ? "?" : shortcut;
    }
    if (shortcut == null) {
        return commandName;
    }
    if (commandName == null) {
        return shortcut;
    }
    return commandName + " (" + shortcut + ")";
}
Also used : Binding(org.eclipse.jface.bindings.Binding) TriggerSequence(org.eclipse.jface.bindings.TriggerSequence) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) IBindingService(org.eclipse.ui.keys.IBindingService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ICommandService(org.eclipse.ui.commands.ICommandService) Nullable(org.jkiss.code.Nullable)

Example 2 with ParameterizedCommand

use of org.eclipse.core.commands.ParameterizedCommand in project dbeaver by serge-rider.

the class ActionUtils method runCommand.

public static void runCommand(String commandId, ISelection selection, 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;
                            }
                        }
                    }
                }
                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, null);
                    handlerService.executeCommandInContext(pc, null, context);
                } else if (command != null && command.isEnabled()) {
                    handlerService.executeCommand(commandId, null);
                }
            }
        } catch (Exception e) {
            log.error("Can't execute command '" + commandId + "'", e);
        }
    }
}
Also used : 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) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) EvaluationContext(org.eclipse.core.expressions.EvaluationContext) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ICommandService(org.eclipse.ui.commands.ICommandService) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException)

Example 3 with ParameterizedCommand

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

the class KeysPreferencePage method applyData.

public void applyData(Object data) {
    // }
    if (data instanceof ModelElement) {
        model.setSelectedElement((ModelElement) data);
    }
    if (data instanceof Binding && fFilteredTree != null) {
        BindingElement be = (BindingElement) model.getBindingToElement().get(data);
        fFilteredTree.getViewer().setSelection(new StructuredSelection(be), true);
    }
    if (data instanceof ParameterizedCommand) {
        Map commandToElement = model.getCommandToElement();
        BindingElement be = (BindingElement) commandToElement.get(data);
        if (be != null) {
            fFilteredTree.getViewer().setSelection(new StructuredSelection(be), true);
        }
    }
}
Also used : Binding(org.eclipse.jface.bindings.Binding) ModelElement(org.eclipse.ui.internal.keys.model.ModelElement) BindingElement(org.eclipse.ui.internal.keys.model.BindingElement) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Map(java.util.Map)

Example 4 with ParameterizedCommand

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

the class BindingModel2 method refresh.

/**
	 * Refreshes the binding model to be in sync with the {@link BindingManager}.
	 * @param contextModel
	 */
public void refresh(ContextModel contextModel, List<String> lstRemove) {
    Set cmdsForBindings = new HashSet();
    Iterator<BindingElement> iterator = bindingElements.iterator();
    while (iterator.hasNext()) {
        BindingElement bindingElement = iterator.next();
        if (lstRemove.contains(bindingElement.getId())) {
            iterator.remove();
        }
    }
    Iterator<Entry<Binding, BindingElement>> it = bindingToElement.entrySet().iterator();
    while (it.hasNext()) {
        Entry<Binding, BindingElement> entry = (Entry<Binding, BindingElement>) it.next();
        if (lstRemove.contains(entry.getValue().getId())) {
            it.remove();
        }
    }
    Collection activeManagerBindings = bindingManager.getActiveBindingsDisregardingContextFlat();
    // add any bindings that we don't already have.
    Iterator i = activeManagerBindings.iterator();
    Map<String, BindingElement> temp = new HashMap<String, BindingElement>();
    while (i.hasNext()) {
        KeyBinding b = (KeyBinding) i.next();
        ParameterizedCommand parameterizedCommand = b.getParameterizedCommand();
        cmdsForBindings.add(parameterizedCommand);
        if (!bindingToElement.containsKey(b)) {
            BindingElement be = new BindingElement(controller);
            be.init(b, contextModel);
            be.setParent(this);
            bindingElements.add(be);
            bindingToElement.put(b, be);
            controller.firePropertyChange(this, PROP_BINDING_ADD, null, be);
            // 去掉添加重复的情况
            temp.put(parameterizedCommand.getId(), be);
            if (commandToElement.containsKey(parameterizedCommand) && be.getUserDelta().intValue() == Binding.SYSTEM) {
                Object remove = commandToElement.remove(parameterizedCommand);
                bindingElements.remove(remove);
                controller.firePropertyChange(this, PROP_BINDING_REMOVE, null, remove);
            }
        }
    }
    // 修改在恢复默认设置,出现两个相同的选项	
    i = bindingElements.iterator();
    while (i.hasNext()) {
        BindingElement be = (BindingElement) i.next();
        Object obj = be.getModelObject();
        if (temp.containsKey(be.getId())) {
            if (!(obj instanceof Binding)) {
                System.out.println("remove");
                i.remove();
                controller.firePropertyChange(this, PROP_BINDING_REMOVE, null, be);
            }
        }
    }
    // remove bindings that shouldn't be there
    i = bindingElements.iterator();
    while (i.hasNext()) {
        BindingElement be = (BindingElement) i.next();
        Object obj = be.getModelObject();
        if (obj instanceof Binding) {
            Binding b = (Binding) obj;
            if (!activeManagerBindings.contains(b) || lstRemove.contains(be.getId())) {
                be.fill(b.getParameterizedCommand());
                bindingToElement.remove(b);
                i.remove();
                controller.firePropertyChange(this, PROP_BINDING_REMOVE, null, be);
            }
        } else {
            cmdsForBindings.add(obj);
        }
    }
    // If we removed the last binding for a parameterized command,
    // put back the CMD
    i = allParameterizedCommands.iterator();
    while (i.hasNext()) {
        ParameterizedCommand cmd = (ParameterizedCommand) i.next();
        if (!cmdsForBindings.contains(cmd)) {
            BindingElement be = new BindingElement(controller);
            be.init(cmd);
            be.setParent(this);
            if (lstRemove.contains(be.getId())) {
                continue;
            }
            bindingElements.add(be);
            commandToElement.put(cmd, be);
            controller.firePropertyChange(this, PROP_BINDING_ADD, null, be);
        }
    }
}
Also used : KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) Binding(org.eclipse.jface.bindings.Binding) HashSet(java.util.HashSet) Set(java.util.Set) BindingElement(org.eclipse.ui.internal.keys.model.BindingElement) HashMap(java.util.HashMap) KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) Entry(java.util.Map.Entry) Iterator(java.util.Iterator) Collection(java.util.Collection) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) HashSet(java.util.HashSet)

Example 5 with ParameterizedCommand

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

the class KeyController2 method updateTrigger.

/**
	 * @param activeBinding
	 * @param oldSequence
	 * @param keySequence
	 */
public void updateTrigger(BindingElement activeBinding, KeySequence oldSequence, KeySequence keySequence) {
    if (activeBinding == null) {
        return;
    }
    Object obj = activeBinding.getModelObject();
    if (obj instanceof KeyBinding) {
        KeyBinding keyBinding = (KeyBinding) obj;
        if (!keyBinding.getKeySequence().equals(keySequence)) {
            if (keySequence != null && !keySequence.isEmpty()) {
                String activeSchemeId = fSchemeModel.getSelectedElement().getId();
                ModelElement selectedElement = contextModel.getSelectedElement();
                String activeContextId = selectedElement == null ? IContextService.CONTEXT_ID_WINDOW : selectedElement.getId();
                final KeyBinding binding = new KeyBinding(keySequence, keyBinding.getParameterizedCommand(), activeSchemeId, activeContextId, null, null, null, Binding.USER);
                Map bindingToElement = bindingModel.getBindingToElement();
                bindingToElement.remove(keyBinding);
                if (keyBinding.getType() == Binding.USER) {
                    fBindingManager.removeBinding(keyBinding);
                } else {
                    fBindingManager.addBinding(new KeyBinding(keyBinding.getKeySequence(), null, keyBinding.getSchemeId(), keyBinding.getContextId(), null, null, null, Binding.USER));
                }
                fBindingManager.addBinding(binding);
                activeBinding.fill(binding, contextModel);
                bindingModel.getBindingToElement().put(binding, activeBinding);
                // Remove binding for any system conflicts
                Object[] keys = bindingToElement.keySet().toArray();
                for (int i = 0; i < keys.length; i++) {
                    Binding bindingKey = (Binding) keys[i];
                    if (oldSequence.equals(bindingKey.getTriggerSequence()) && bindingKey.getType() == Binding.SYSTEM) {
                        BindingElement be = (BindingElement) bindingToElement.get(bindingKey);
                        bindingModel.remove(be);
                    }
                }
                bindingModel.setSelectedElement(activeBinding);
            } else {
                bindingModel.getBindingToElement().remove(keyBinding);
                if (keyBinding.getType() == Binding.USER) {
                    fBindingManager.removeBinding(keyBinding);
                } else {
                    fBindingManager.addBinding(new KeyBinding(keyBinding.getKeySequence(), null, keyBinding.getSchemeId(), keyBinding.getContextId(), null, null, null, Binding.USER));
                }
                activeBinding.fill(keyBinding.getParameterizedCommand());
            }
        }
    } else if (obj instanceof ParameterizedCommand) {
        ParameterizedCommand cmd = (ParameterizedCommand) obj;
        if (keySequence != null && !keySequence.isEmpty()) {
            String activeSchemeId = fSchemeModel.getSelectedElement().getId();
            ModelElement selectedElement = contextModel.getSelectedElement();
            String activeContextId = selectedElement == null ? IContextService.CONTEXT_ID_WINDOW : selectedElement.getId();
            final KeyBinding binding = new KeyBinding(keySequence, cmd, activeSchemeId, activeContextId, null, null, null, Binding.USER);
            fBindingManager.addBinding(binding);
            activeBinding.fill(binding, contextModel);
            bindingModel.getBindingToElement().put(binding, activeBinding);
        }
    }
}
Also used : KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) Binding(org.eclipse.jface.bindings.Binding) KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) ModelElement(org.eclipse.ui.internal.keys.model.ModelElement) BindingElement(org.eclipse.ui.internal.keys.model.BindingElement) Map(java.util.Map) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Aggregations

ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)13 Binding (org.eclipse.jface.bindings.Binding)11 KeyBinding (org.eclipse.jface.bindings.keys.KeyBinding)9 BindingElement (org.eclipse.ui.internal.keys.model.BindingElement)7 Iterator (java.util.Iterator)4 NotDefinedException (org.eclipse.core.commands.common.NotDefinedException)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 Command (org.eclipse.core.commands.Command)3 ICommandService (org.eclipse.ui.commands.ICommandService)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 IWorkbench (org.eclipse.ui.IWorkbench)2 BindingService (org.eclipse.ui.internal.keys.BindingService)2 ModelElement (org.eclipse.ui.internal.keys.model.ModelElement)2