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

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

Introduction

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

Prototype

public static String removeAcceleratorText(String text) 

Source Link

Document

Convenience method for removing any optional accelerator text from the given string.

Usage

From source file:com.architexa.org.eclipse.gef.internal.ui.palette.ToolbarDropdownContributionItem.java

License:Open Source License

/**
 * Synchronizes the UI with the given property.
 *
 * @param propertyName the name of the property, or <code>null</code> meaning all 
 *                   applicable properties 
 *//*  w  w w .  j  av a 2 s. c  o m*/
public void update(String propertyName) {
    if (widget != null) {

        // determine what to do         
        boolean textChanged = propertyName == null || propertyName.equals(Action.TEXT);
        boolean imageChanged = propertyName == null || propertyName.equals(Action.IMAGE);
        boolean tooltipTextChanged = propertyName == null || propertyName.equals(Action.TOOL_TIP_TEXT);
        boolean enableStateChanged = propertyName == null || propertyName.equals(Action.ENABLED)
                || propertyName.equals(IContributionManagerOverrides.P_ENABLED);
        boolean checkChanged = (action.getStyle() == IAction.AS_CHECK_BOX)
                && (propertyName == null || propertyName.equals(Action.CHECKED));

        if (widget instanceof ToolItem) {
            ToolItem ti = (ToolItem) widget;
            if (imageChanged) {
                updateImages(true);
            }
            if (tooltipTextChanged)
                ti.setToolTipText(action.getToolTipText());

            if (enableStateChanged) {
                boolean shouldBeEnabled = action.isEnabled() && isEnabledAllowed();
                if (ti.getEnabled() != shouldBeEnabled)
                    ti.setEnabled(shouldBeEnabled);
            }

            if (checkChanged) {
                boolean bv = action.isChecked();
                if (ti.getSelection() != bv)
                    ti.setSelection(bv);
            }
            return;
        }

        if (widget instanceof MenuItem) {
            MenuItem mi = (MenuItem) widget;
            boolean isContextMenu = belongsToContextMenu(mi);

            // We only install an accelerator if the menu item doesn't
            // belong to a context menu (right mouse button menu).
            if (textChanged) {
                if (isContextMenu) {
                    String text = action.getText();
                    if (text != null) {
                        text = Action.removeAcceleratorText(text);
                        mi.setText(text);
                    }
                } else {
                    String text = null;
                    IContributionManagerOverrides overrides = null;
                    if (getParent() != null)
                        overrides = getParent().getOverrides();
                    if (overrides != null)
                        text = getParent().getOverrides().getText(this);
                    if (text == null)
                        text = action.getText();
                    if (text != null) {
                        String label = Action.removeAcceleratorText(text);
                        String accText = null;
                        Integer acc = null;
                        if (overrides != null) {
                            accText = overrides.getAcceleratorText(this);
                            acc = overrides.getAccelerator(this);
                        }
                        if ((accText == null) && (label.length() + 1 < text.length()))
                            accText = text.substring(label.length() + 1);
                        if (acc == null)
                            acc = new Integer(action.getAccelerator());
                        if (acc.intValue() >= 0)
                            mi.setAccelerator(acc.intValue());
                        if (accText == null)
                            mi.setText(label);
                        else
                            mi.setText(label + '\t' + accText);
                    }
                }
            }
            if (imageChanged) {
                updateImages(false);
            }
            if (enableStateChanged) {
                boolean shouldBeEnabled = action.isEnabled() && isEnabledAllowed();
                if (mi.getEnabled() != shouldBeEnabled)
                    mi.setEnabled(shouldBeEnabled);
            }

            if (checkChanged) {
                boolean bv = action.isChecked();
                if (mi.getSelection() != bv)
                    mi.setSelection(bv);
            }
            return;
        }

        if (widget instanceof Button) {
            Button button = (Button) widget;
            if (imageChanged) {
                if (updateImages(false)) {
                    // don't update text if it has an image
                    textChanged = false;
                }
            }
            if (textChanged) {
                String text = action.getText();
                if (text != null)
                    button.setText(text);
            }
            if (tooltipTextChanged)
                button.setToolTipText(action.getToolTipText());

            if (enableStateChanged) {
                boolean shouldBeEnabled = action.isEnabled() && isEnabledAllowed();
                if (button.getEnabled() != shouldBeEnabled)
                    button.setEnabled(shouldBeEnabled);
            }

            if (checkChanged) {
                boolean bv = action.isChecked();
                if (button.getSelection() != bv)
                    button.setSelection(bv);
            }
            return;
        }
    }
}

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

