Example usage for org.eclipse.jface.bindings TriggerSequence getTriggers

List of usage examples for org.eclipse.jface.bindings TriggerSequence getTriggers

Introduction

In this page you can find the example usage for org.eclipse.jface.bindings TriggerSequence getTriggers.

Prototype

public final Trigger[] getTriggers() 

Source Link

Document

Returns the list of triggers.

Usage

From source file:com.github.haixing_hu.swt.action.ActionContributionItemEx.java

License:Open Source License

/**
 * Synchronizes the UI with the given property.
 *
 * @param propertyName/*  w  w w .  j a v a  2  s .  c om*/
 *          the name of the property, or <code>null</code> meaning all
 *          applicable properties
 */
@Override
public void update(String propertyName) {
    if (widget != null) {
        // determine what to do
        final boolean textChanged = (propertyName == null) || propertyName.equals(IAction.TEXT);
        boolean imageChanged = (propertyName == null) || propertyName.equals(IAction.IMAGE);
        final boolean tooltipTextChanged = (propertyName == null) || propertyName.equals(IAction.TOOL_TIP_TEXT);
        final boolean enableStateChanged = (propertyName == null) || propertyName.equals(IAction.ENABLED)
                || propertyName.equals(IContributionManagerOverrides.P_ENABLED);
        final boolean checkChanged = ((action.getStyle() == IAction.AS_CHECK_BOX)
                || (action.getStyle() == IAction.AS_RADIO_BUTTON))
                && ((propertyName == null) || propertyName.equals(IAction.CHECKED));

        if (!showImage) {
            //  do not update the image if not show image
            imageChanged = false;
        }
        if (widget instanceof ToolItem) {
            final ToolItem ti = (ToolItem) widget;
            String text = action.getText();
            // the set text is shown only if there is no image or if forced
            // by MODE_FORCE_TEXT
            final boolean showText = (text != null)
                    && (((getMode() & MODE_FORCE_TEXT) != 0) || !hasImages(action));

            // only do the trimming if the text will be used
            if (showText && (text != null)) {
                text = Action.removeAcceleratorText(text);
                text = Action.removeMnemonics(text);
            }

            if (textChanged) {
                final String textToSet = showText ? text : ""; //$NON-NLS-1$
                final boolean rightStyle = (ti.getParent().getStyle() & SWT.RIGHT) != 0;
                if (rightStyle || !ti.getText().equals(textToSet)) {
                    // In addition to being required to update the text if
                    // it
                    // gets nulled out in the action, this is also a
                    // workaround
                    // for bug 50151: Using SWT.RIGHT on a ToolBar leaves
                    // blank space
                    ti.setText(textToSet);
                }
            }

            if (imageChanged) {
                // only substitute a missing image if it has no text
                updateImages(!showText);
            }

            if (tooltipTextChanged || textChanged) {
                String toolTip = action.getToolTipText();
                if ((toolTip == null) || (toolTip.length() == 0)) {
                    toolTip = text;
                }

                final ExternalActionManager.ICallback callback = ExternalActionManager.getInstance()
                        .getCallback();
                final String commandId = action.getActionDefinitionId();
                if ((callback != null) && (commandId != null) && (toolTip != null)) {
                    final String acceleratorText = callback.getAcceleratorText(commandId);
                    if ((acceleratorText != null) && (acceleratorText.length() != 0)) {
                        toolTip = JFaceResources.format("Toolbar_Tooltip_Accelerator", //$NON-NLS-1$
                                new Object[] { toolTip, acceleratorText });
                    }
                }

                // if the text is showing, then only set the tooltip if
                // different
                if (!showText || ((toolTip != null) && !toolTip.equals(text))) {
                    ti.setToolTipText(toolTip);
                } else {
                    ti.setToolTipText(null);
                }
            }

            if (enableStateChanged) {
                final boolean shouldBeEnabled = action.isEnabled() && isEnabledAllowed();

                if (ti.getEnabled() != shouldBeEnabled) {
                    ti.setEnabled(shouldBeEnabled);
                }
            }

            if (checkChanged) {
                final boolean bv = action.isChecked();

                if (ti.getSelection() != bv) {
                    ti.setSelection(bv);
                }
            }
            return;
        }

        if (widget instanceof MenuItem) {
            final MenuItem mi = (MenuItem) widget;

            if (textChanged) {
                int accelerator = 0;
                String acceleratorText = null;
                final ActionEx updatedAction = getAction();
                String text = null;
                accelerator = updatedAction.getAccelerator();
                final ExternalActionManager.ICallback callback = ExternalActionManager.getInstance()
                        .getCallback();

                // Block accelerators that are already in use.
                if ((accelerator != 0) && (callback != null) && (callback.isAcceleratorInUse(accelerator))) {
                    accelerator = 0;
                }

                /*
                 * Process accelerators on GTK in a special way to avoid Bug 42009. We
                 * will override the native input method by allowing these reserved
                 * accelerators to be placed on the menu. We will only do this for
                 * "Ctrl+Shift+[0-9A-FU]".
                 */
                final String commandId = updatedAction.getActionDefinitionId();
                if ((Util.isGtk()) && (callback instanceof IBindingManagerCallback) && (commandId != null)) {
                    final IBindingManagerCallback bindingManagerCallback = (IBindingManagerCallback) callback;
                    final IKeyLookup lookup = KeyLookupFactory.getDefault();
                    final TriggerSequence[] triggerSequences = bindingManagerCallback
                            .getActiveBindingsFor(commandId);
                    for (final TriggerSequence triggerSequence : triggerSequences) {
                        final Trigger[] triggers = triggerSequence.getTriggers();
                        if (triggers.length == 1) {
                            final Trigger trigger = triggers[0];
                            if (trigger instanceof KeyStroke) {
                                final KeyStroke currentKeyStroke = (KeyStroke) trigger;
                                final int currentNaturalKey = currentKeyStroke.getNaturalKey();
                                if ((currentKeyStroke
                                        .getModifierKeys() == (lookup.getCtrl() | lookup.getShift()))
                                        && (((currentNaturalKey >= '0') && (currentNaturalKey <= '9'))
                                                || ((currentNaturalKey >= 'A') && (currentNaturalKey <= 'F'))
                                                || (currentNaturalKey == 'U'))) {
                                    accelerator = currentKeyStroke.getModifierKeys() | currentNaturalKey;
                                    acceleratorText = triggerSequence.format();
                                    break;
                                }
                            }
                        }
                    }
                }

                if (accelerator == 0) {
                    if ((callback != null) && (commandId != null)) {
                        acceleratorText = callback.getAcceleratorText(commandId);
                    }
                }

                IContributionManagerOverrides overrides = null;

                if (getParent() != null) {
                    overrides = getParent().getOverrides();
                }

                if (overrides != null) {
                    text = getParent().getOverrides().getText(this);
                }

                mi.setAccelerator(accelerator);

                if (text == null) {
                    text = updatedAction.getText();
                }

                if ((text != null) && (acceleratorText == null)) {
                    // use extracted accelerator text in case accelerator
                    // cannot be fully represented in one int (e.g.
                    // multi-stroke keys)
                    acceleratorText = LegacyActionTools.extractAcceleratorText(text);
                    if ((acceleratorText == null) && (accelerator != 0)) {
                        acceleratorText = Action.convertAccelerator(accelerator);
                    }
                }

                if (text == null) {
                    text = ""; //$NON-NLS-1$
                } else {
                    text = Action.removeAcceleratorText(text);
                }

                // add "..." if the action will show a dialog
                if (updatedAction.isShowDialog()) {
                    text = text + dialogIndicator;
                }

                if (acceleratorText == null) {
                    mi.setText(text);
                } else {
                    mi.setText(text + '\t' + acceleratorText);
                }
            }

            if (imageChanged) {
                updateImages(false);
            }

            if (enableStateChanged) {
                final boolean shouldBeEnabled = action.isEnabled() && isEnabledAllowed();

                if (mi.getEnabled() != shouldBeEnabled) {
                    mi.setEnabled(shouldBeEnabled);
                }
            }

            if (checkChanged) {
                final boolean bv = action.isChecked();

                if (mi.getSelection() != bv) {
                    mi.setSelection(bv);
                }
            }

            return;
        }

        if (widget instanceof Button) {
            final Button button = (Button) widget;

            if (imageChanged) {
                updateImages(false);
            }

            if (textChanged) {
                String text = action.getText();
                final boolean showText = (text != null)
                        && (((getMode() & MODE_FORCE_TEXT) != 0) || !hasImages(action));
                // only do the trimming if the text will be used
                if (showText) {
                    text = Action.removeAcceleratorText(text);
                }
                final String textToSet = showText ? text : ""; //$NON-NLS-1$
                button.setText(textToSet);
            }

            if (tooltipTextChanged) {
                button.setToolTipText(action.getToolTipText());
            }

            if (enableStateChanged) {
                final boolean shouldBeEnabled = action.isEnabled() && isEnabledAllowed();

                if (button.getEnabled() != shouldBeEnabled) {
                    button.setEnabled(shouldBeEnabled);
                }
            }

            if (checkChanged) {
                final boolean bv = action.isChecked();

                if (button.getSelection() != bv) {
                    button.setSelection(bv);
                }
            }
            return;
        }
    }
}

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

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {

    EmacsPlusConsole console = EmacsPlusConsole.getInstance();
    console.clear();/*from  w w w  .  j  a v a2 s  .c om*/
    console.activate();
    IBindingService bindingService = (IBindingService) PlatformUI.getWorkbench()
            .getService(IBindingService.class);
    String id = (event.getCommand() != null ? event.getCommand().getId() : null);
    if (id != null) {
        try {
            TriggerSequence trigger = bindingService.getBestActiveBindingFor(event.getCommand().getId());
            Trigger[] trigs = trigger.getTriggers();
            KeyStroke key = (KeyStroke) trigs[0];
            Collection<Binding> partials = EmacsPlusUtils
                    .getPartialMatches(bindingService, KeySequence.getInstance(key)).values();

            for (Binding bind : partials) {
                ParameterizedCommand cmd = bind.getParameterizedCommand();
                if (cmd.getId().startsWith(EmacsPlusUtils.MULGASOFT)) {
                    console.printBold(bind.getTriggerSequence().toString());
                    console.print(SWT.TAB + cmd.getCommand().getName());
                    String desc = cmd.getCommand().getDescription();
                    if (desc != null) {
                        desc = desc.replaceAll(CR, EMPTY_STR);
                        console.print(" - " + desc + CR); //$NON-NLS-1$ 
                    } else {
                        console.print(CR);
                    }
                }
            }
        } catch (Exception e) {
        }
        console.setFocus(true);
    }
    return null;
}

