Search in sources :

Example 11 with EBindingService

use of org.eclipse.e4.ui.bindings.EBindingService in project eclipse.platform.ui by eclipse-platform.

the class BindingLookupTest method testLookupBestShortcut.

@Test
public void testLookupBestShortcut() {
    ECommandService cs = workbenchContext.get(ECommandService.class);
    ParameterizedCommand cmd = cs.createCommand(TEST_ID1, null);
    EBindingService bs = workbenchContext.get(EBindingService.class);
    TriggerSequence seq2 = bs.createSequence("ALT+5 X");
    Binding db2 = createDefaultBinding(bs, seq2, cmd, ID_DIALOG_AND_WINDOW);
    bs.activateBinding(db2);
    TriggerSequence seq = bs.createSequence("CTRL+5 T");
    Binding db = createDefaultBinding(bs, seq, cmd, ID_DIALOG_AND_WINDOW);
    bs.activateBinding(db);
    TriggerSequence foundSequence = bs.getBestSequenceFor(cmd);
    assertNotNull(foundSequence);
    assertEquals(seq, foundSequence);
}
Also used : Binding(org.eclipse.jface.bindings.Binding) TriggerSequence(org.eclipse.jface.bindings.TriggerSequence) EBindingService(org.eclipse.e4.ui.bindings.EBindingService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ECommandService(org.eclipse.e4.core.commands.ECommandService) Test(org.junit.Test)

Example 12 with EBindingService

use of org.eclipse.e4.ui.bindings.EBindingService in project eclipse.platform.ui by eclipse-platform.

the class KeyController method loadModelBackend.

private static BindingManager loadModelBackend(IServiceLocator locator) {
    IBindingService bindingService = locator.getService(IBindingService.class);
    BindingManager bindingManager = new BindingManager(new ContextManager(), new CommandManager());
    final Scheme[] definedSchemes = bindingService.getDefinedSchemes();
    try {
        Scheme modelActiveScheme = null;
        for (final Scheme scheme : definedSchemes) {
            final Scheme copy = bindingManager.getScheme(scheme.getId());
            copy.define(scheme.getName(), scheme.getDescription(), scheme.getParentId());
            if (scheme.getId().equals(bindingService.getActiveScheme().getId())) {
                modelActiveScheme = copy;
            }
        }
        bindingManager.setActiveScheme(modelActiveScheme);
    } catch (final NotDefinedException e) {
        StatusManager.getManager().handle(new Status(IStatus.WARNING, WorkbenchPlugin.PI_WORKBENCH, "Keys page found an undefined scheme", // $NON-NLS-1$
        e));
    }
    bindingManager.setLocale(bindingService.getLocale());
    bindingManager.setPlatform(bindingService.getPlatform());
    Set<Binding> bindings = new HashSet<>();
    EBindingService eBindingService = locator.getService(EBindingService.class);
    bindings.addAll(eBindingService.getActiveBindings());
    bindings.addAll(Arrays.asList(bindingService.getBindings()));
    bindingManager.setBindings(bindings.toArray(new Binding[0]));
    return bindingManager;
}
Also used : BindingManager(org.eclipse.jface.bindings.BindingManager) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) Binding(org.eclipse.jface.bindings.Binding) CommandManager(org.eclipse.core.commands.CommandManager) Scheme(org.eclipse.jface.bindings.Scheme) EBindingService(org.eclipse.e4.ui.bindings.EBindingService) ContextManager(org.eclipse.core.commands.contexts.ContextManager) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) IBindingService(org.eclipse.ui.keys.IBindingService) HashSet(java.util.HashSet)

Example 13 with EBindingService

use of org.eclipse.e4.ui.bindings.EBindingService in project eclipse.platform.ui by eclipse-platform.

the class BindingPersistenceTest method testAboutBindingIn3x.

