Example usage for org.eclipse.jface.bindings.keys KeyBinding KeyBinding

List of usage examples for org.eclipse.jface.bindings.keys KeyBinding KeyBinding

Introduction

In this page you can find the example usage for org.eclipse.jface.bindings.keys KeyBinding KeyBinding.

Prototype

public KeyBinding(final KeySequence keySequence, final ParameterizedCommand command, final String schemeId,
        final String contextId, final String locale, final String platform, final String windowManager,
        final int type) 

Source Link

Document

Constructs a new instance of KeyBinding.

Usage

From source file:com.bdaum.zoom.ui.internal.preferences.KeyPreferencePage.java

License:Open Source License

private final Control createButtonBar(final Composite parent) {
    final Composite buttonBar = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(2, false);
    layout.marginWidth = 0;// w w w . j a  v  a 2 s  .  c o m
    buttonBar.setLayout(layout);
    buttonBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    final Button removeBindingButton = new Button(buttonBar, SWT.PUSH);
    removeBindingButton.setText(Messages.getString("KeyPreferencePage.remove_shortcuts")); //$NON-NLS-1$
    removeBindingButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public final void widgetSelected(final SelectionEvent event) {
            if (selectedCommand != null) {
                Binding b = commandMap.get(selectedCommand.getId());
                if (b != null) {
                    Binding userBinding = new KeyBinding((KeySequence) b.getTriggerSequence(), null,
                            activeSchemeId, b.getContextId(), null, null, null, Binding.USER);
                    userMap.put(userBinding.getTriggerSequence(), userBinding);
                    commandMap.remove(selectedCommand.getId());
                }
                refreshViewer();
                doValidate();
            }
        }
    });

    final Button restore = new Button(buttonBar, SWT.PUSH);
    restore.setText(Messages.getString("KeyPreferencePage.restore_command")); //$NON-NLS-1$
    restore.addSelectionListener(new SelectionAdapter() {
        @Override
        public final void widgetSelected(final SelectionEvent event) {
            if (selectedCommand != null) {
                String id = selectedCommand.getId();
                Binding b = commandMap.get(id);
                userMap.remove(b.getTriggerSequence());
                for (Binding sb : systemMap.values()) {
                    if (sb.getParameterizedCommand() != null
                            && sb.getParameterizedCommand().getId().equals(id)) {
                        commandMap.put(sb.getParameterizedCommand().getId(), sb);
                        break;
                    }
                }
                refreshViewer();
                doValidate();
            }
        }
    });
    return buttonBar;
}

From source file:com.bdaum.zoom.ui.internal.preferences.KeyPreferencePage.java

License:Open Source License

