Example usage for org.eclipse.jface.action ExternalActionManager getInstance

List of usage examples for org.eclipse.jface.action ExternalActionManager getInstance

Introduction

In this page you can find the example usage for org.eclipse.jface.action ExternalActionManager getInstance.

Prototype

public static ExternalActionManager getInstance() 

Source Link

Document

Retrieves the current singleton instance of this class.

Usage

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

License:Open Source License

/**
 * The <code>ActionContributionItemEx</code> implementation of this
 * <code>IContributionItem</code> method creates an SWT <code>Button</code>
 * for the action using the action's style. If the action's checked property
 * has been set, the button is created and primed to the value of the checked
 * property./*from w w  w. ja v  a 2s  .c  o m*/
 */
@Override
public void fill(Composite parent) {
    if ((widget == null) && (parent != null)) {
        int flags = SWT.PUSH;
        if (action != null) {
            if (action.getStyle() == IAction.AS_CHECK_BOX) {
                flags = SWT.TOGGLE;
            }
            if (action.getStyle() == IAction.AS_RADIO_BUTTON) {
                flags = SWT.RADIO;
            }
        }

        final Button b = new Button(parent, flags);
        b.setData(this);
        b.addListener(SWT.Dispose, getButtonListener());
        // Don't hook a dispose listener on the parent
        b.addListener(SWT.Selection, getButtonListener());
        if (action.getHelpListener() != null) {
            b.addHelpListener(action.getHelpListener());
        }
        widget = b;

        update(null);

        // Attach some extra listeners.
        action.addPropertyChangeListener(propertyListener);
        if (action != null) {
            final String commandId = action.getActionDefinitionId();
            final ExternalActionManager.ICallback callback = ExternalActionManager.getInstance().getCallback();

            if ((callback != null) && (commandId != null)) {
                callback.addPropertyChangeListener(commandId, actionTextListener);
            }
        }
    }
}

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

License:Open Source License

/**
 * The <code>ActionContributionItemEx</code> implementation of this
 * <code>IContributionItem</code> method creates an SWT <code>MenuItem</code>
 * for the action using the action's style. If the action's checked property
 * has been set, a button is created and primed to the value of the checked
 * property. If the action's menu creator property has been set, a cascading
 * submenu is created.//ww  w  .ja  v  a 2  s  .co m
 */
@Override
public void fill(Menu parent, int index) {
    if ((widget == null) && (parent != null)) {
        int flags = SWT.PUSH;
        if (action != null) {
            final int style = action.getStyle();
            if (style == IAction.AS_CHECK_BOX) {
                flags = SWT.CHECK;
            } else if (style == IAction.AS_RADIO_BUTTON) {
                flags = SWT.RADIO;
            } else if (style == IAction.AS_DROP_DOWN_MENU) {
                flags = SWT.CASCADE;
            }
        }

        MenuItem mi = null;
        if (index >= 0) {
            mi = new MenuItem(parent, flags, index);
        } else {
            mi = new MenuItem(parent, flags);
        }
        widget = mi;

        mi.setData(this);
        mi.addListener(SWT.Dispose, getMenuItemListener());
        mi.addListener(SWT.Selection, getMenuItemListener());
        if (action.getHelpListener() != null) {
            mi.addHelpListener(action.getHelpListener());
        }

        if (flags == SWT.CASCADE) {
            // just create a proxy for now, if the user shows it then
            // fill it in
            final Menu subMenu = new Menu(parent);
            subMenu.addListener(SWT.Show, getMenuCreatorListener());
            subMenu.addListener(SWT.Hide, getMenuCreatorListener());
            mi.setMenu(subMenu);
        }

        update(null);

        // Attach some extra listeners.
        action.addPropertyChangeListener(propertyListener);
        if (action != null) {
            final String commandId = action.getActionDefinitionId();
            final ExternalActionManager.ICallback callback = ExternalActionManager.getInstance().getCallback();

            if ((callback != null) && (commandId != null)) {
                callback.addPropertyChangeListener(commandId, actionTextListener);
            }
        }
    }
}

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

License:Open Source License

/**
 * The <code>ActionContributionItemEx</code> implementation of this ,
 * <code>IContributionItem</code> method creates an SWT <code>ToolItem</code>
 * for the action using the action's style. If the action's checked property
 * has been set, a button is created and primed to the value of the checked
 * property. If the action's menu creator property has been set, a drop-down
 * tool item is created./*  ww  w  .  ja  v a2  s  .  c o m*/
 */