License:Open Source License

/**
 * Synchronizes the UI with the given property.
 *
 * @param propertyName/* ww  w . java 2s  . com*/
 *          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:org.eclipse.gef.internal.ui.palette.ToolbarDropdownContributionItem.java

License:Open Source License

/**
 * Synchronizes the UI with the given property.
 * //from   w  w  w  .ja  va2s  . c  om
 * @param propertyName
 *            the name of the property, or <code>null</code> meaning all
 *            applicable properties
 */
public void update(String propertyName) {
    if (widget != null) {

        // determine what to do
        boolean textChanged = propertyName == null || propertyName.equals(Action.TEXT);
        boolean imageChanged = propertyName == null || propertyName.equals(Action.IMAGE);
        boolean tooltipTextChanged = propertyName == null || propertyName.equals(Action.TOOL_TIP_TEXT);
        boolean enableStateChanged = propertyName == null || propertyName.equals(Action.ENABLED)
                || propertyName.equals(IContributionManagerOverrides.P_ENABLED);
        boolean checkChanged = (action.getStyle() == IAction.AS_CHECK_BOX)
                && (propertyName == null || propertyName.equals(Action.CHECKED));

        if (widget instanceof ToolItem) {
            ToolItem ti = (ToolItem) widget;
            if (imageChanged) {
                updateImages(true);
            }
            if (tooltipTextChanged)
                ti.setToolTipText(action.getToolTipText());

            if (enableStateChanged) {
                boolean shouldBeEnabled = action.isEnabled() && isEnabledAllowed();
                if (ti.getEnabled() != shouldBeEnabled)
                    ti.setEnabled(shouldBeEnabled);
            }

            if (checkChanged) {
                boolean bv = action.isChecked();
                if (ti.getSelection() != bv)
                    ti.setSelection(bv);
            }
            return;
        }

        if (widget instanceof MenuItem) {
            MenuItem mi = (MenuItem) widget;
            boolean isContextMenu = belongsToContextMenu(mi);

            // We only install an accelerator if the menu item doesn't
            // belong to a context menu (right mouse button menu).
            if (textChanged) {
                if (isContextMenu) {
                    String text = action.getText();
                    if (text != null) {
                        text = Action.removeAcceleratorText(text);
                        mi.setText(text);
                    }
                } else {
                    String text = null;
                    IContributionManagerOverrides overrides = null;
                    if (getParent() != null)
                        overrides = getParent().getOverrides();
                    if (overrides != null)
                        text = getParent().getOverrides().getText(this);
                    if (text == null)
                        text = action.getText();
                    if (text != null) {
                        String label = Action.removeAcceleratorText(text);
                        String accText = null;
                        Integer acc = null;
                        if (overrides != null) {
                            accText = overrides.getAcceleratorText(this);
                            acc = overrides.getAccelerator(this);
                        }
                        if ((accText == null) && (label.length() + 1 < text.length()))
                            accText = text.substring(label.length() + 1);
                        if (acc == null)
                            acc = new Integer(action.getAccelerator());
                        if (acc.intValue() >= 0)
                            mi.setAccelerator(acc.intValue());
                        if (accText == null)
                            mi.setText(label);
                        else
                            mi.setText(label + '\t' + accText);
                    }
                }
            }
            if (imageChanged) {
                updateImages(false);
            }
            if (enableStateChanged) {
                boolean shouldBeEnabled = action.isEnabled() && isEnabledAllowed();
                if (mi.getEnabled() != shouldBeEnabled)
                    mi.setEnabled(shouldBeEnabled);
            }

            if (checkChanged) {
                boolean bv = action.isChecked();
                if (mi.getSelection() != bv)
                    mi.setSelection(bv);
            }
            return;
        }

        if (widget instanceof Button) {
            Button button = (Button) widget;
            if (imageChanged) {
                if (updateImages(false)) {
                    // don't update text if it has an image
                    textChanged = false;
                }
            }
            if (textChanged) {
                String text = action.getText();
                if (text != null)
                    button.setText(text);
            }
            if (tooltipTextChanged)
                button.setToolTipText(action.getToolTipText());

            if (enableStateChanged) {
                boolean shouldBeEnabled = action.isEnabled() && isEnabledAllowed();
                if (button.getEnabled() != shouldBeEnabled)
                    button.setEnabled(shouldBeEnabled);
            }

            if (checkChanged) {
                boolean bv = action.isChecked();
                if (button.getSelection() != bv)
                    button.setSelection(bv);
            }
            return;
        }
    }
}