From source file:hydrograph.ui.expression.editor.javasourceviewerconfiguration.HotKeyUtil.java

License:Apache License

public static KeyStroke getHotKey(String commondID) throws ParseException {
    IBindingService bindingService = (IBindingService) PlatformUI.getWorkbench()
            .getService(IBindingService.class);
    if (bindingService != null) {
        TriggerSequence trigger = bindingService.getBestActiveBindingFor(commondID);
        if (trigger != null) {
            Trigger[] tiggers = trigger.getTriggers();
            if (tiggers.length > 0) {
                Trigger tigger = tiggers[0];
                if (tigger instanceof KeyStroke) {
                    return (KeyStroke) tigger;
                }// w  w  w  .j av  a  2 s  . c  om
            }
        }
    }
    return KeyStroke.getInstance("Ctrl+Space");
}

From source file:org.eclipse.php.internal.ui.text.correction.CorrectionCommandHandler.java

License:Open Source License

/**
 * //  ww  w .jav a2s . co m
 */
public static String getShortcut(String commandId) {
    final IBindingService keys = (IBindingService) PlatformUI.getWorkbench().getService(IBindingService.class);

    if (commandId != null && keys != null) {
        TriggerSequence trigger = keys.getBestActiveBindingFor(commandId);
        if (trigger != null && trigger.getTriggers().length > 0) {
            return trigger.format();
        }
    }

    return null;
}

