Search in sources :

Example 1 with IActionCommandMappingService

use of org.eclipse.ui.internal.handlers.IActionCommandMappingService in project eclipse.platform.ui by eclipse-platform.

the class SubActionBars method setGlobalActionHandler.

/**
 * Add a handler for a window action.
 *
 * @param actionID an action ID declared in the registry
 * @param handler  an action which implements the action ID. <code>null</code>
 *                 may be passed to deregister a handler.
 */
@Override
public void setGlobalActionHandler(String actionID, IAction handler) {
    if (actionID == null) {
        /*
			 * Bug 124061. It used to be invalid to pass null as an action id, but some
			 * people still did it. Handle this case by trapping the exception and logging
			 * it.
			 */
        // $NON-NLS-1$
        WorkbenchPlugin.log("Cannot set the global action handler for a null action id");
        return;
    }
    if (handler instanceof CommandLegacyActionWrapper) {
        // this is a registration of a fake action for an already
        // registered handler
        // $NON-NLS-1$
        WorkbenchPlugin.log("Cannot feed a CommandLegacyActionWrapper back into the system");
        return;
    }
    if (handler instanceof CommandAction) {
        // still must not feed back into the system
        return;
    }
    if (handler != null) {
        // Update the action handlers.
        if (actionHandlers == null) {
            actionHandlers = new HashMap<>(11);
        }
        actionHandlers.put(actionID, handler);
        // Add a mapping from this action id to the command id.
        if (serviceLocator != null) {
            String commandId = null;
            final IActionCommandMappingService mappingService = serviceLocator.getService(IActionCommandMappingService.class);
            if (mappingService != null) {
                commandId = mappingService.getCommandId(actionID);
            }
            if (commandId == null) {
                commandId = handler.getActionDefinitionId();
            }
            // Update the handler activations.
            final IHandlerService service = serviceLocator.getService(IHandlerService.class);
            Map<String, IHandlerActivation> activationsByActionId = null;
            if (activationsByActionIdByServiceLocator == null) {
                activationsByActionIdByServiceLocator = new WeakHashMap<>();
                activationsByActionId = new HashMap<>();
                activationsByActionIdByServiceLocator.put(serviceLocator, activationsByActionId);
            } else {
                activationsByActionId = activationsByActionIdByServiceLocator.get(serviceLocator);
                if (activationsByActionId == null) {
                    activationsByActionId = new HashMap<>();
                    activationsByActionIdByServiceLocator.put(serviceLocator, activationsByActionId);
                } else if (activationsByActionId.containsKey(actionID)) {
                    final Object value = activationsByActionId.remove(actionID);
                    if (value instanceof IHandlerActivation) {
                        final IHandlerActivation activation = (IHandlerActivation) value;
                        actionIdByCommandId.remove(activation.getCommandId());
                        if (service != null) {
                            service.deactivateHandler(activation);
                        }
                        activation.getHandler().dispose();
                    }
                } else if (commandId != null && actionIdByCommandId.containsKey(commandId)) {
                    final Object value = activationsByActionId.remove(actionIdByCommandId.remove(commandId));
                    if (value instanceof IHandlerActivation) {
                        final IHandlerActivation activation = (IHandlerActivation) value;
                        if (service != null) {
                            service.deactivateHandler(activation);
                        }
                        activation.getHandler().dispose();
                    }
                }
            }
            if (commandId != null) {
                actionIdByCommandId.put(commandId, actionID);
                // Register this as a handler with the given definition id.
                // the expression gives the setGlobalActionHandler() a
                // priority.
                final IHandler actionHandler = new ActionHandler(handler);
                Expression handlerExpression = EXPRESSION;
                // XXX add new API in next release to avoid down-casting (bug 137091)
                if (this instanceof EditorActionBars) {
                    handlerExpression = ((EditorActionBars) this).getHandlerExpression();
                }
                if (service != null) {
                    final IHandlerActivation activation = service.activateHandler(commandId, actionHandler, handlerExpression);
                    activationsByActionId.put(actionID, activation);
                }
            }
        }
    } else {
        if (actionHandlers != null) {
            actionHandlers.remove(actionID);
        }
        // Remove the handler activation.
        if (serviceLocator != null) {
            final IHandlerService service = serviceLocator.getService(IHandlerService.class);
            if (activationsByActionIdByServiceLocator != null) {
                final Map<String, IHandlerActivation> activationsByActionId = activationsByActionIdByServiceLocator.get(serviceLocator);
                if ((activationsByActionId != null) && (activationsByActionId.containsKey(actionID))) {
                    final Object value = activationsByActionId.remove(actionID);
                    if (value instanceof IHandlerActivation) {
                        final IHandlerActivation activation = (IHandlerActivation) value;
                        actionIdByCommandId.remove(activation.getCommandId());
                        service.deactivateHandler(activation);
                        activation.getHandler().dispose();
                    }
                }
            }
        }
    }
    actionHandlersChanged = true;
}
Also used : EditorActionBars(org.eclipse.ui.internal.EditorActionBars) IActionCommandMappingService(org.eclipse.ui.internal.handlers.IActionCommandMappingService) CommandAction(org.eclipse.ui.internal.actions.CommandAction) CommandLegacyActionWrapper(org.eclipse.ui.internal.handlers.CommandLegacyActionWrapper) IHandlerService(org.eclipse.ui.handlers.IHandlerService) Expression(org.eclipse.core.expressions.Expression) IHandlerActivation(org.eclipse.ui.handlers.IHandlerActivation) IHandler(org.eclipse.core.commands.IHandler) ActionHandler(org.eclipse.jface.commands.ActionHandler)

