Search in sources :

Example 1 with Binding

use of org.eclipse.jface.bindings.Binding 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 2 with Binding

use of org.eclipse.jface.bindings.Binding 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 3 with Binding

use of org.eclipse.jface.bindings.Binding 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)

Example 4 with Binding

use of org.eclipse.jface.bindings.Binding in project translationstudio8 by heartsome.

the class KeyController2 method setDefaultBindings.

/**
	 * Sets the bindings to default.
	 * @param bindingService
	 * @throws NotDefinedException
	 */
public void setDefaultBindings(IBindingService bindingService, List<String> lstRemove) throws NotDefinedException {
    // Fix the scheme in the local changes.
    final String defaultSchemeId = bindingService.getDefaultSchemeId();
    final Scheme defaultScheme = fBindingManager.getScheme(defaultSchemeId);
    try {
        fBindingManager.setActiveScheme(defaultScheme);
    } catch (final NotDefinedException e) {
    // At least we tried....
    }
    // Restore any User defined bindings
    Binding[] bindings = fBindingManager.getBindings();
    for (int i = 0; i < bindings.length; i++) {
        ParameterizedCommand pCommand = bindings[i].getParameterizedCommand();
        String commandId = null;
        if (pCommand != null) {
            commandId = pCommand.getCommand().getId();
        }
        if (bindings[i].getType() == Binding.USER || (commandId != null && lstRemove.contains(commandId))) {
            fBindingManager.removeBinding(bindings[i]);
        }
    }
    bindingModel.refresh(contextModel, lstRemove);
    saveBindings(bindingService);
}
Also used : KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) Binding(org.eclipse.jface.bindings.Binding) Scheme(org.eclipse.jface.bindings.Scheme) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Example 5 with Binding

use of org.eclipse.jface.bindings.Binding in project translationstudio8 by heartsome.

the class BindingModel2 method copy.

/**
	 * Makes a copy of the
	 * @param element
	 */
public void copy(BindingElement element) {
    if (element == null || !(element.getModelObject() instanceof Binding)) {
        return;
    }
    BindingElement be = new BindingElement(controller);
    ParameterizedCommand parameterizedCommand = ((Binding) element.getModelObject()).getParameterizedCommand();
    be.init(parameterizedCommand);
    be.setParent(this);
    bindingElements.add(be);
    commandToElement.put(parameterizedCommand, be);
    controller.firePropertyChange(this, PROP_BINDING_ADD, null, be);
    setSelectedElement(be);
}
Also used : KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) Binding(org.eclipse.jface.bindings.Binding) BindingElement(org.eclipse.ui.internal.keys.model.BindingElement) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Aggregations

Binding (org.eclipse.jface.bindings.Binding)16 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)12 KeyBinding (org.eclipse.jface.bindings.keys.KeyBinding)9 BindingElement (org.eclipse.ui.internal.keys.model.BindingElement)9 ArrayList (java.util.ArrayList)5 Collection (java.util.Collection)5 Iterator (java.util.Iterator)5 IBindingService (org.eclipse.ui.keys.IBindingService)5 NotDefinedException (org.eclipse.core.commands.common.NotDefinedException)4 TriggerSequence (org.eclipse.jface.bindings.TriggerSequence)4 HashMap (java.util.HashMap)3 Command (org.eclipse.core.commands.Command)3 ICommandService (org.eclipse.ui.commands.ICommandService)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 IWorkbench (org.eclipse.ui.IWorkbench)2 BindingService (org.eclipse.ui.internal.keys.BindingService)2 ModelElement (org.eclipse.ui.internal.keys.model.ModelElement)2