Example usage for org.eclipse.jface.bindings Binding getTriggerSequence

List of usage examples for org.eclipse.jface.bindings Binding getTriggerSequence

Introduction

In this page you can find the example usage for org.eclipse.jface.bindings Binding getTriggerSequence.

Prototype

public abstract TriggerSequence getTriggerSequence();

Source Link

Document

Returns the sequence of trigger for a given binding.

Usage

From source file:com.aptana.editor.findbar.impl.FindBarActions.java

License:Open Source License

/**
 * @return a map with the commands -> bindings available.
 *//*from  w w w.  j  av a  2  s . co  m*/
public HashMap<String, List<TriggerSequence>> getCommandToBindings() {
    HashMap<String, List<TriggerSequence>> commandToBinding = new HashMap<String, List<TriggerSequence>>();

    IWorkbenchPartSite site = textEditor.getSite();
    IBindingService service = (IBindingService) site.getService(IBindingService.class);
    Binding[] bindings = service.getBindings();

    for (int i = 0; i < bindings.length; i++) {
        Binding binding = bindings[i];
        ParameterizedCommand command = binding.getParameterizedCommand();
        if (command != null) {
            String id = command.getId();
            // Filter only the actions we decided would be active.
            //
            // Note: we don't just make all actions active because they conflict with the find bar
            // expected accelerators, so, things as Alt+W don't work -- even a second Ctrl+F isn't properly
            // treated as specified in the options.
            // A different option could be filtering those out and let everything else enabled,
            // but this would need to be throughly tested to know if corner-cases work.
            if (fCommandToHandler.containsKey(id)) {
                List<TriggerSequence> list = commandToBinding.get(id);
                if (list == null) {
                    list = new ArrayList<TriggerSequence>();
                    commandToBinding.put(id, list);
                }
                list.add(binding.getTriggerSequence());
            }

            // Uncomment to know which actions will be disabled
            // else
            // {
            // try
            // {
            // System.out.println("Command disabled: " + id + ": " + command.getName());
            // }
            // catch (NotDefinedException e)
            // {
            //
            // }
            // }
        }
    }
    return commandToBinding;
}

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

License:Open Source License

@SuppressWarnings("unused")
private void createBindingTable(Composite composite) {
    bindingViewer = new TableViewer(composite, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL | SWT.FULL_SELECTION);
    Table table = bindingViewer.getTable();
    table.setLayoutData(new GridData(550, 300));
    table.setHeaderVisible(true);/*from  ww  w  .ja  va  2s .c o  m*/
    table.setLinesVisible(true);
    commandColumn = createColumn(bindingViewer, Messages.getString("KeyPreferencePage.command"), 200); //$NON-NLS-1$
    commandLabelProvider = new ZColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            if (element instanceof Command) {
                try {
                    return ((Command) element).getName();
                } catch (NotDefinedException e) {
                    return Messages.getString("KeyPreferencePage.undefined"); //$NON-NLS-1$
                }
            }
            return element.toString();
        }

        @Override
        public Font getFont(Object element) {
            if (element instanceof Command) {
                Binding binding = commandMap.get(((Command) element).getId());
                if (binding != null && binding.getType() == Binding.USER)
                    return JFaceResources.getFont(UiConstants.ITALICFONT);
            }
            return super.getFont(element);
        }

    };
    commandColumn.setLabelProvider(commandLabelProvider);
    keyColumn = createColumn(bindingViewer, Messages.getString("KeyPreferencePage.keys"), 150); //$NON-NLS-1$
    keyLabelProvider = new ZColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            if (element instanceof Command) {
                Binding binding = commandMap.get(((Command) element).getId());
                if (binding != null)
                    return binding.getTriggerSequence().format();
                return null;
            }
            return element.toString();
        }
    };
    keyColumn.setLabelProvider(keyLabelProvider);
    catColumn = createColumn(bindingViewer, Messages.getString("KeyPreferencePage.category"), 150); //$NON-NLS-1$
    catLabelProvider = new ZColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            if (element instanceof Command)
                try {
                    return ((Command) element).getCategory().getName();
                } catch (NotDefinedException e) {
                    return Messages.getString("KeyPreferencePage.undefined"); //$NON-NLS-1$
                }
            return element.toString();
        }
    };
    catColumn.setLabelProvider(catLabelProvider);
    bindingViewer.setContentProvider(ArrayContentProvider.getInstance());
    new SortColumnManager(bindingViewer, new int[] { SWT.UP, SWT.UP, SWT.UP }, 0);
    bindingViewer.setComparator(ZViewerComparator.INSTANCE);
    bindingViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            updateDetails();
        }
    });
}

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;/*from  w  w w .j a v a2 s. c  om*/
    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   ww  w  .j a  va 2 s  .  com
    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.bdaum.zoom.ui.internal.preferences.KeyPreferencePage.java

