Search in sources :

Example 1 with CommandNotMappedException

use of org.eclipse.ui.actions.CommandNotMappedException in project eclipse.platform.text by eclipse.

the class AbstractTextEditor method findContributedAction.

/**
 * Returns the action with the given action id that has been contributed via XML to this editor.
 * The lookup honors the dependencies of plug-ins.
 *
 * @param actionID the action id to look up
 * @return the action that has been contributed
 * @since 2.0
 */
private IAction findContributedAction(String actionID) {
    List<IConfigurationElement> actions = new ArrayList<>();
    // $NON-NLS-1$
    IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(PlatformUI.PLUGIN_ID, "editorActions");
    for (int i = 0; i < elements.length; i++) {
        IConfigurationElement element = elements[i];
        if (TAG_CONTRIBUTION_TYPE.equals(element.getName())) {
            IWorkbenchPartSite site = getSite();
            if (site == null) {
                return null;
            }
            if (// $NON-NLS-1$
            !site.getId().equals(element.getAttribute("targetID")))
                continue;
            // $NON-NLS-1$
            IConfigurationElement[] children = element.getChildren("action");
            for (int j = 0; j < children.length; j++) {
                IConfigurationElement child = children[j];
                if (// $NON-NLS-1$
                actionID.equals(child.getAttribute("actionID")))
                    actions.add(child);
            }
        }
    }
    int actionSize = actions.size();
    if (actionSize > 0) {
        IConfigurationElement element;
        if (actionSize > 1) {
            IConfigurationElement[] actionArray = actions.toArray(new IConfigurationElement[actionSize]);
            ConfigurationElementSorter sorter = new ConfigurationElementSorter() {

                @Override
                public IConfigurationElement getConfigurationElement(Object object) {
                    return (IConfigurationElement) object;
                }
            };
            sorter.sort(actionArray);
            element = actionArray[0];
        } else
            element = actions.get(0);
        try {
            return new ContributedAction(getSite(), element);
        } catch (CommandNotMappedException e) {
        // out of luck, no command action mapping
        }
    }
    return null;
}
Also used : IWorkbenchPartSite(org.eclipse.ui.IWorkbenchPartSite) CommandNotMappedException(org.eclipse.ui.actions.CommandNotMappedException) ContributedAction(org.eclipse.ui.actions.ContributedAction) ArrayList(java.util.ArrayList) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) Point(org.eclipse.swt.graphics.Point)

Aggregations

ArrayList (java.util.ArrayList)1 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)1 Point (org.eclipse.swt.graphics.Point)1 IWorkbenchPartSite (org.eclipse.ui.IWorkbenchPartSite)1 CommandNotMappedException (org.eclipse.ui.actions.CommandNotMappedException)1 ContributedAction (org.eclipse.ui.actions.ContributedAction)1