Example 2 with IActionCommandMappingService

use of org.eclipse.ui.internal.handlers.IActionCommandMappingService in project eclipse.platform.ui by eclipse-platform.

the class WorkbenchWindow method registerGlobalAction.

void registerGlobalAction(IAction globalAction) {
    String commandId = globalAction.getActionDefinitionId();
    if (commandId != null) {
        final Object value = globalActionHandlersByCommandId.remove(commandId);
        if (value instanceof ActionHandler) {
            // This handler is about to get clobbered, so dispose it.
            final ActionHandler handler = (ActionHandler) value;
            handler.dispose();
        }
        if (globalAction instanceof CommandAction) {
            final String actionId = globalAction.getId();
            if (actionId != null) {
                final IActionCommandMappingService mappingService = serviceLocator.getService(IActionCommandMappingService.class);
                mappingService.map(actionId, commandId);
            }
        } else {
            globalActionHandlersByCommandId.put(commandId, new ActionHandler(globalAction));
        }
    }
    submitGlobalActions();
}
Also used : IActionCommandMappingService(org.eclipse.ui.internal.handlers.IActionCommandMappingService) CommandAction(org.eclipse.ui.internal.actions.CommandAction) EObject(org.eclipse.emf.ecore.EObject) ActionHandler(org.eclipse.jface.commands.ActionHandler)

Example 3 with IActionCommandMappingService

use of org.eclipse.ui.internal.handlers.IActionCommandMappingService in project eclipse.platform.ui by eclipse-platform.

the class LegacyActionPersistence method convertActionToHandler.

/**
 * <p>
 * Extracts the handler information from the given action element. These are
 * registered with the handler service. They are always active.
 * </p>
 *
 * @param element              The action element from which the handler should
 *                             be read; must not be <code>null</code>.
 * @param actionId             The identifier of the action for which a handler
 *                             is being created; must not be <code>null</code>.
 * @param command              The command for which this handler applies; must
 *                             not be <code>null</code>.
 * @param activeWhenExpression The expression controlling when the handler is
 *                             active; may be <code>null</code>.
 * @param viewId               The view to which this handler is associated.
 *                             This value is required if this is a view action;
 *                             otherwise it can be <code>null</code>.
 * @param warningsToLog        The collection of warnings while parsing this
 *                             extension point; must not be <code>null</code>.
 */