License:Open Source License

@Override
protected void fillValues() {
    IBindingService bindingService = PlatformUI.getWorkbench().getService(IBindingService.class);
    userMap.clear();/*  w w w . ja v  a  2 s .  c o  m*/
    systemMap.clear();
    commandMap.clear();
    activeSchemeId = bindingService.getActiveScheme().getId();
    Binding[] bindings = bindingService.getBindings();
    for (Binding binding : bindings) {
        if (activeSchemeId.equals(binding.getSchemeId())) {
            TriggerSequence triggerSequence = binding.getTriggerSequence();
            if (binding.getType() == Binding.SYSTEM)
                systemMap.put(triggerSequence, binding);
            else
                userMap.put(triggerSequence, binding);
        }
    }
    updateViewer();
    ISelection selection = bindingViewer.getSelection();
    if (selection.isEmpty())
        bindingViewer.setSelection(new StructuredSelection(bindingViewer.getElementAt(0)), true);
}

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

License:Open Source License

private void updateViewer() {
    for (Binding binding : systemMap.values()) {
        ParameterizedCommand parameterizedCommand = binding.getParameterizedCommand();
        if (parameterizedCommand != null)
            commandMap.put(parameterizedCommand.getId(), binding);
    }/*from  www . j a va2  s.c o  m*/
    for (Binding binding : userMap.values()) {
        ParameterizedCommand parameterizedCommand = binding.getParameterizedCommand();
        if (parameterizedCommand != null)
            commandMap.put(parameterizedCommand.getId(), binding);
        else {
            Binding systemBinding = systemMap.get(binding.getTriggerSequence());
            if (systemBinding != null) {
                parameterizedCommand = systemBinding.getParameterizedCommand();
                if (parameterizedCommand != null)
                    commandMap.remove(parameterizedCommand.getId());
            }
        }
    }
    refreshViewer();
}

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

License:Open Source License

@Override
protected String doValidate() {
    Map<TriggerSequence, List<Binding>> conflictMap = new HashMap<>();
    for (Map.Entry<TriggerSequence, Binding> entry : systemMap.entrySet()) {
        TriggerSequence key = entry.getKey();
        Binding binding = userMap.get(key);
        if (binding == null)
            binding = entry.getValue();/*  w  ww .j  a va  2  s .c  o m*/
        if (binding.getParameterizedCommand() != null && binding.getSchemeId().equals(activeSchemeId)) {
            TriggerSequence triggerSequence = binding.getTriggerSequence();
            List<Binding> list = conflictMap.get(triggerSequence);
            if (list == null) {
                list = new ArrayList<Binding>();
                conflictMap.put(triggerSequence, list);
            }
            list.add(binding);
        }
    }
    for (Map.Entry<TriggerSequence, Binding> entry : userMap.entrySet()) {
        TriggerSequence key = entry.getKey();
        Binding userBinding = entry.getValue();
        Binding binding = systemMap.get(key);
        if (binding != null && binding.getParameterizedCommand() != null
                && userBinding.getParameterizedCommand() != null
                && !binding.getParameterizedCommand().getId()
                        .equals(userBinding.getParameterizedCommand().getId())
                && binding.getSchemeId().equals(activeSchemeId)) {
            TriggerSequence triggerSequence = binding.getTriggerSequence();
            List<Binding> list = conflictMap.get(triggerSequence);
            if (list == null) {
                list = new ArrayList<Binding>();
                conflictMap.put(triggerSequence, list);
            }
            if (!list.contains(binding))
                list.add(binding);
        }
    }
    List<Binding[]> conflicts = new ArrayList<Binding[]>();
    for (List<Binding> conflict : conflictMap.values())
        if (conflict.size() > 1)
            conflicts.add(conflict.toArray(new Binding[conflict.size()]));
    conflictViewer.setInput(conflicts);
    return conflicts.isEmpty() ? null : Messages.getString("KeyPreferencePage.there_are_conflicts"); //$NON-NLS-1$
}

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