From source file:org.eclipse.rcptt.ecl.platform.internal.ui.commands.GetCommandHotkeyService.java

License:Open Source License

public IStatus service(Command command, IProcess context) throws InterruptedException, CoreException {
    if (!(command instanceof GetHotkey)) {
        return Status.CANCEL_STATUS;
    }/*w w  w.  j a  va 2 s . com*/
    GetHotkey getCommand = (GetHotkey) command;
    IBindingService bindingService = (IBindingService) PlatformUI.getWorkbench()
            .getService(IBindingService.class);
    for (TriggerSequence binding : bindingService.getActiveBindingsFor(getCommand.getCommandId())) {
        for (Trigger trigger : binding.getTriggers()) {
            if (!(trigger instanceof KeyStroke)) {
                continue;
            }
            KeyStroke keyStroke = (KeyStroke) trigger;
            // context.getOutput().write(formatKeyWithMeta(0,
            // keyStroke.getNaturalKey(), keyStroke.getModifierKeys()));
            context.getOutput().write(keyStroke.toString());
            return Status.OK_STATUS;
        }
    }
    return new Status(IStatus.ERROR, PlatformUIPlugin.PLUGIN_ID,
            "No keyboard hotkey is defined for " + getCommand.getCommandId() + " in current context");
}

From source file:org.eclipse.sirius.diagram.editor.properties.sections.description.diagramdescription.DiagramDescriptionPreconditionExpressionPropertySection.java