private void convertActionToHandler(final IConfigurationElement element, final String actionId, final ParameterizedCommand command, final Expression activeWhenExpression, final String viewId, final List<IStatus> warningsToLog) {
    // Check to see if this is a retargettable action.
    final boolean retarget = readBoolean(element, ATT_RETARGET, false);
    final boolean classAvailable = (element.getAttribute(ATT_CLASS) != null) || (element.getChildren(TAG_CLASS).length != 0);
    // Read the class attribute.
    String classString = readOptional(element, ATT_CLASS);
    if (classAvailable && classString == null) {
        classString = readOptional(element.getChildren(TAG_CLASS)[0], ATT_CLASS);
    }
    if (retarget) {
        if (classAvailable && !isPulldown(element)) {
            addWarning(// $NON-NLS-1$
            warningsToLog, // $NON-NLS-1$
            "The class was not null but retarget was set to true", element, actionId, ATT_CLASS, classString);
        }
        // Add a mapping from this action id to the command id.
        final IActionCommandMappingService mappingService = window.getService(IActionCommandMappingService.class);
        if (mappingService != null) {
            mappingService.map(actionId, command.getId());
        } else {
            // this is probably the shutdown case where the service has
            // already disposed.
            addWarning(// $NON-NLS-1$
            warningsToLog, // $NON-NLS-1$
            "Retarget service unavailable", element, actionId);
        }
        // This is nothing more to be done.
        return;
    } else if (!classAvailable) {
        addWarning(// $NON-NLS-1$
        warningsToLog, // $NON-NLS-1$
        "There was no class provided, and the action is not retargettable", element, actionId);
        // There is nothing to be done.
        return;
    }
    // Read the enablesFor attribute, and enablement and selection elements.
    SelectionEnabler enabler = null;
    if (element.getAttribute(ATT_ENABLES_FOR) != null) {
        enabler = new SelectionEnabler(element);
    } else {
        IConfigurationElement[] kids = element.getChildren(TAG_ENABLEMENT);
        if (kids.length > 0) {
            enabler = new SelectionEnabler(element);
        }
    }
    final Expression enabledWhenExpression;
    if (enabler == null) {
        enabledWhenExpression = null;
    } else {
        enabledWhenExpression = new LegacySelectionEnablerWrapper(enabler, window);
    }
    /*
		 * Create the handler. TODO The image style is read at the workbench level, but
		 * it is hard to communicate this information to this point. For now, I'll pass
		 * null, but this ultimately won't work.
		 */
    final ActionDelegateHandlerProxy handler = new ActionDelegateHandlerProxy(element, ATT_CLASS, actionId, command, window, null, enabledWhenExpression, viewId);
    // Read the help context id.
    final String helpContextId = readOptional(element, ATT_HELP_CONTEXT_ID);
    if (helpContextId != null) {
        commandService.setHelpContextId(handler, helpContextId);
    }
    // Activate the handler.
    final String commandId = command.getId();
    final IHandlerService service = window.getService(IHandlerService.class);
    final IHandlerActivation handlerActivation;
    if (activeWhenExpression == null) {
        handlerActivation = service.activateHandler(commandId, handler);
    } else {
        handlerActivation = service.activateHandler(commandId, handler, activeWhenExpression);
    }
    handlerActivations.add(handlerActivation);
}
Also used : SelectionEnabler(org.eclipse.ui.SelectionEnabler) IHandlerService(org.eclipse.ui.handlers.IHandlerService) Expression(org.eclipse.core.expressions.Expression) LegacyActionSetExpression(org.eclipse.ui.internal.expressions.LegacyActionSetExpression) LegacyEditorContributionExpression(org.eclipse.ui.internal.expressions.LegacyEditorContributionExpression) LegacyViewContributionExpression(org.eclipse.ui.internal.expressions.LegacyViewContributionExpression) ActionDelegateHandlerProxy(org.eclipse.ui.internal.handlers.ActionDelegateHandlerProxy) IActionCommandMappingService(org.eclipse.ui.internal.handlers.IActionCommandMappingService) IHandlerActivation(org.eclipse.ui.handlers.IHandlerActivation) LegacySelectionEnablerWrapper(org.eclipse.ui.internal.expressions.LegacySelectionEnablerWrapper) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement)

Example 4 with IActionCommandMappingService

use of org.eclipse.ui.internal.handlers.IActionCommandMappingService in project eclipse.platform.ui by eclipse-platform.

the class LegacyActionPersistence method convertActionToCommand.

/**
 * Determine which command to use. This is slightly complicated as actions do
 * not have to have commands, but the new architecture requires it. As such, we
 * will auto-generate a command for the action if the definitionId is missing or
 * points to a command that does not yet exist. All such command identifiers are
 * prefixed with AUTOGENERATED_COMMAND_ID_PREFIX.
 *
 * @param element       The action element from which a command must be
 *                      generated; must not be <code>null</code>.
 * @param primaryId     The primary identifier to use when auto-generating a
 *                      command; must not be <code>null</code>.
 * @param secondaryId   The secondary identifier to use when auto-generating a
 *                      command; must not be <code>null</code>.
 * @param warningsToLog The collection of warnings logged while reading the
 *                      extension point; must not be <code>null</code>.
 * @return the fully-parameterized command; <code>null</code> if an error
 *         occurred.
 */