License:Open Source License

private void updateDetails() {
    selectedCommand = (Command) bindingViewer.getStructuredSelection().getFirstElement();
    if (selectedCommand != null) {
        try {//from  w w w  . j  av a2s .  c  o  m
            String name = selectedCommand.getName();
            nameField.setText(name == null ? "" : name); //$NON-NLS-1$
            String description = selectedCommand.getDescription();
            descriptionField.setText(description == null ? "" //$NON-NLS-1$
                    : description);
            Binding binding = commandMap.get(selectedCommand.getId());
            if (binding != null) {
                TriggerSequence triggerSequence = binding.getTriggerSequence();
                if (triggerSequence instanceof KeySequence)
                    keySequenceField.setKeySequence((KeySequence) triggerSequence);
                else
                    keySequenceField.setKeySequence(null);
                userLabel.setText(
                        binding.getType() == Binding.USER ? Messages.getString("KeyPreferencePage.user_defined") //$NON-NLS-1$
                                : ""); //$NON-NLS-1$
            } else {
                keySequenceField.setKeySequence(null);
                userLabel.setText(""); //$NON-NLS-1$
            }
        } catch (NotDefinedException e) {
            // ignore
        }
    }
}

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

License:Open Source License

private Element createBindingElement(Binding binding, Document document) {
    // binding is known to have a ParameterizedCommand whose command ID matches a registered Command
    String keys = binding.getTriggerSequence().toString();
    String platform = binding.getPlatform();
    String commandName;//from  ww w .ja  v a  2 s .  c  o m
    try {
        commandName = binding.getParameterizedCommand().getName();
    } catch (NotDefinedException ex) {
        return null;
    }
    String id = keys + commandName + (platform == null ? "" : platform);
    if (knownBindings.containsKey(id)) {
        if (binding.getType() == Binding.USER) {
            // A SYSTEM binding has already been created
            return null; // do not add it again
        } else {
            // A USER binding has already been created; update its standard key binding
            Element element = knownBindings.get(id);
            element.setAttribute(XML_ATTRIBUTE_KEYS, binding.getTriggerSequence().toString());
            return null;
        }
    }
    Element element = document.createElement(XML_NODE_BINDING);
    element.setAttribute(XML_ATTRIBUTE_KEYS, keys);
    element.setAttribute(XML_ATTRIBUTE_COMMANDID, commandName);
    if (platform != null) {
        element.setAttribute(XML_ATTRIBUTE_PLATFORM, platform);
    }
    knownBindings.put(id, element);
    return element;
}

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

License:Open Source License

private Binding[] sort(Binding[] bindings) {
    Comparator<Binding> comp = new Comparator<Binding>() {
        @Override/*w ww .j  ava  2  s  .c  o m*/
        public int compare(Binding b0, Binding b1) {
            ParameterizedCommand c0 = b0.getParameterizedCommand();
            ParameterizedCommand c1 = b1.getParameterizedCommand();
            int k;
            if (c0 == null || c1 == null) {
                if (c0 != c1) {
                    k = c0 == null ? -1 : 1;
                } else {
                    k = 0;
                }
            } else {
                try {
                    k = c0.getCommand().getName().compareTo(c1.getCommand().getName());
                } catch (NotDefinedException ex) {
                    k = 0;
                }
            }
            if (k == 0) {
                String p0 = b0.getPlatform();
                if (p0 == null) {
                    p0 = XML_UNKNOWN;
                }
                String p1 = b1.getPlatform();
                if (p1 == null) {
                    p1 = XML_UNKNOWN;
                }
                k = p0.compareTo(p1);
            }
            if (k == 0) {
                k = b0.getTriggerSequence().toString().compareTo(b1.getTriggerSequence().toString());
            }
            return k;
        }
    };
    Arrays.sort(bindings, comp);
    return bindings;
}