Search in sources :

Example 1 with EBindingService

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

the class KeyBindingsTest method doTestKeyBindingsOnPage.

private void doTestKeyBindingsOnPage(String pageName) {
    openEditor(pageName);
    try {
        PartSite partSite = (PartSite) (editorPart.getEditorSite() instanceof PartSite ? editorPart.getEditorSite() : null);
        assertNotNull("Cannot get PartSite instance for " + pageName, partSite);
        IEclipseContext context = partSite.getContext();
        assertNotNull("Cannot get EclipseContext instance for " + pageName, context);
        EBindingService bindingService = (EBindingService) context.get(EBindingService.class.getName());
        assertNotNull("Cannot get BindingService instance for " + pageName, bindingService);
        for (KeyCombination key : keys) {
            Event event = createKeyCombinationEvent(key.mask, key.key);
            List<KeyStroke> keyStrokes = KeyBindingDispatcher.generatePossibleKeyStrokes(event);
            boolean found = false;
            KeySequence sequenceBeforeKeyStroke = KeySequence.getInstance();
            for (Iterator<KeyStroke> iterator = keyStrokes.iterator(); iterator.hasNext(); ) {
                KeySequence sequenceAfterKeyStroke = KeySequence.getInstance(sequenceBeforeKeyStroke, iterator.next());
                if (isPerfectMatch(bindingService, sequenceAfterKeyStroke)) {
                    final ParameterizedCommand cmd = getPerfectMatch(bindingService, sequenceAfterKeyStroke);
                    assertNotNull("Command not found for 'Perfect Match' keystroke " + sequenceAfterKeyStroke, cmd);
                    assertNotNull("Command found but cannot be executed for 'Perfect Match' keystroke " + sequenceAfterKeyStroke, HandlerServiceImpl.lookUpHandler(context, cmd.getId()));
                    found = true;
                    break;
                }
            }
            assertTrue("Command not found for key combination " + key.mask + "/" + key.key, found);
        }
    } finally {
        closeEditor();
    }
}
Also used : EBindingService(org.eclipse.e4.ui.bindings.EBindingService) PartSite(org.eclipse.ui.internal.PartSite) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) KeyStroke(org.eclipse.jface.bindings.keys.KeyStroke) Event(org.eclipse.swt.widgets.Event) KeySequence(org.eclipse.jface.bindings.keys.KeySequence) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Example 2 with EBindingService

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

the class KeyAssistDialog method getActiveBindings.

private Collection<Binding> getActiveBindings() {
    EBindingService bindingService = context.getActiveLeaf().get(EBindingService.class);
    Iterator<Binding> iter, matchesIter;
    Binding binding, bindingToAdd;
    Collection<Binding> matchesForCommand;
    Collection<Binding> activeBindings = bindingService.getActiveBindings();
    Collection<Binding> conflictBindings = bindingService.getAllConflicts();
    Collection<Binding> sortedMatches = new TreeSet<>((binding1, binding2) -> {
        ParameterizedCommand cmdA = binding1.getParameterizedCommand();
        ParameterizedCommand cmdB = binding2.getParameterizedCommand();
        int result = 0;
        try {
            result = cmdA.getName().compareTo(cmdB.getName());
        } catch (NotDefinedException e) {
        // whaaa?
        }
        return result;
    });
    // different schemes, then we need to handle which one should be displayed in the dialog
    if (activeBindings != null) {
        iter = activeBindings.iterator();
        while (iter.hasNext()) {
            binding = iter.next();
            matchesForCommand = bindingService.getBindingsFor(binding.getParameterizedCommand());
            // belong to the default scheme, then arbitrarily choose one
            if (matchesForCommand != null && matchesForCommand.size() > 1) {
                bindingToAdd = null;
                matchesIter = matchesForCommand.iterator();
                while (matchesIter.hasNext()) {
                    bindingToAdd = matchesIter.next();
                    if (!bindingToAdd.getSchemeId().equals(EBindingService.DEFAULT_SCHEME_ID)) {
                        sortedMatches.add(bindingToAdd);
                        break;
                    }
                }
                // if they're all the same, arbitrarily choose one
                if (bindingToAdd != null) {
                    sortedMatches.add(bindingToAdd);
                }
            } else // if there is only one match, then just add it
            if (matchesForCommand != null && matchesForCommand.size() == 1) {
                sortedMatches.addAll(matchesForCommand);
            }
        }
    }
    if (conflictBindings != null) {
        iter = conflictBindings.iterator();
        while (iter.hasNext()) {
            binding = iter.next();
            sortedMatches.add(binding);
        }
    }
    return sortedMatches;
}
Also used : Binding(org.eclipse.jface.bindings.Binding) EBindingService(org.eclipse.e4.ui.bindings.EBindingService) TreeSet(java.util.TreeSet) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Point(org.eclipse.swt.graphics.Point)