private ParameterizedCommand convertActionToCommand(final IConfigurationElement element, final String primaryId, final String secondaryId, final List<IStatus> warningsToLog) {
    String commandId = readOptional(element, ATT_DEFINITION_ID);
    Command command = null;
    if (commandId != null) {
        command = commandService.getCommand(commandId);
    }
    final IActionCommandMappingService mappingService = window.getService(IActionCommandMappingService.class);
    String label = null;
    if ((commandId == null) || (!command.isDefined())) {
        // Add a mapping from this action id to the command id.
        if (commandId == null && mappingService != null) {
            commandId = mappingService.getGeneratedCommandId(primaryId, secondaryId);
        }
        if (commandId == null) {
            // $NON-NLS-1$
            WorkbenchPlugin.log("MappingService unavailable");
            return null;
        }
        // Read the label attribute.
        label = readRequired(// $NON-NLS-1$
        element, // $NON-NLS-1$
        ATT_LABEL, // $NON-NLS-1$
        warningsToLog, // $NON-NLS-1$
        "Actions require a non-empty label or definitionId", commandId);
        if (label == null) {
            label = WorkbenchMessages.LegacyActionPersistence_AutogeneratedCommandName;
        }
        /*
			 * Read the tooltip attribute. The tooltip is really the description of the
			 * command.
			 */
        final String tooltip = readOptional(element, ATT_TOOLTIP);
        // Define the command.
        command = commandService.getCommand(commandId);
        final Category category = commandService.getCategory(null);
        final String name = LegacyActionTools.removeAcceleratorText(Action.removeMnemonics(label));
        command.define(name, tooltip, category, null);
        // TODO Decide the command state.
        final String style = readOptional(element, ATT_STYLE);
        if (STYLE_RADIO.equals(style)) {
            final State state = new RadioState();
            // TODO How to set the id?
            final boolean checked = readBoolean(element, ATT_STATE, false);
            state.setValue((checked) ? Boolean.TRUE : Boolean.FALSE);
            command.addState(IMenuStateIds.STYLE, state);
        } else if (STYLE_TOGGLE.equals(style)) {
            final State state = new ToggleState();
            final boolean checked = readBoolean(element, ATT_STATE, false);
            state.setValue((checked) ? Boolean.TRUE : Boolean.FALSE);
            command.addState(IMenuStateIds.STYLE, state);
        }
    }
    // and the actionId
    if (mappingService != null) {
        mappingService.map(mappingService.getGeneratedCommandId(primaryId, secondaryId), commandId);
    }
    return new ParameterizedCommand(command, null);
}
Also used : Category(org.eclipse.core.commands.Category) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) State(org.eclipse.core.commands.State) RadioState(org.eclipse.jface.commands.RadioState) ToggleState(org.eclipse.jface.commands.ToggleState) IActionCommandMappingService(org.eclipse.ui.internal.handlers.IActionCommandMappingService) ToggleState(org.eclipse.jface.commands.ToggleState) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) RadioState(org.eclipse.jface.commands.RadioState)

Example 5 with IActionCommandMappingService

use of org.eclipse.ui.internal.handlers.IActionCommandMappingService in project gda-core by openGDA.

the class ApplicationActionBarAdvisor method getItem.

private IContributionItem getItem(String actionId, String commandId, String image, String disabledImage, String label, String tooltip, String helpContextId) {
    ISharedImages sharedImages = getWindow().getWorkbench().getSharedImages();
    IActionCommandMappingService acms = getWindow().getService(IActionCommandMappingService.class);
    acms.map(actionId, commandId);
    CommandContributionItemParameter commandParm = new CommandContributionItemParameter(getWindow(), actionId, commandId, null, sharedImages.getImageDescriptor(image), sharedImages.getImageDescriptor(disabledImage), null, label, null, tooltip, CommandContributionItem.STYLE_PUSH, helpContextId, false);
    return new CommandContributionItem(commandParm);
}
Also used : ISharedImages(org.eclipse.ui.ISharedImages) IActionCommandMappingService(org.eclipse.ui.internal.handlers.IActionCommandMappingService) CommandContributionItemParameter(org.eclipse.ui.menus.CommandContributionItemParameter) CommandContributionItem(org.eclipse.ui.menus.CommandContributionItem)

Aggregations

IActionCommandMappingService (org.eclipse.ui.internal.handlers.IActionCommandMappingService)8 IHandlerService (org.eclipse.ui.handlers.IHandlerService)3 Expression (org.eclipse.core.expressions.Expression)2 ActionHandler (org.eclipse.jface.commands.ActionHandler)2 ISharedImages (org.eclipse.ui.ISharedImages)2 IHandlerActivation (org.eclipse.ui.handlers.IHandlerActivation)2 CommandAction (org.eclipse.ui.internal.actions.CommandAction)2 CommandContributionItem (org.eclipse.ui.menus.CommandContributionItem)2 CommandContributionItemParameter (org.eclipse.ui.menus.CommandContributionItemParameter)2 Category (org.eclipse.core.commands.Category)1 Command (org.eclipse.core.commands.Command)1 IHandler (org.eclipse.core.commands.IHandler)1 NotEnabledException (org.eclipse.core.commands.NotEnabledException)1 NotHandledException (org.eclipse.core.commands.NotHandledException)1 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)1 State (org.eclipse.core.commands.State)1 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)1 EObject (org.eclipse.emf.ecore.EObject)1 RadioState (org.eclipse.jface.commands.RadioState)1 ToggleState (org.eclipse.jface.commands.ToggleState)1