Example usage for org.eclipse.jface.action Action removeMnemonics

List of usage examples for org.eclipse.jface.action Action removeMnemonics

Introduction

In this page you can find the example usage for org.eclipse.jface.action Action removeMnemonics.

Prototype

public static String removeMnemonics(String text) 

Source Link

Document

Convenience method for removing any mnemonics from the given string.

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  .ja v  a2s  .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:net.enilink.komma.edit.ui.action.AbstractActionDelegate.java

License:Open Source License

/**
 * Opens an error dialog for the specified status object.
 * //from   w ww .  j av a  2 s  .c  o  m
 * @param status
 *            The status object for which to open an error dialog.
 * 
 */
protected void openErrorDialog(final IStatus status) {
    final Display display = getDisplay();

    if (display.getThread() == Thread.currentThread()) {
        // we're already on the UI thread
        ErrorDialog.openError(display.getActiveShell(), Action.removeMnemonics(getLabel()), null, status);
    } else {
        // we're not on the UI thread
        display.asyncExec(new Runnable() {
            public void run() {
                ErrorDialog.openError(display.getActiveShell(), Action.removeMnemonics(getLabel()), null,
                        status);
            }
        });
    }
}

From source file:net.enilink.komma.edit.ui.commands.AbstractHandler.java

License:Open Source License

/**
 * Opens an error dialog for the specified status object.
 * /*from  w w  w .  j av  a2s.  c  om*/
 * @param event
 * 
 * @param status
 *            The status object for which to open an error dialog.
 * 
 */
protected void openErrorDialog(final ExecutionEvent event, final IStatus status) {
    final Display display = getDisplay();

    if (display.getThread() == Thread.currentThread()) {
        // we're already on the UI thread
        ErrorDialog.openError(display.getActiveShell(), Action.removeMnemonics(getLabel(event)), null, status);
    } else {
        // we're not on the UI thread
        display.asyncExec(new Runnable() {
            public void run() {
                ErrorDialog.openError(display.getActiveShell(), Action.removeMnemonics(getLabel(event)), null,
                        status);
            }
        });
    }
}

From source file:org.eclipse.cdt.internal.ui.editor.CElementHyperlink.java

License:Open Source License

@Override
public String getHyperlinkText() {
    return Action.removeMnemonics(fOpenAction.getText());
}

From source file:org.eclipse.gmf.examples.runtime.diagram.logic.internal.editparts.LogicShapeCompartmentEditPart.java

License:Open Source License

public String getCompartmentName() {
    return Action.removeMnemonics(ExampleDiagramLogicMessages.logic_CircuitTool_Label);
}

From source file:org.eclipse.gmf.runtime.common.ui.action.AbstractActionDelegate.java

License:Open Source License

/**
 * Opens an error dialog for the specified status object.
 * //  w w  w  .  j a v  a2 s.co m
 * @param status The status object for which to open an error dialog.
 * 
 */
protected void openErrorDialog(final IStatus status) {

    final Display display = DisplayUtils.getDisplay();

    if (display.getThread() == Thread.currentThread()) {
        // we're already on the UI thread
        ErrorDialog.openError(display.getActiveShell(), Action.removeMnemonics(getLabel()), null, status);

    } else {
        // we're not on the UI thread
        display.asyncExec(new Runnable() {
            public void run() {
                ErrorDialog.openError(display.getActiveShell(), Action.removeMnemonics(getLabel()), null,
                        status);
            }
        });
    }
}

From source file:org.eclipse.gmf.runtime.diagram.ui.preferences.AppearancePreferencePage.java

License:Open Source License

/**
 * Adds the font and color fields to the <code>Composite</code> given.
 * @param composite/* ww w . j a  v  a2s .com*/
 */