License:Open Source License

private KeyStroke getKeyStroke(TriggerSequence sequence) {
    for (Trigger trigger : sequence.getTriggers()) {
        if (trigger instanceof KeyStroke) {
            return (KeyStroke) trigger;
        }//from   w  w w. j a  v a 2  s.c  o m
    }
    return null;
}

From source file:org.eclipse.sirius.editor.tools.api.assist.TypeContentProposalProvider.java

License:Open Source License

private static KeyStroke getKeyStroke(TriggerSequence sequence) {
    for (Trigger trigger : sequence.getTriggers()) {
        if (trigger instanceof KeyStroke) {
            return (KeyStroke) trigger;
        }//from w  ww.j  a v a2  s .c  o  m
    }
    return null;
}

From source file:org.eclipse.ui.internal.CycleBaseHandler.java

License:Open Source License

protected void addKeyListener(final Table table, final Shell dialog) {
    table.addKeyListener(new KeyListener() {
        private boolean firstKey = true;

        private boolean quickReleaseMode = false;

        public void keyPressed(KeyEvent e) {
            int keyCode = e.keyCode;
            char character = e.character;
            int accelerator = SWTKeySupport.convertEventToUnmodifiedAccelerator(e);
            KeyStroke keyStroke = SWTKeySupport.convertAcceleratorToKeyStroke(accelerator);

            boolean acceleratorForward = false;
            boolean acceleratorBackward = false;

            if (commandForward != null) {
                if (forwardTriggerSequences != null) {
                    final int forwardCount = forwardTriggerSequences.length;
                    for (int i = 0; i < forwardCount; i++) {
                        final TriggerSequence triggerSequence = forwardTriggerSequences[i];

                        // Compare the last key stroke of the binding.
                        final Trigger[] triggers = triggerSequence.getTriggers();
                        final int triggersLength = triggers.length;
                        if ((triggersLength > 0) && (triggers[triggersLength - 1].equals(keyStroke))) {
                            acceleratorForward = true;
                            break;
                        }//from   w ww .  j a v a2  s . c  o m
                    }
                }
            }

            if (commandBackward != null) {
                if (backwardTriggerSequences != null) {
                    final int backwardCount = backwardTriggerSequences.length;
                    for (int i = 0; i < backwardCount; i++) {
                        final TriggerSequence triggerSequence = backwardTriggerSequences[i];

                        // Compare the last key stroke of the binding.
                        final Trigger[] triggers = triggerSequence.getTriggers();
                        final int triggersLength = triggers.length;
                        if ((triggersLength > 0) && (triggers[triggersLength - 1].equals(keyStroke))) {
                            acceleratorBackward = true;
                            break;
                        }
                    }
                }
            }

            if (character == SWT.CR || character == SWT.LF) {
                ok(dialog, table);
            } else if (acceleratorForward) {
                if (firstKey && e.stateMask != 0) {
                    quickReleaseMode = true;
                }

                int index = table.getSelectionIndex();
                table.setSelection((index + 1) % table.getItemCount());
            } else if (acceleratorBackward) {
                if (firstKey && e.stateMask != 0) {
                    quickReleaseMode = true;
                }

                int index = table.getSelectionIndex();
                table.setSelection(index >= 1 ? index - 1 : table.getItemCount() - 1);
            } else if (keyCode != SWT.ALT && keyCode != SWT.COMMAND && keyCode != SWT.CTRL
                    && keyCode != SWT.SHIFT && keyCode != SWT.ARROW_DOWN && keyCode != SWT.ARROW_UP
                    && keyCode != SWT.ARROW_LEFT && keyCode != SWT.ARROW_RIGHT) {
                cancel(dialog);
            }

            firstKey = false;
        }

        public void keyReleased(KeyEvent e) {
            int keyCode = e.keyCode;
            int stateMask = e.stateMask;

            final IPreferenceStore store = WorkbenchPlugin.getDefault().getPreferenceStore();
            final boolean stickyCycle = store.getBoolean(IPreferenceConstants.STICKY_CYCLE);
            if ((!stickyCycle && (firstKey || quickReleaseMode)) && keyCode == stateMask) {
                ok(dialog, table);
            }
        }
    });
}