From source file:org.springframework.ide.eclipse.boot.dash.util.ToolbarPulldownContributionItem.java

License:Open Source License

/**
 * Synchronizes the UI with the given property.
 *
 * @param propertyName//  ww  w. j a  va2s .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
        boolean textChanged = propertyName == null || propertyName.equals(IAction.TEXT);
        boolean imageChanged = propertyName == null || propertyName.equals(IAction.IMAGE);
        boolean tooltipTextChanged = propertyName == null || propertyName.equals(IAction.TOOL_TIP_TEXT);
        boolean enableStateChanged = propertyName == null || propertyName.equals(IAction.ENABLED)
                || propertyName.equals(IContributionManagerOverrides.P_ENABLED);
        boolean checkChanged = (action.getStyle() == IAction.AS_CHECK_BOX
                || action.getStyle() == IAction.AS_RADIO_BUTTON)
                && (propertyName == null || propertyName.equals(IAction.CHECKED));

        if (widget instanceof ToolItem) {
            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
            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) {
                String textToSet = showText ? text : ""; //$NON-NLS-1$
                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;
                }

                ExternalActionManager.ICallback callback = ExternalActionManager.getInstance().getCallback();
                String commandId = action.getActionDefinitionId();
                if ((callback != null) && (commandId != null) && (toolTip != null)) {
                    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) {
                boolean shouldBeEnabled = action.isEnabled() && isEnabledAllowed();

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

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

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

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

            if (textChanged) {
                int accelerator = 0;
                String acceleratorText = null;
                IAction updatedAction = getAction();
                String text = null;
                accelerator = updatedAction.getAccelerator();
                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 (int i = 0; i < triggerSequences.length; i++) {
                        final TriggerSequence triggerSequence = triggerSequences[i];
                        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);
                }

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

            if (imageChanged) {
                updateImages(false);
            }

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

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

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

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

            return;
        }

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

            if (imageChanged) {
                updateImages(false);
            }

            if (textChanged) {
                String text = action.getText();
                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);
                }
                String textToSet = showText ? text : ""; //$NON-NLS-1$
                button.setText(textToSet);
            }

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

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

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

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

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

From source file:org.xmind.ui.color.ColorPicker.java

License:Open Source License

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

        if (widget instanceof ToolItem) {
            //int toolbarStyle = SWT.NONE;

            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
            boolean showText = text != null && ((getMode() & MODE_FORCE_TEXT) != 0 || !hasImages(action));
            //                        && ((toolbarStyle & BFaceConstants.TOOLBAR_TEXT)!=0 || 
            //                                ((toolbarStyle & BFaceConstants.TOOLBAR_TEXT_RIGHT)!=0 && hasRightText));

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

            if (textChanged) {
                String textToSet = showText ? text : ""; //$NON-NLS-1$
                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;
                }
                // 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) {
                boolean shouldBeEnabled = action.isEnabled();
                //                            && isEnabledAllowed();

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

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

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

            if (colorChanged) {
                updateColors();
            }
            return;
        }

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

            if (textChanged) {
                int accelerator = 0;
                String acceleratorText = null;
                IAction updatedAction = getAction();
                String text = null;
                accelerator = updatedAction.getAccelerator();
                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 (("gtk".equals(SWT.getPlatform())) && (callback instanceof IBindingManagerCallback) //$NON-NLS-1$
                        && (commandId != null)) {
                    final IBindingManagerCallback bindingManagerCallback = (IBindingManagerCallback) callback;
                    final IKeyLookup lookup = KeyLookupFactory.getDefault();
                    final TriggerSequence[] triggerSequences = bindingManagerCallback
                            .getActiveBindingsFor(commandId);
                    for (int i = 0; i < triggerSequences.length; i++) {
                        final TriggerSequence triggerSequence = triggerSequences[i];
                        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);
                    }
                } else {
                    acceleratorText = Action.convertAccelerator(accelerator);
                }

                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) {
                    text = ""; //$NON-NLS-1$
                } else {
                    text = Action.removeAcceleratorText(text);
                }

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

            if (imageChanged) {
                updateImages(false);
            }

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

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

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

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

            if (colorChanged) {
                updateColors();
            }
            return;
        }

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

            if (imageChanged && updateImages(false)) {
                textChanged = false; // don't update text if it has an image
            }

            if (textChanged) {
                String text = action.getText();
                if (text == null) {
                    text = ""; //$NON-NLS-1$
                } else {
                    text = Action.removeAcceleratorText(text);
                }
                button.setText(text);
            }

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

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

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

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

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

            if (colorChanged) {
                updateColors();
            }
            return;
        }
    }

}

From source file:org.xmind.ui.dialogs.SimpleInfoPopupDialog.java

License:Open Source License

protected String getHyperlinkText(IAction action) {
    String text = action.getText();
    if (text != null) {
        text = Action.removeAcceleratorText(text);
        text = Action.removeMnemonics(text);
        return text;
    }/*  w  ww .  jav  a2 s . c  om*/
    return ""; //$NON-NLS-1$
}