@Override
public void fill(ToolBar parent, int index) {
    if ((widget == null) && (parent != null)) {
        int flags = SWT.PUSH;
        if (action != null) {
            final int style = action.getStyle();
            if (style == IAction.AS_CHECK_BOX) {
                flags = SWT.CHECK;
            } else if (style == IAction.AS_RADIO_BUTTON) {
                flags = SWT.RADIO;
            } else if (style == IAction.AS_DROP_DOWN_MENU) {
                flags = SWT.DROP_DOWN;
            }
        }

        ToolItem ti = null;
        if (index >= 0) {
            ti = new ToolItem(parent, flags, index);
        } else {
            ti = new ToolItem(parent, flags);
        }
        ti.setData(this);
        ti.addListener(SWT.Selection, getToolItemListener());
        ti.addListener(SWT.Dispose, getToolItemListener());

        widget = ti;

        update(null);

        // Attach some extra listeners.
        if (action != null) {
            action.addPropertyChangeListener(propertyListener);
            final String commandId = action.getActionDefinitionId();
            final ExternalActionManager.ICallback callback = ExternalActionManager.getInstance().getCallback();

            if ((callback != null) && (commandId != null)) {
                callback.addPropertyChangeListener(commandId, actionTextListener);
            }
        }
    }
}

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

License:Open Source License

/**
 * Handles a widget dispose event for the widget corresponding to this item.
 *//*ww w.ja  va 2s  . c om*/
private void handleWidgetDispose(Event e) {
    // Check if our widget is the one being disposed.
    if (e.widget == widget) {
        // Dispose of the menu creator.
        if ((action.getStyle() == IAction.AS_DROP_DOWN_MENU) && menuCreatorCalled) {
            final IMenuCreator mc = action.getMenuCreator();
            if (mc != null) {
                mc.dispose();
            }
        }

        // Unhook all of the listeners.
        action.removePropertyChangeListener(propertyListener);
        if (action != null) {
            final String commandId = action.getActionDefinitionId();
            final ExternalActionManager.ICallback callback = ExternalActionManager.getInstance().getCallback();

            if ((callback != null) && (commandId != null)) {
                callback.removePropertyChangeListener(commandId, actionTextListener);
            }
        }

        // Clear the widget field.
        widget = null;

        disposeOldImages();
    }
}

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

License:Open Source License

/**
 * Handles a widget selection event./*from w w  w  .ja  v a 2s  . c o m*/
 */
private void handleWidgetSelection(Event e, boolean selection) {

    final Widget item = e.widget;
    if (item != null) {
        final int style = item.getStyle();

        if ((style & (SWT.TOGGLE | SWT.CHECK)) != 0) {
            if (action.getStyle() == IAction.AS_CHECK_BOX) {
                action.setChecked(selection);
            }
        } else if ((style & SWT.RADIO) != 0) {
            if (action.getStyle() == IAction.AS_RADIO_BUTTON) {
                action.setChecked(selection);
            }
        } else if ((style & SWT.DROP_DOWN) != 0) {
            if (e.detail == 4) { // on drop-down button
                if (action.getStyle() == IAction.AS_DROP_DOWN_MENU) {
                    final IMenuCreator mc = action.getMenuCreator();
                    menuCreatorCalled = true;
                    final ToolItem ti = (ToolItem) item;
                    // we create the menu as a sub-menu of "dummy" so that
                    // we can use
                    // it in a cascading menu too.
                    // If created on a SWT control we would get an SWT
                    // error...
                    // Menu dummy= new Menu(ti.getParent());
                    // Menu m= mc.getMenu(dummy);
                    // dummy.dispose();
                    if (mc != null) {
                        final Menu m = mc.getMenu(ti.getParent());
                        if (m != null) {
                            // position the menu below the drop down item
                            final Point point = ti.getParent().toDisplay(new Point(e.x, e.y));
                            m.setLocation(point.x, point.y); // waiting
                            // for SWT
                            // 0.42
                            m.setVisible(true);
                            return; // we don't fire the action
                        }
                    }
                }
            }
        }

        ExternalActionManager.IExecuteCallback callback = null;
        final String actionDefinitionId = action.getActionDefinitionId();
        if (actionDefinitionId != null) {
            final Object obj = ExternalActionManager.getInstance().getCallback();
            if (obj instanceof ExternalActionManager.IExecuteCallback) {
                callback = (ExternalActionManager.IExecuteCallback) obj;
            }
        }

        // Ensure action is enabled first.
        // See 1GAN3M6: ITPUI:WINNT - Any IAction in the workbench can be
        // executed while disabled.
        if (action.isEnabled()) {
            final boolean trace = Policy.TRACE_ACTIONS;

            long ms = 0L;
            if (trace) {
                ms = System.currentTimeMillis();
                System.out.println("Running action: " + action.getText()); //$NON-NLS-1$
            }

            IPropertyChangeListener resultListener = null;
            if (callback != null) {
                resultListener = new IPropertyChangeListener() {
                    @Override
                    public void propertyChange(PropertyChangeEvent event) {
                        // Check on result
                        if (event.getProperty().equals(IAction.RESULT)) {
                            if (event.getNewValue() instanceof Boolean) {
                                result = (Boolean) event.getNewValue();
                            }
                        }
                    }
                };
                action.addPropertyChangeListener(resultListener);
                callback.preExecute(action, e);
            }

            action.runWithEvent(e);

            if (callback != null) {
                if ((result == null) || result.equals(Boolean.TRUE)) {
                    callback.postExecuteSuccess(action, Boolean.TRUE);
                } else {
                    callback.postExecuteFailure(action,
                            new ExecutionException(action.getText() + " returned failure.")); //$NON-NLS-1$
                }
            }

            if (resultListener != null) {
                result = null;
                action.removePropertyChangeListener(resultListener);
            }
            if (trace) {
                System.out
                        .println((System.currentTimeMillis() - ms) + " ms to run action: " + action.getText()); //$NON-NLS-1$
            }
        } else {
            if (callback != null) {
                callback.notEnabled(action, new NotEnabledException(action.getText() + " is not enabled.")); //$NON-NLS-1$
            }
        }
    }
}

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