@SuppressWarnings("unused")
private void createDefinitionArea(Composite parent) {
    final Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    composite.setLayout(new GridLayout(2, false));
    new Label(composite, SWT.NONE).setText(Messages.getString("KeyPreferencePage.command2")); //$NON-NLS-1$
    nameField = new Text(composite, SWT.SINGLE | SWT.LEAD | SWT.BORDER | SWT.READ_ONLY);
    nameField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    new Label(composite, SWT.NONE).setText(Messages.getString("KeyPreferencePage.description")); //$NON-NLS-1$
    descriptionField = new Text(composite, SWT.MULTI | SWT.LEAD | SWT.BORDER | SWT.READ_ONLY | SWT.WRAP);
    GridData layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
    layoutData.heightHint = 50;/*from   w  ww  . j a v  a 2  s.  c o  m*/
    descriptionField.setLayoutData(layoutData);
    new Label(composite, SWT.NONE).setText(Messages.getString("KeyPreferencePage.key_sequence")); //$NON-NLS-1$
    Composite keyGroup = new Composite(composite, SWT.NONE);
    keyGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
    GridLayout layout = new GridLayout(2, false);
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    keyGroup.setLayout(layout);
    keyField = new Text(keyGroup, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
    keyField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    keySequenceField = new KeySequenceText(keyField);
    keySequenceField.setKeyStrokeLimit(2);
    keySequenceField.addPropertyChangeListener(new IPropertyChangeListener() {
        public final void propertyChange(final PropertyChangeEvent event) {
            if (!event.getOldValue().equals(event.getNewValue())) {
                final KeySequence keySequence = keySequenceField.getKeySequence();
                if (selectedCommand == null || !keySequence.isComplete())
                    return;
                boolean empty = keySequence.isEmpty();
                Binding newBinding;
                Binding b = commandMap.get(selectedCommand.getId());
                if (b != null) {
                    if (!keySequence.equals(b.getTriggerSequence())) {
                        newBinding = new KeyBinding(keySequence, empty ? null : b.getParameterizedCommand(),
                                activeSchemeId, b.getContextId(), null, null, null, Binding.USER);
                        userMap.remove(b.getTriggerSequence());
                        userMap.put(keySequence, newBinding);
                        if (empty)
                            commandMap.remove(selectedCommand.getId());
                        else
                            commandMap.put(selectedCommand.getId(), newBinding);
                    }
                } else if (!empty) {
                    ParameterizedCommand pc = new ParameterizedCommand(selectedCommand, null);
                    newBinding = new KeyBinding(keySequence, pc, activeSchemeId,
                            "org.eclipse.ui.contexts.window", //$NON-NLS-1$
                            null, null, null, Binding.USER);
                    userMap.put(keySequence, newBinding);
                    commandMap.put(selectedCommand.getId(), newBinding);
                }
                refreshViewer();
                doValidate();
                keyField.setSelection(keyField.getTextLimit());
            }
        }
    });
    final Button helpButton = new Button(keyGroup, SWT.ARROW | SWT.LEFT);
    helpButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    helpButton.setToolTipText(Messages.getString("KeyPreferencePage.add_a_special_key")); //$NON-NLS-1$
    // Arrow buttons aren't normally added to the tab list. Let's fix that.
    final Control[] tabStops = keyGroup.getTabList();
    final ArrayList<Control> newTabStops = new ArrayList<Control>();
    for (int i = 0; i < tabStops.length; i++) {
        Control tabStop = tabStops[i];
        newTabStops.add(tabStop);
        if (keyField.equals(tabStop))
            newTabStops.add(helpButton);
    }
    keyGroup.setTabList(newTabStops.toArray(new Control[newTabStops.size()]));

    // Construct the menu to attach to the above button.
    final Menu addKeyMenu = new Menu(helpButton);
    final Iterator<?> trappedKeyItr = KeySequenceText.TRAPPED_KEYS.iterator();
    while (trappedKeyItr.hasNext()) {
        final KeyStroke trappedKey = (KeyStroke) trappedKeyItr.next();
        final MenuItem menuItem = new MenuItem(addKeyMenu, SWT.PUSH);
        menuItem.setText(trappedKey.format());
        menuItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                keySequenceField.insert(trappedKey);
                keyField.setFocus();
                keyField.setSelection(keyField.getTextLimit());
            }
        });
    }
    helpButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent selectionEvent) {
            Point buttonLocation = helpButton.getLocation();
            buttonLocation = composite.toDisplay(buttonLocation.x, buttonLocation.y);
            Point buttonSize = helpButton.getSize();
            addKeyMenu.setLocation(buttonLocation.x, buttonLocation.y + buttonSize.y);
            addKeyMenu.setVisible(true);
        }
    });
    new Label(composite, SWT.NONE);
    userLabel = new Label(composite, SWT.NONE);
    userLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
}

From source file:com.google.dart.tools.ui.internal.preferences.DartKeyBindingPersistence.java

License:Open Source License

