Example usage for org.eclipse.jface.bindings BindingManager addBinding

List of usage examples for org.eclipse.jface.bindings BindingManager addBinding

Introduction

In this page you can find the example usage for org.eclipse.jface.bindings BindingManager addBinding.

Prototype

public void addBinding(final Binding binding) 

Source Link

Document

Adds a single new binding to the existing array of bindings.

Usage

From source file:org.csstudio.iter.css.product.util.WorkbenchUtil.java

License:Open Source License

/**
 * Unbind F11 KeyBinding of org.eclipse.debug.ui to avoid conflict with
 * org.csstudio.opibuilder plugin/*w  w  w . ja  va 2s  .  c om*/
 */
public static void unbindDebugLast() {
    IBindingService bindingService = (IBindingService) PlatformUI.getWorkbench()
            .getAdapter(IBindingService.class);
    BindingManager localChangeManager = new BindingManager(new ContextManager(), new CommandManager());

    final Scheme[] definedSchemes = bindingService.getDefinedSchemes();
    try {
        for (int i = 0; i < definedSchemes.length; i++) {
            final Scheme scheme = definedSchemes[i];
            final Scheme copy = localChangeManager.getScheme(scheme.getId());
            copy.define(scheme.getName(), scheme.getDescription(), scheme.getParentId());
        }
        localChangeManager.setActiveScheme(bindingService.getActiveScheme());
    } catch (final NotDefinedException e) {
        e.printStackTrace();
    }
    localChangeManager.setLocale(bindingService.getLocale());
    localChangeManager.setPlatform(bindingService.getPlatform());
    localChangeManager.setBindings(bindingService.getBindings());

    KeyBinding opiFullScreenBinding = null;
    int nbBinding = 0;

    Binding[] bArray = bindingService.getBindings();
    if (bArray != null) {
        for (Binding binding : bArray) {
            if (binding instanceof KeyBinding) {
                KeyBinding kBind = (KeyBinding) binding;
                if (kBind.getParameterizedCommand() != null
                        && kBind.getParameterizedCommand().getCommand() != null) {
                    if ("org.eclipse.debug.ui.commands.DebugLast"
                            .equals(kBind.getParameterizedCommand().getCommand().getId())) {
                        KeySequence triggerSequence = kBind.getKeySequence();
                        String contextId = kBind.getContextId();
                        String schemeId = kBind.getSchemeId();
                        KeyBinding deleteBinding = new KeyBinding(triggerSequence, null, schemeId, contextId,
                                null, null, null, Binding.USER);
                        localChangeManager.addBinding(deleteBinding);
                        try {
                            bindingService.savePreferences(localChangeManager.getActiveScheme(),
                                    localChangeManager.getBindings());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else if ("org.csstudio.opibuilder.actions.fullscreen"
                            .equals(kBind.getParameterizedCommand().getCommand().getId())) {
                        if (opiFullScreenBinding == null)
                            opiFullScreenBinding = kBind;
                        nbBinding++;
                    }
                }
            }
        }
    }

    // Rebind OPI runner full screen command if it exists only one time
    if (nbBinding == 1) {
        KeySequence triggerSequence = opiFullScreenBinding.getKeySequence();
        String contextId = opiFullScreenBinding.getContextId();
        String schemeId = opiFullScreenBinding.getSchemeId();

        KeyBinding updateBinding = new KeyBinding(triggerSequence,
                opiFullScreenBinding.getParameterizedCommand(), schemeId, contextId, null, null, null,
                Binding.USER);
        localChangeManager.addBinding(updateBinding);
        try {
            bindingService.savePreferences(localChangeManager.getActiveScheme(),
                    localChangeManager.getBindings());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.eclipse.ui.internal.keys.BindingPersistence.java

License:Open Source License

/**
 * Reads all of the binding definitions from the preferences.
 * //from ww w .  j ava2 s  . c o  m
 * @param preferences
 *            The memento for the commands preferences key.
 * @param bindingManager
 *            The binding manager to which the bindings should be added;
 *            must not be <code>null</code>.
 * @param commandService
 *            The command service for the workbench; must not be
 *            <code>null</code>.
 */
private static final void readBindingsFromPreferences(final IMemento preferences,
        final BindingManager bindingManager, final CommandManager commandService) {
    List warningsToLog = new ArrayList(1);

    if (preferences != null) {
        final IMemento[] preferenceMementos = preferences.getChildren(TAG_KEY_BINDING);
        int preferenceMementoCount = preferenceMementos.length;
        for (int i = preferenceMementoCount - 1; i >= 0; i--) {
            final IMemento memento = preferenceMementos[i];

            // Read out the command id.
            String commandId = readOptional(memento, ATT_COMMAND_ID);
            if (commandId == null) {
                commandId = readOptional(memento, ATT_COMMAND);
            }
            String viewParameter = null;
            final Command command;
            if (commandId != null) {
                command = commandService.getCommand(commandId);
            } else {
                command = null;
            }

            // Read out the scheme id.
            String schemeId = readOptional(memento, ATT_KEY_CONFIGURATION_ID);
            if (schemeId == null) {
                schemeId = readRequired(memento, ATT_CONFIGURATION, warningsToLog,
                        "Key bindings need a scheme or key configuration"); //$NON-NLS-1$
                if (schemeId == null) {
                    continue;
                }
            }

            // Read out the context id.
            String contextId = readOptional(memento, ATT_CONTEXT_ID);
            if (contextId == null) {
                contextId = readOptional(memento, ATT_SCOPE);
            }
            if (LEGACY_DEFAULT_SCOPE.equals(contextId)) {
                contextId = null;
            }
            if (contextId == null) {
                contextId = IContextIds.CONTEXT_ID_WINDOW;
            }

            // Read out the key sequence.
            String keySequenceText = readOptional(memento, ATT_KEY_SEQUENCE);
            KeySequence keySequence = null;
            if (keySequenceText == null) {
                keySequenceText = readRequired(memento, ATT_STRING, warningsToLog,
                        "Key bindings need a key sequence or string"); //$NON-NLS-1$
                if (keySequenceText == null) {
                    continue;
                }

                // The key sequence is in the old-style format.
                keySequence = convert2_1Sequence(parse2_1Sequence(keySequenceText));

            } else {
                // The key sequence is in the new-style format.
                try {
                    keySequence = KeySequence.getInstance(keySequenceText);
                } catch (final ParseException e) {
                    addWarning(warningsToLog, "Could not parse", null, //$NON-NLS-1$
                            commandId, "keySequence", keySequenceText); //$NON-NLS-1$
                    continue;
                }
                if (keySequence.isEmpty() || !keySequence.isComplete()) {
                    addWarning(warningsToLog, "Key bindings cannot use an empty or incomplete key sequence", //$NON-NLS-1$
                            null, commandId, "keySequence", keySequence //$NON-NLS-1$
                                    .toString());
                    continue;
                }

            }

            // Read out the locale and platform.
            final String locale = readOptional(memento, ATT_LOCALE);
            final String platform = readOptional(memento, ATT_PLATFORM);

            // Read out the parameters
            final ParameterizedCommand parameterizedCommand;
            if (command == null) {
                parameterizedCommand = null;
            } else if (viewParameter != null) {
                HashMap parms = new HashMap();
                parms.put(ShowViewMenu.VIEW_ID_PARM, viewParameter);
                parameterizedCommand = ParameterizedCommand.generateCommand(command, parms);
            } else {
                parameterizedCommand = readParameters(memento, warningsToLog, command);
            }

            final Binding binding = new KeyBinding(keySequence, parameterizedCommand, schemeId, contextId,
                    locale, platform, null, Binding.USER);
            bindingManager.addBinding(binding);
        }
    }

    // If there were any warnings, then log them now.
    logWarnings(warningsToLog, "Warnings while parsing the key bindings from the preference store"); //$NON-NLS-1$
}

From source file:org.limy.eclipse.qalab.action.KeyBindAction.java

License:Open Source License

@Override
public void doRun(ISelection selection, IProgressMonitor monitor) throws CoreException {

    IBindingService bindingService = (IBindingService) getWindow().getWorkbench()
            .getService(IBindingService.class);

    KeybindWindow keyWindow = new KeybindWindow(getWindow().getShell(), bindingService);
    keyWindow.setBlockOnOpen(true);//  w ww  . j a  v  a  2  s . c  om
    keyWindow.open();
    Map<Binding, KeySequence> results = keyWindow.getResults();
    if (results != null) {

        BindingManager localChangeManager;
        try {
            localChangeManager = createLocalManager(bindingService);
        } catch (NotDefinedException e) {
            throw new WorkbenchException(e.getMessage(), e);
        }
        Binding[] bindings = bindingService.getBindings();

        for (Entry<Binding, KeySequence> entry : results.entrySet()) {
            Binding systemBinding = entry.getKey();
            String contextId = systemBinding.getContextId();
            String schemeId = systemBinding.getSchemeId();
            String commandId = systemBinding.getParameterizedCommand().getId();

            // L?[oCh??
            for (Binding binding : bindings) {
                ParameterizedCommand command = binding.getParameterizedCommand();

                if (command != null && commandId.equals(command.getId()) && binding.getType() == Binding.USER) {

                    localChangeManager.removeBindings(binding.getTriggerSequence(), schemeId, contextId, null,
                            null, null, Binding.USER);
                }
            }

            // L?[oCho^
            final ParameterizedCommand command = systemBinding.getParameterizedCommand();
            localChangeManager.addBinding(new KeyBinding(entry.getValue(), command, schemeId, contextId, null,
                    null, null, Binding.USER));

        }

        try {
            bindingService.savePreferences(localChangeManager.getActiveScheme(),
                    localChangeManager.getBindings());
        } catch (IOException e) {
            LimyEclipsePluginUtils.log(e);
        }

    }

}

From source file:org.vimplugin.editors.VimEditorPartListener.java

License:Open Source License

private void disableKeys() {
    if (!keysDisabled) {
        logger.debug("Disabling conflicting keybindings while vim editor is focused: " + Arrays.toString(keys));
        BindingManager localChangeManager = getLocalChangeManager();
        String schemeId = localChangeManager.getActiveScheme().getId();
        for (KeySequence keySequence : keySequences) {
            for (String contextId : CONTEXT_IDS) {
                localChangeManager.removeBindings(keySequence, schemeId, contextId, null, null, null,
                        Binding.USER);
                localChangeManager.addBinding(
                        new KeyBinding(keySequence, null, schemeId, contextId, null, null, null, Binding.USER));
            }/*from  ww w.j a  v a2s .  c om*/
        }
        keysDisabled = true;
        saveKeyChanges(localChangeManager);
    }
}