Search in sources :

Example 1 with KeyBinding

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

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

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

the class KeyController2 method updateBindingContext.

private void updateBindingContext(ContextElement context) {
    if (context == null) {
        return;
    }
    BindingElement activeBinding = (BindingElement) bindingModel.getSelectedElement();
    if (activeBinding == null) {
        return;
    }
    String activeSchemeId = fSchemeModel.getSelectedElement().getId();
    Object obj = activeBinding.getModelObject();
    if (obj instanceof KeyBinding) {
        KeyBinding keyBinding = (KeyBinding) obj;
        if (!keyBinding.getContextId().equals(context.getId())) {
            final KeyBinding binding = new KeyBinding(keyBinding.getKeySequence(), keyBinding.getParameterizedCommand(), activeSchemeId, context.getId(), null, null, null, Binding.USER);
            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));
            }
            bindingModel.getBindingToElement().remove(activeBinding.getModelObject());
            fBindingManager.addBinding(binding);
            activeBinding.fill(binding, contextModel);
            bindingModel.getBindingToElement().put(binding, activeBinding);
        }
    }
}
Also used : KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) BindingElement(org.eclipse.ui.internal.keys.model.BindingElement)

Example 4 with KeyBinding

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

the class BindingModel2 method restoreBinding.

/**
	 * Restores the specified BindingElement. A refresh should be performed afterwards. The refresh may be done after
	 * several elements have been restored.
	 * @param element
	 */
public void restoreBinding(BindingElement element) {
    if (element == null) {
        return;
    }
    Object modelObject = element.getModelObject();
    ParameterizedCommand cmd = null;
    if (modelObject instanceof ParameterizedCommand) {
        cmd = (ParameterizedCommand) modelObject;
        TriggerSequence trigger = bindingManager.getBestActiveBindingFor(cmd.getId());
        Binding binding = bindingManager.getPerfectMatch(trigger);
        if (binding != null && binding.getType() == Binding.SYSTEM) {
            return;
        }
    } else if (modelObject instanceof KeyBinding) {
        cmd = ((KeyBinding) modelObject).getParameterizedCommand();
    }
    // Remove any USER bindings
    Binding[] managerBindings = bindingManager.getBindings();
    ArrayList systemBindings = new ArrayList();
    ArrayList removalBindings = new ArrayList();
    for (int i = 0; i < managerBindings.length; i++) {
        if (managerBindings[i].getParameterizedCommand() == null) {
            removalBindings.add(managerBindings[i]);
        } else if (managerBindings[i].getParameterizedCommand().equals(cmd)) {
            if (managerBindings[i].getType() == Binding.USER) {
                bindingManager.removeBinding(managerBindings[i]);
            } else if (managerBindings[i].getType() == Binding.SYSTEM) {
                systemBindings.add(managerBindings[i]);
            }
        }
    }
    // Clear the USER bindings for parameterized commands
    Iterator i = systemBindings.iterator();
    while (i.hasNext()) {
        Binding sys = (Binding) i.next();
        Iterator j = removalBindings.iterator();
        while (j.hasNext()) {
            Binding del = (Binding) j.next();
            if (deletes(del, sys) && del.getType() == Binding.USER) {
                bindingManager.removeBinding(del);
            }
        }
    }
    setSelectedElement(null);
    bindingElements.remove(element);
    bindingToElement.remove(modelObject);
    commandToElement.remove(modelObject);
    controller.firePropertyChange(this, PROP_BINDING_REMOVE, null, element);
}
Also used : KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) Binding(org.eclipse.jface.bindings.Binding) KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) TriggerSequence(org.eclipse.jface.bindings.TriggerSequence) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Example 5 with KeyBinding

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

Aggregations

KeyBinding (org.eclipse.jface.bindings.keys.KeyBinding)6 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)5 BindingElement (org.eclipse.ui.internal.keys.model.BindingElement)5 Binding (org.eclipse.jface.bindings.Binding)4 Iterator (java.util.Iterator)3 Collection (java.util.Collection)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 TriggerSequence (org.eclipse.jface.bindings.TriggerSequence)1 KeySequence (org.eclipse.jface.bindings.keys.KeySequence)1 ConflictModel (org.eclipse.ui.internal.keys.model.ConflictModel)1 ModelElement (org.eclipse.ui.internal.keys.model.ModelElement)1