protected void addFontAndColorFields(Composite composite) {
    defaultFontEditor = new FontFieldEditor(IPreferenceConstants.PREF_DEFAULT_FONT, DEFAULT_FONT_LABEL,
            composite);
    addField(defaultFontEditor);

    fontColorEditor = new ColorFieldEditor(IPreferenceConstants.PREF_FONT_COLOR, FONT_COLOR_LABEL, composite);
    addField(fontColorEditor);

    fillColorEditor = new ColorFieldEditor(IPreferenceConstants.PREF_FILL_COLOR, FILL_COLOR_LABEL, composite);
    addField(fillColorEditor);

    lineColorEditor = new ColorFieldEditor(IPreferenceConstants.PREF_LINE_COLOR, LINE_COLOR_LABEL, composite);
    addField(lineColorEditor);

    noteFillColorEditor = new ColorFieldEditor(IPreferenceConstants.PREF_NOTE_FILL_COLOR, NOTE_FILL_COLOR_LABEL,
            composite);
    addField(noteFillColorEditor);

    noteLineColorEditor = new ColorFieldEditor(IPreferenceConstants.PREF_NOTE_LINE_COLOR, NOTE_LINE_COLOR_LABEL,
            composite);
    addField(noteLineColorEditor);

    //Accessibility helpers...  See bugzilla#142826 ///////////////////////

    fontColorEditor.getColorSelector().getButton().getAccessible()
            .addAccessibleListener(new AccessibleAdapter() {
                public void getName(AccessibleEvent e) {
                    String labelText = fontColorEditor.getLabelText();
                    labelText = Action.removeMnemonics(labelText);
                    e.result = labelText;
                }
            });

    fillColorEditor.getColorSelector().getButton().getAccessible()
            .addAccessibleListener(new AccessibleAdapter() {
                public void getName(AccessibleEvent e) {
                    String labelText = fillColorEditor.getLabelText();
                    labelText = Action.removeMnemonics(labelText);
                    e.result = labelText;
                }
            });

    lineColorEditor.getColorSelector().getButton().getAccessible()
            .addAccessibleListener(new AccessibleAdapter() {
                public void getName(AccessibleEvent e) {
                    String labelText = lineColorEditor.getLabelText();
                    labelText = Action.removeMnemonics(labelText);
                    e.result = labelText;
                }
            });

    noteFillColorEditor.getColorSelector().getButton().getAccessible()
            .addAccessibleListener(new AccessibleAdapter() {
                public void getName(AccessibleEvent e) {
                    String labelText = noteFillColorEditor.getLabelText();
                    labelText = Action.removeMnemonics(labelText);
                    e.result = labelText;
                }
            });

    noteLineColorEditor.getColorSelector().getButton().getAccessible()
            .addAccessibleListener(new AccessibleAdapter() {
                public void getName(AccessibleEvent e) {
                    String labelText = noteLineColorEditor.getLabelText();
                    labelText = Action.removeMnemonics(labelText);
                    e.result = labelText;
                }
            });

}

From source file:org.eclipse.lsp4e.ui.LanguageServerPreferencePage.java

License:Open Source License