From source file:org.eclipse.ui.internal.FilteredTableBaseHandler.java

License:Open Source License

boolean computeAcceleratorForward(KeyEvent e) {
    boolean acceleratorForward = false;
    if (commandForward != null) {
        if (forwardTriggerSequences != null) {
            final int forwardCount = forwardTriggerSequences.length;
            for (int i = 0; i < forwardCount; i++) {
                final TriggerSequence triggerSequence = forwardTriggerSequences[i];

                // Compare the last key stroke of the binding.
                final Trigger[] triggers = triggerSequence.getTriggers();
                final int triggersLength = triggers.length;
                if ((triggersLength > 0) && (triggers[triggersLength - 1].equals(computeKeyStroke(e)))) {
                    acceleratorForward = true;
                    break;
                }/*from   ww  w.  j a va2  s .c  o  m*/
            }
        }
    }
    return acceleratorForward;
}

From source file:org.eclipse.ui.internal.FilteredTableBaseHandler.java

License:Open Source License

boolean computeAcceleratorBackward(KeyEvent e) {
    boolean acceleratorBackward = false;
    if (commandBackward != null) {
        if (backwardTriggerSequences != null) {
            final int backwardCount = backwardTriggerSequences.length;
            for (int i = 0; i < backwardCount; i++) {
                final TriggerSequence triggerSequence = backwardTriggerSequences[i];

                // Compare the last key stroke of the binding.
                final Trigger[] triggers = triggerSequence.getTriggers();
                final int triggersLength = triggers.length;
                if ((triggersLength > 0) && (triggers[triggersLength - 1].equals(computeKeyStroke(e)))) {
                    acceleratorBackward = true;
                    break;
                }/*ww  w  . ja  va2s .c o m*/
            }
        }
    }
    return acceleratorBackward;
}