From source file:org.xmind.ui.internal.mindmap.IconTipPart.java

License:Open Source License

protected IFigure createToolTip() {
    if (action != null) {
        String text = action.getText();
        String tooltip = action.getToolTipText();
        if (text != null || tooltip != null) {
            IFigure fig = new Figure();

            fig.setBorder(new MarginBorder(1, 3, 1, 3));

            ToolbarLayout layout = new ToolbarLayout(false);
            layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
            layout.setSpacing(7);//from ww  w. j  ava  2s.  c o  m
            fig.setLayoutManager(layout);

            //                if (actionId != null
            //                        && actionId.equals(MindMapActionFactory.EDIT_NOTES
            //                                .getId())) {
            //                    text = null;
            //                }

            if (text != null) {
                text = Action.removeAcceleratorText(text);
                text = Action.removeMnemonics(text);
                Label title = new Label(text);
                title.setFont(FontUtils.getBold(JFaceResources.DEFAULT_FONT));
                title.setForegroundColor(ColorConstants.darkGray);
                fig.add(title);
            }

            if (tooltip != null) {
                RotatableWrapLabel description = new RotatableWrapLabel(tooltip, RotatableWrapLabel.NORMAL);
                description.setTextAlignment(PositionConstants.LEFT);
                description.setPrefWidth(Math.min(Display.getCurrent().getClientArea().width / 3, 350));
                description.setFont(FontUtils.getRelativeHeight(JFaceResources.DEFAULT_FONT, -1));
                //                    description.setForegroundColor(ColorConstants.gray);
                description.setForegroundColor(ColorUtils.getColor(TEXT_COLOR));
                fig.add(description);
            }

            return fig;
        }
    }
    return super.createToolTip();
}

From source file:org.xmind.ui.richtext.AlignmentGroup.java

License:Open Source License

public void update(String propertyName) {
    if (widget == null || currentAction == null)
        return;//from   w  w  w  .  j a  va2  s  .  c  om

    boolean textChanged = propertyName == null || propertyName.equals(IAction.TEXT);
    boolean imageChanged = propertyName == null || propertyName.equals(IAction.IMAGE);
    boolean tooltipTextChanged = propertyName == null || propertyName.equals(IAction.TOOL_TIP_TEXT);
    boolean enableStateChanged = propertyName == null || propertyName.equals(IAction.ENABLED)
            || propertyName.equals(IContributionManagerOverrides.P_ENABLED);
    boolean checkChanged = (currentAction.getStyle() == IAction.AS_CHECK_BOX
            || currentAction.getStyle() == IAction.AS_RADIO_BUTTON)
            && (propertyName == null || propertyName.equals(IAction.CHECKED));
    ToolItem ti = (ToolItem) widget;
    String text = currentAction.getText();
    // the set text is shown only if there is no image or if forced
    // by MODE_FORCE_TEXT
    boolean showText = text != null && !hasImages(currentAction);

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

    if (textChanged) {
        String textToSet = showText ? text : ""; //$NON-NLS-1$
        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 = currentAction.getToolTipText();
        if ((toolTip == null) || (toolTip.length() == 0)) {
            toolTip = text;
        }

        ExternalActionManager.ICallback callback = ExternalActionManager.getInstance().getCallback();
        String commandId = currentAction.getActionDefinitionId();
        if ((callback != null) && (commandId != null) && (toolTip != null)) {
            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) {
        boolean shouldBeEnabled = currentAction.isEnabled() && isEnabledAllowed();

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

    if (checkChanged) {
        boolean bv = currentAction.isChecked();

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

}