private void updateKeyBinding(Map<String, String> map) throws CoreException {
    try {/*from w w w. ja v a2  s. c o m*/
        String platform = map.get(XML_ATTRIBUTE_PLATFORM);
        String commandName = map.get(XML_ATTRIBUTE_COMMANDID);
        String stdKeys = map.get(XML_ATTRIBUTE_KEYS);
        Binding binding = findBinding(commandName, platform);
        if (binding == null) {
            return;
        }
        Command command = binding.getParameterizedCommand().getCommand();
        ParameterizedCommand cmd = new ParameterizedCommand(command, null);
        String schemeId = binding.getSchemeId();
        String contextId = binding.getContextId();
        String locale = binding.getLocale();
        String wm = null;
        int type = Binding.USER;
        KeySequence stdSeq = KeySequence.getInstance(stdKeys);
        Binding newBind = new KeyBinding(stdSeq, cmd, schemeId, contextId, locale, platform, wm, type);
        bindingManager.removeBindings(stdSeq, schemeId, contextId, null, null, null, type);
        bindingManager.addBinding(newBind);
    } catch (NotDefinedException ex) {
        throw createException(ex, ex.getMessage());
    } catch (ParseException ex) {
        throw createException(ex, ex.getMessage());
    }
}

From source file:com.google.eclipse.mechanic.core.keybinding.KeyBindings.java

License:Open Source License

private Binding createBinding(Scheme scheme, String platform, String contextId, KeySequence triggerSequence,
        ParameterizedCommand parameterizedCommand) {

    Binding newBinding = new KeyBinding(triggerSequence, parameterizedCommand, scheme.getId(), contextId, null,
            platform, null, Binding.USER);

    return newBinding;
}

From source file:com.mulgasoft.emacsplus.commands.KbdMacroBindHandler.java

License:Open Source License

/**
 * Add the binding to the Emacs+ scheme/*from w  ww  .  java 2 s .  c  om*/
 * 
 * @param editor
 * @param bindingResult
 */
private void addBinding(ITextEditor editor, IBindingResult bindingResult, String name) {
    IBindingService service = (IBindingService) editor.getSite().getService(IBindingService.class);
    if (service instanceof BindingService) {
        try {
            BindingService bindingMgr = (BindingService) service;
            if (bindingResult.getKeyBinding() != null) {
                // we're overwriting a binding, out with the old
                bindingMgr.removeBinding(bindingResult.getKeyBinding());
            }
            Command command = null;
            if (name != null) {
                ICommandService ics = (ICommandService) editor.getSite().getService(ICommandService.class);
                String id = EmacsPlusUtils.kbdMacroId(name);
                // check first, as getCommand will create it if it doesn't already exist
                if (ics.getDefinedCommandIds().contains(id)) {
                    command = ics.getCommand(id);
                }
            } else {
                // use the unexposed category
                command = nameKbdMacro(KBD_LNAME + nameid++, editor, KBD_GAZONK);
            }
            if (command != null) {
                Binding binding = new KeyBinding(bindingResult.getTrigger(),
                        new ParameterizedCommand(command, null), KBD_SCHEMEID, KBD_CONTEXTID, null, null, null,
                        Binding.USER);
                bindingMgr.addBinding(binding);
                asyncShowMessage(editor, String.format(BOUND, bindingResult.getKeyString()), false);
            } else {
                asyncShowMessage(editor, String.format(NO_NAME_UNO, name), true);
            }
        } catch (Exception e) {
            asyncShowMessage(editor, String.format(ABORT, bindingResult.getKeyString()), true);
        }
    }
}

From source file:com.mulgasoft.emacsplus.commands.KbdMacroLoadHandler.java

License:Open Source License

/**
 * Bind the loaded macro to its previous key binding, removing any conflicts
 * //from w  w  w . j  av  a2 s .  c o  m
 * @param editor
 * @param command - the new kbd macro command
 * @param sequence - key sequence for binding
 * @param previous - conflicting binding
 */
private void bindMacro(ITextEditor editor, Command command, KeySequence sequence, Binding previous) {
    if (command != null && sequence != null) {

        IBindingService service = (editor != null)
                ? (IBindingService) editor.getSite().getService(IBindingService.class)
                : (IBindingService) PlatformUI.getWorkbench().getService(IBindingService.class);
        if (service instanceof BindingService) {
            BindingService bindingMgr = (BindingService) service;
            if (previous != null) {
                bindingMgr.removeBinding(previous);
            }
            ParameterizedCommand p = new ParameterizedCommand(command, null);
            Binding binding = new KeyBinding(sequence, p, KBD_SCHEMEID, KBD_CONTEXTID, null, null, null,
                    Binding.USER);
            bindingMgr.addBinding(binding);
            // check for conflicts independent of the current Eclipse context
            checkConflicts(bindingMgr, sequence, binding);
        }
    }
}