License:Open Source License

/**
 * Returns whether the command corresponding to this action is active.
 *//*from   w  w w . ja v a 2 s  .co m*/
private boolean isCommandActive() {
    final IAction actionToCheck = getAction();

    if (actionToCheck != null) {
        final String commandId = actionToCheck.getActionDefinitionId();
        final ExternalActionManager.ICallback callback = ExternalActionManager.getInstance().getCallback();

        if (callback != null) {
            return callback.isActive(commandId);
        }
    }
    return true;
}

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  ww.  j a va2 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.github.haixing_hu.swt.menu.MenuManagerEx.java

License:Open Source License

/**
 * Returns the text shown in the menu, potentially with a shortcut appended.
 *
 * @return the menu text//  ww  w .  j av a2  s. com
 */
public String getMenuText() {
    if (definitionId == null) {
        return menuText;
    }
    final ExternalActionManager.ICallback callback = ExternalActionManager.getInstance().getCallback();
    if (callback != null) {
        final String shortCut = callback.getAcceleratorText(definitionId);
        if (shortCut == null) {
            return menuText;
        }
        return menuText + "\t" + shortCut; //$NON-NLS-1$
    }
    return menuText;
}

From source file:com.github.haixing_hu.swt.menu.MenuManagerEx.java

License:Open Source License

@Override
public void update(String property) {
    final IContributionItem items[] = getItems();

    for (final IContributionItem item : items) {
        item.update(property);/*from  w w  w  .  j a v  a2 s .c  o  m*/
    }

    if ((menu != null) && !menu.isDisposed() && (menu.getParentItem() != null)) {
        if (IAction.TEXT.equals(property)) {
            String text = getOverrides().getText(this);

            if (text == null) {
                text = getMenuText();
            }

            if (text != null) {
                final ExternalActionManager.ICallback callback = ExternalActionManager.getInstance()
                        .getCallback();

                if (callback != null) {
                    final int index = text.indexOf('&');

                    if ((index >= 0) && (index < (text.length() - 1))) {
                        final char character = Character.toUpperCase(text.charAt(index + 1));

                        if (callback.isAcceleratorInUse(SWT.ALT | character) && isTopLevelMenu()) {
                            if (index == 0) {
                                text = text.substring(1);
                            } else {
                                text = text.substring(0, index) + text.substring(index + 1);
                            }
                        }
                    }
                }

                menu.getParentItem().setText(text);
            }
        } else if (IAction.IMAGE.equals(property) && (image != null)) {
            final LocalResourceManager localManager = new LocalResourceManager(JFaceResources.getResources());
            menu.getParentItem().setImage(localManager.createImage(image));
            disposeOldImages();
            imageManager = localManager;
        }
    }
}

From source file:com.mousefeed.eclipse.ActionActionDescGenerator.java

License:Open Source License

/**
 * Finds keyboard shortcut for the action definition id.
 * /*from   www .  j  av a  2s  .  co  m*/
 * @param actionDefinitionId
 *            the action definition id to search accelerator for. If blank,
 *            the method returns <code>null</code>
 * @return A human-readable string representation of the accelerator. Is
 *         <code>null</code> if can't be found.
 */
private String findAcceleratorForActionDefinition(final String definitionId) {
    if (StringUtils.isBlank(definitionId)) {
        return null;
    }
    final ICallback callback = ExternalActionManager.getInstance().getCallback();
    return callback.getAcceleratorText(definitionId);
}