package org.yamcs.studio.ui;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.services.IServiceLocator;
import org.eclipse.ui.services.ISourceProviderService;

/**
 * Various 'improvements' to RCP code to shorten our code a bit
 */
public class RCPUtils {

    private RCPUtils() {
    }

    /**
     * Finds a source provider within the active workbench for the execution event
     */
    public static <T> T findSourceProvider(ExecutionEvent evt, String sourceName, Class<T> expectedClass) {
        IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(evt);
        return findSourceProvider(workbenchWindow, sourceName, expectedClass);
    }

    /**
     * Finds a source provider using the specified locator (for example, a workbench window)
     */
    @SuppressWarnings("unchecked")
    public static <T> T findSourceProvider(IServiceLocator locator, String sourceName, Class<T> expectedClass) {
        ISourceProviderService service = (ISourceProviderService) locator
                .getService(ISourceProviderService.class);
        return (T) service.getSourceProvider(sourceName);
    }
}

@@ -34,132 +34,399 @@
 ort 
-org.eclipse.core.commands.ExecutionEvent;%0Aimport org.eclipse.ui.IWorkbenchWindow;%0Aimport org.eclipse.ui.handlers.HandlerUtil
+java.util.HashMap;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0Aimport org.eclipse.core.commands.Command;%0Aimport org.eclipse.core.commands.ExecutionEvent;%0Aimport org.eclipse.ui.IWorkbenchWindow;%0Aimport org.eclipse.ui.PlatformUI;%0Aimport org.eclipse.ui.commands.ICommandService;%0Aimport org.eclipse.ui.handlers.HandlerUtil;%0Aimport org.eclipse.ui.services.IEvaluationService
 ;%0Aim
@@ -640,26 +640,77 @@
 ate 
-RCPUtils() %7B%0A    %7D
+static final Logger log = Logger.getLogger(RCPUtils.class.getName());
 %0A%0A  
@@ -1539,11 +1539,711 @@
 );%0A    %7D
+%0A%0A    public static void runCommand(String commandId) %7B%0A        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();%0A        ICommandService commandService = (ICommandService) window.getService(ICommandService.class);%0A        IEvaluationService evaluationService = (IEvaluationService) window.getService(IEvaluationService.class);%0A        try %7B%0A            Command cmd = commandService.getCommand(commandId);%0A            cmd.executeWithChecks(new ExecutionEvent(cmd, new HashMap%3CString, String%3E(), null, evaluationService.getCurrentState()));%0A        %7D catch (Exception exception) %7B%0A            log.log(Level.SEVERE, %22Could not execute command%22, exception);%0A        %7D%0A    %7D
 %0A%7D%0A

Add utility method for running workbench commands