@Test
public void testAboutBindingIn3x() throws Exception {
    if (Util.isMac()) {
        // TODO investigate on Mac
        return;
    }
    final Scheme activeScheme = bindingService.getActiveScheme();
    ParameterizedCommand aboutCmd = new ParameterizedCommand(commandService.getCommand(IWorkbenchCommandConstants.HELP_ABOUT), null);
    ParameterizedCommand activateEditorCmd = new ParameterizedCommand(commandService.getCommand(IWorkbenchCommandConstants.WINDOW_ACTIVATE_EDITOR), null);
    final KeySequence keyF12 = Util.isMac() ? KeySequence.getInstance("M1+F12") : KeySequence.getInstance("F12");
    final Binding editorBinding = bindingService.getPerfectMatch(keyF12);
    assertNotNull(editorBinding);
    assertEquals(activateEditorCmd, editorBinding.getParameterizedCommand());
    EBindingService ebs = PlatformUI.getWorkbench().getService(EBindingService.class);
    HashMap<String, String> attrs = new HashMap<>();
    attrs.put(EBindingService.TYPE_ATTR_TAG, "user");
    final Binding localAboutBinding = ebs.createBinding(keyF12, aboutCmd, IContextService.CONTEXT_ID_WINDOW, attrs);
    assertEquals(Binding.USER, localAboutBinding.getType());
    final Binding[] bindings = bindingService.getBindings();
    Binding[] added = new Binding[bindings.length + 1];
    System.arraycopy(bindings, 0, added, 0, bindings.length);
    added[bindings.length] = localAboutBinding;
    bindingService.savePreferences(activeScheme, added);
    final Binding secondMatch = bindingService.getPerfectMatch(keyF12);
    // fails
    assertNotNull(secondMatch);
    assertEquals(aboutCmd, secondMatch.getParameterizedCommand());
}
Also used : KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) Binding(org.eclipse.jface.bindings.Binding) Scheme(org.eclipse.jface.bindings.Scheme) EBindingService(org.eclipse.e4.ui.bindings.EBindingService) HashMap(java.util.HashMap) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) KeySequence(org.eclipse.jface.bindings.keys.KeySequence) Test(org.junit.Test)

Example 14 with EBindingService

use of org.eclipse.e4.ui.bindings.EBindingService in project eclipse.platform.ui by eclipse-platform.

the class BindingPersistenceTest method testAboutBinding.

@Test
public void testAboutBinding() throws Exception {
    if (Util.isMac()) {
        return;
    }
    final Scheme activeScheme = bindingService.getActiveScheme();
    final Binding[] originalBindings = bindingService.getBindings();
    ParameterizedCommand aboutCmd = new ParameterizedCommand(commandService.getCommand(IWorkbenchCommandConstants.HELP_ABOUT), null);
    ParameterizedCommand activateEditorCmd = new ParameterizedCommand(commandService.getCommand(IWorkbenchCommandConstants.WINDOW_ACTIVATE_EDITOR), null);
    final KeySequence keyF12 = Util.isMac() ? KeySequence.getInstance("M1+F12") : KeySequence.getInstance("F12");
    final KeySequence keyAltCtrlShiftI = KeySequence.getInstance("ALT+CTRL+SHIFT+I");
    final Binding editorBinding = bindingService.getPerfectMatch(keyF12);
    assertNotNull(editorBinding);
    assertEquals(activateEditorCmd, editorBinding.getParameterizedCommand());
    EBindingService ebs = PlatformUI.getWorkbench().getService(EBindingService.class);
    HashMap<String, String> attrs = new HashMap<>();
    attrs.put(EBindingService.TYPE_ATTR_TAG, "user");
    final Binding localAboutBinding = ebs.createBinding(keyF12, aboutCmd, IContextService.CONTEXT_ID_WINDOW, attrs);
    assertEquals(Binding.USER, localAboutBinding.getType());
    // test unbinding a system binding and adding a user binding (same
    // triggers and context)
    final Binding[] bindings = originalBindings;
    Binding[] added = new Binding[bindings.length + 2];
    System.arraycopy(bindings, 0, added, 0, bindings.length);
    Binding del = new KeyBinding(keyF12, null, IBindingService.DEFAULT_DEFAULT_ACTIVE_SCHEME_ID, IContextService.CONTEXT_ID_WINDOW, null, null, null, Binding.USER);
    added[bindings.length] = del;
    added[bindings.length + 1] = localAboutBinding;
    bindingService.savePreferences(activeScheme, added);
    // the match should be the user binding that we just added
    final Binding secondMatch = bindingService.getPerfectMatch(keyF12);
    assertNotNull(secondMatch);
    assertEquals(aboutCmd, secondMatch.getParameterizedCommand());
    // go back to the defaults
    bindingService.savePreferences(activeScheme, originalBindings);
    final Binding thirdMatch = bindingService.getPerfectMatch(keyF12);
    assertNotNull(thirdMatch);
    assertEquals(activateEditorCmd, thirdMatch.getParameterizedCommand());
    // try assigning alt-ctrl-shift-i (no other binding uses this for this
    // context) to the 'about' command
    final Binding localAboutBinding1 = ebs.createBinding(keyAltCtrlShiftI, aboutCmd, IContextService.CONTEXT_ID_WINDOW, attrs);
    assertEquals(Binding.USER, localAboutBinding1.getType());
    Binding[] added1 = new Binding[bindings.length + 1];
    System.arraycopy(bindings, 0, added1, 0, bindings.length);
    added1[bindings.length] = localAboutBinding1;
    bindingService.savePreferences(activeScheme, added1);
    final Binding fourthMatch = bindingService.getPerfectMatch(keyAltCtrlShiftI);
    assertNotNull(fourthMatch);
    assertEquals(aboutCmd, fourthMatch.getParameterizedCommand());
}
Also used : KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) Binding(org.eclipse.jface.bindings.Binding) KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) Scheme(org.eclipse.jface.bindings.Scheme) EBindingService(org.eclipse.e4.ui.bindings.EBindingService) HashMap(java.util.HashMap) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) KeySequence(org.eclipse.jface.bindings.keys.KeySequence) Test(org.junit.Test)