private void createStaticServersTable(Composite res) {
    Link staticServersIntro = new Link(res, SWT.WRAP);
    GridDataFactory.swtDefaults().align(SWT.FILL, SWT.TOP).grab(true, false).span(2, 1).hint(400, SWT.DEFAULT)
            .applyTo(staticServersIntro);
    staticServersIntro.setText(Messages.PreferencesPage_staticServers);
    staticServersIntro.addSelectionListener(this.contentTypeLinkListener);
    checkboxViewer = CheckboxTableViewer.newCheckList(res, SWT.NONE);
    checkboxViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    checkboxViewer.setContentProvider(new ArrayContentProvider());

    TableViewerColumn enablementColumn = new TableViewerColumn(checkboxViewer, SWT.NONE);
    enablementColumn.getColumn().setText(Messages.PreferencesPage_Enabled);
    enablementColumn.getColumn().setWidth(70);
    enablementColumn.setLabelProvider(new ColumnLabelProvider() {
        @Override/*w  ww .  ja v a 2 s. c o  m*/
        public String getText(Object element) {
            return null;
        }
    });

    TableViewerColumn contentTypeColumn = new TableViewerColumn(checkboxViewer, SWT.NONE);
    contentTypeColumn.getColumn().setText(Messages.PreferencesPage_contentType);
    contentTypeColumn.getColumn().setWidth(200);
    contentTypeColumn.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            return ((ContentTypeToLanguageServerDefinition) element).getKey().getName();
        }
    });

    TableViewerColumn launchConfigColumn = new TableViewerColumn(checkboxViewer, SWT.NONE);
    launchConfigColumn.getColumn().setText(Messages.PreferencesPage_languageServer);
    launchConfigColumn.getColumn().setWidth(300);
    launchConfigColumn.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            return ((ContentTypeToLanguageServerDefinition) element).getValue().label;
        }
    });

    List<ContentTypeToLanguageServerDefinition> contentTypeToLanguageServerDefinitions = registry
            .getContentTypeToLSPExtensions();
    if (contentTypeToLanguageServerDefinitions.stream()
            .anyMatch(definition -> definition.getEnablementCondition() != null)) {

        TableViewerColumn conditionColumn = new TableViewerColumn(checkboxViewer, SWT.NONE);
        conditionColumn.getColumn().setText(Messages.PreferencesPage_enablementCondition);
        conditionColumn.getColumn().setWidth(150);
        conditionColumn.setLabelProvider(new ColumnLabelProvider() {
            @Override
            public String getText(Object element) {
                EnablementTester tester = ((ContentTypeToLanguageServerDefinition) element)
                        .getEnablementCondition();

                if (tester == null) {
                    // table does not support mnemonic
                    return Action.removeMnemonics(IDialogConstants.NO_LABEL);

                }
                String extensionStatus = ((ContentTypeToLanguageServerDefinition) element).isExtensionEnabled()
                        ? Messages.PreferencePage_enablementCondition_true
                        : Messages.PreferencePage_enablementCondition_false;
                return tester.getDescription() + " (" + extensionStatus + ")"; //$NON-NLS-1$ //$NON-NLS-2$
            }

            @Override
            public Color getBackground(Object element) {
                EnablementTester tester = ((ContentTypeToLanguageServerDefinition) element)
                        .getEnablementCondition();
                if (tester == null) {
                    return null;
                }
                Color red = Display.getDefault().getSystemColor(SWT.COLOR_RED);
                Color green = Display.getDefault().getSystemColor(SWT.COLOR_GREEN);
                return ((ContentTypeToLanguageServerDefinition) element).isExtensionEnabled() ? green : red;
            }
        });
    }

    checkboxViewer.setInput(contentTypeToLanguageServerDefinitions);
    checkboxViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
    checkboxViewer.getTable().setHeaderVisible(true);
    checkboxViewer.getTable().setLinesVisible(true);

    this.checkboxViewer.setCheckedElements(contentTypeToLanguageServerDefinitions.stream()
            .filter(definition -> definition.isUserEnabled()).toArray());

    checkboxViewer.addCheckStateListener(new ICheckStateListener() {

        @Override
        public void checkStateChanged(CheckStateChangedEvent event) {
            if (event.getElement() instanceof ContentTypeToLanguageServerDefinition) {
                ContentTypeToLanguageServerDefinition contentTypeToLanguageServerDefinition = (ContentTypeToLanguageServerDefinition) event
                        .getElement();
                contentTypeToLanguageServerDefinition.setUserEnabled(event.getChecked());
                changedDefinitions.add(contentTypeToLanguageServerDefinition);
            }

        }
    });
}

From source file:org.eclipse.ui.ide.undo.AbstractWorkspaceOperation.java

License:Open Source License

/**
 * Create an AbstractWorkspaceOperation with the specified name.
 * //from ww w .j ava 2 s .  c  o  m
 * @param name
 *            the name used to describe the operation
 */
AbstractWorkspaceOperation(String name) {
    // Many operation names are based on the triggering action's name, so
    // we strip out the any mnemonics that may be embedded in the name.
    super(Action.removeMnemonics(name));

    // For the same reason, check for an ellipsis and strip out
    String label = this.getLabel();
    if (label.endsWith(ELLIPSIS)) {
        this.setLabel(label.substring(0, label.length() - ELLIPSIS.length()));
    }
}

From source file:org.eclipse.ui.internal.actions.DynamicHelpAction.java

License:Open Source License

/**
 * Constructor for use by ActionFactory.
 * //  w  w w.  jav a  2s.c  o m
 * @param window
 *            the window
 */
public DynamicHelpAction(IWorkbenchWindow window) {
    if (window == null) {
        throw new IllegalArgumentException();
    }
    this.workbenchWindow = window;
    setActionDefinitionId(IWorkbenchCommandConstants.HELP_DYNAMIC_HELP);

    // support for allowing a product to override the text for the action
    String overrideText = PrefUtil.getAPIPreferenceStore()
            .getString(IWorkbenchPreferenceConstants.DYNAMIC_HELP_ACTION_TEXT);
    if ("".equals(overrideText)) { //$NON-NLS-1$
        setText(appendAccelerator(WorkbenchMessages.DynamicHelpAction_text));
        setToolTipText(WorkbenchMessages.DynamicHelpAction_toolTip);
    } else {
        setText(appendAccelerator(overrideText));
        setToolTipText(Action.removeMnemonics(overrideText));
    }
    window.getWorkbench().getHelpSystem().setHelp(this, IWorkbenchHelpContextIds.DYNAMIC_HELP_ACTION);
}