Example 3 with EBindingService

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

the class CustomizePerspectiveDialog method getToolTipText.

private String getToolTipText(MItem item) {
    String text = item.getLocalizedTooltip();
    if (item instanceof MHandledItem) {
        MHandledItem handledItem = (MHandledItem) item;
        EBindingService bs = context.get(EBindingService.class);
        ParameterizedCommand cmd = handledItem.getWbCommand();
        if (cmd == null) {
            cmd = generateParameterizedCommand(handledItem, context);
        }
        TriggerSequence sequence = bs.getBestSequenceFor(handledItem.getWbCommand());
        if (sequence != null) {
            if (text == null) {
                try {
                    text = cmd.getName();
                } catch (NotDefinedException e) {
                    return null;
                }
            }
            // $NON-NLS-1$
            text = text + " (" + sequence.format() + ')';
        }
        return text;
    } else if (OpaqueElementUtil.isOpaqueMenuItem(item)) {
        Object opaque = OpaqueElementUtil.getOpaqueItem(item);
        if (opaque instanceof ActionContributionItem) {
            return ((ActionContributionItem) opaque).getAction().getText();
        }
    } else if (OpaqueElementUtil.isOpaqueToolItem(item)) {
        Object opaque = OpaqueElementUtil.getOpaqueItem(item);
        if (opaque instanceof ActionContributionItem) {
            return ((ActionContributionItem) opaque).getAction().getToolTipText();
        }
    }
    return text;
}
Also used : MHandledItem(org.eclipse.e4.ui.model.application.ui.menu.MHandledItem) TriggerSequence(org.eclipse.jface.bindings.TriggerSequence) ActionContributionItem(org.eclipse.jface.action.ActionContributionItem) EBindingService(org.eclipse.e4.ui.bindings.EBindingService) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Example 4 with EBindingService

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

the class BindingLookupTest method testLookupAllShortcuts.

@Test
public void testLookupAllShortcuts() {
    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);
    // the list will always be ordered
    ArrayList<TriggerSequence> list = new ArrayList<>();
    list.add(seq);
    list.add(seq2);
    assertEquals(list, bs.getSequencesFor(cmd));
}
Also used : Binding(org.eclipse.jface.bindings.Binding) TriggerSequence(org.eclipse.jface.bindings.TriggerSequence) EBindingService(org.eclipse.e4.ui.bindings.EBindingService) ArrayList(java.util.ArrayList) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ECommandService(org.eclipse.e4.core.commands.ECommandService) Test(org.junit.Test)

Example 5 with EBindingService

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

the class BindingLookupTest method testMultipleBindings.

@Test
public void testMultipleBindings() {
    ECommandService cs = workbenchContext.get(ECommandService.class);
    ParameterizedCommand cmd = cs.createCommand(TEST_ID1, null);
    EBindingService bs = workbenchContext.get(EBindingService.class);
    TriggerSequence seq = bs.createSequence("CTRL+5 T");
    TriggerSequence seq2 = bs.createSequence("CTRL+2 X");
    Binding db = createDefaultBinding(bs, seq, cmd, ID_DIALOG_AND_WINDOW);
    bs.activateBinding(db);
    db = createDefaultBinding(bs, seq2, cmd, ID_DIALOG_AND_WINDOW);
    bs.activateBinding(db);
    assertEquals(cmd, bs.getPerfectMatch(seq).getParameterizedCommand());
    assertEquals(cmd, bs.getPerfectMatch(seq2).getParameterizedCommand());
}
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)

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