Example 15 with EBindingService

use of org.eclipse.e4.ui.bindings.EBindingService in project eclipse.platform.ui by eclipse-platform.

the class GlobalKeyAssistDialog method getInfoText.

/**
 * Determines what keybinding was used to open this dialog and returns an info
 * string using that binding. ex: "Press 'Ctrl-Shift-L') to open the preference
 * page";
 *
 * @return info text for this dialog
 */
private String getInfoText() {
    ECommandService commandService = context.getActiveLeaf().get(ECommandService.class);
    Command cmd = commandService.getCommand(IWorkbenchCommandConstants.WINDOW_SHOW_KEY_ASSIST);
    if (cmd != null) {
        EBindingService bindingService = context.getActiveLeaf().get(EBindingService.class);
        TriggerSequence keySeq = bindingService.getBestSequenceFor(new ParameterizedCommand(cmd, null));
        if (keySeq != null) {
            return NLS.bind(KeyAssistMessages.openPreferencePage, keySeq.format());
        }
    }
    // $NON-NLS-1$
    return "";
}
Also used : TriggerSequence(org.eclipse.jface.bindings.TriggerSequence) EBindingService(org.eclipse.e4.ui.bindings.EBindingService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ECommandService(org.eclipse.e4.core.commands.ECommandService)

Aggregations

EBindingService (org.eclipse.e4.ui.bindings.EBindingService)24 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)23 Binding (org.eclipse.jface.bindings.Binding)21 TriggerSequence (org.eclipse.jface.bindings.TriggerSequence)18 Test (org.junit.Test)18 ECommandService (org.eclipse.e4.core.commands.ECommandService)17 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)9 EContextService (org.eclipse.e4.ui.services.EContextService)5 Scheme (org.eclipse.jface.bindings.Scheme)4 KeyBinding (org.eclipse.jface.bindings.keys.KeyBinding)4 KeySequence (org.eclipse.jface.bindings.keys.KeySequence)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 NotDefinedException (org.eclipse.core.commands.common.NotDefinedException)3 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 Category (org.eclipse.core.commands.Category)1 Command (org.eclipse.core.commands.Command)1 CommandManager (org.eclipse.core.commands.CommandManager)1 ContextManager (org.eclipse.core.commands.contexts.ContextManager)1