From source file:com.mulgasoft.emacsplus.DynamicInitializer.java

License:Open Source License

/**
 * Add the bindings to the Emacs+ scheme
 *
 * @param editor/*from w  ww .  j av  a2  s  . c  o  m*/
 * @param bindingResult
 */
//   public static void addBindings(ITextEditor editor, Set<MinderBinder> bindings) {
private static void addBindings(Set<MinderBinder> bindings) {

    IBindingService service = ((IBindingService) PlatformUI.getWorkbench().getService(IBindingService.class));
    if (service instanceof BindingService) {
        BindingService bindingSvc = (BindingService) service;
        for (MinderBinder mb : bindings) {
            try {
                ICommandService ics = ((ICommandService) PlatformUI.getWorkbench()
                        .getService(ICommandService.class));
                // check first, as getCommand will create it if it doesn't already exist
                if (ics.getDefinedCommandIds().contains(mb.getCommandId())) {
                    Command cmd = ics.getCommand(mb.getCommandId());
                    if (mb.getEnhancer() != null) {
                        // enhance the pre-defined command with some Emacs+ behavior
                        cmd.addExecutionListener(mb.getEnhancer());
                    }
                    Binding binding = new KeyBinding(mb.getTrigger(), new ParameterizedCommand(cmd, null),
                            mb.getSchemeId(), mb.getContextId(), null, null, null, Binding.SYSTEM); // Binding.USER
                    // this call is scheduled for API promotion sometime (after Helios)
                    bindingSvc.addBinding(binding);
                }
            } catch (ParseException e) {
                e.printStackTrace(); // won't happen
            } catch (Exception e) {
                e.printStackTrace(); // won't happen
            }
        }
    }
}

From source file:org.brainwy.liclipsetext.shared_ui.bindings.BindKeysHelper.java

License:Open Source License

/**
 * @param force if true, we'll create the user binding regardless of having some existing binding. Otherwise,
 * we'll not allow the creation if a binding already exists for it.
 *
 * Note: conflicting bindings should be removed before (through removeUserBindingsWithFilter). If they're
 * not removed, a conflict will be created in the bindings.
 *//*from ww  w  .j  a v a2s. c  om*/
public void addUserBindings(KeySequence keySequence, ParameterizedCommand command) throws Exception {
    Scheme activeScheme = bindingService.getActiveScheme();
    String schemeId = activeScheme.getId();

    localChangeManager.addBinding(
            new KeyBinding(keySequence, command, schemeId, contextId, null, null, null, Binding.USER));

}

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/*from  w  w  w . j  ava2s . com*/
 */
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.e4.ui.bindings.internal.BindingServiceImpl.java

License:Open Source License

public Binding createBinding(TriggerSequence sequence, ParameterizedCommand command, String contextId,
        Map<String, String> attributes) {

    String schemeId = DEFAULT_SCHEME_ID;
    String locale = null;/*from   w  ww  .  j  ava2  s.  c  o  m*/
    String platform = null;
    int bindingType = Binding.SYSTEM;

    if (sequence != null && !sequence.isEmpty() && contextId != null) {
        if (attributes != null) {
            String tmp = attributes.get(SCHEME_ID_ATTR_TAG);
            if (tmp != null && tmp.length() > 0) {
                schemeId = tmp;
            }
            locale = attributes.get(LOCALE_ATTR_TAG);
            platform = attributes.get(PLATFORM_ATTR_TAG);
            if (USER_TYPE.equals(attributes.get(TYPE_ATTR_TAG))) {
                bindingType = Binding.USER;
            }
        }
        return new KeyBinding((KeySequence) sequence, command, schemeId, contextId, locale, platform, null,
                bindingType);
    }
    return null;
}