package org.yamcs.studio.core.ui.utils;

import java.util.Calendar;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.services.IEvaluationService;
import org.eclipse.ui.services.IServiceLocator;
import org.eclipse.ui.services.ISourceProviderService;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.yamcs.studio.core.model.TimeCatalogue;

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

    private static final Logger log = Logger.getLogger(RCPUtils.class.getName());

    /**
     * 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);
    }

    public static void runCommand(String commandId) {
        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        ICommandService commandService = (ICommandService) window.getService(ICommandService.class);
        IEvaluationService evaluationService = (IEvaluationService) window.getService(IEvaluationService.class);
        try {
            Command cmd = commandService.getCommand(commandId);
            cmd.executeWithChecks(
                    new ExecutionEvent(cmd, new HashMap<String, String>(), null, evaluationService.getCurrentState()));
        } catch (Exception exception) {
            log.log(Level.SEVERE, "Could not execute command " + commandId, exception);
        }
    }

    public static ImageDescriptor getImageDescriptor(Class<?> classFromBundle, String path) {
        Bundle bundle = FrameworkUtil.getBundle(classFromBundle);
        return ImageDescriptor.createFromURL(FileLocator.find(bundle, new Path(path), null));
    }

    public static Calendar toCalendar(DateTime dateWidget, DateTime timeWidget) {
        Calendar cal = Calendar.getInstance(TimeCatalogue.getInstance().getTimeZone());
        cal.set(dateWidget.getYear(), dateWidget.getMonth(), dateWidget.getDay());
        cal.set(Calendar.HOUR_OF_DAY, timeWidget.getHours());
        cal.set(Calendar.MINUTE, timeWidget.getMinutes());
        cal.set(Calendar.SECOND, timeWidget.getSeconds());
        cal.set(Calendar.MILLISECOND, 0);
        return cal;
    }
}

@@ -327,16 +327,68 @@
 e.Path;%0A
+import org.eclipse.jface.action.IStatusLineManager;%0A
 import o
@@ -471,16 +471,49 @@
 teTime;%0A
+import org.eclipse.ui.IViewSite;%0A
 import o
@@ -2687,32 +2687,1211 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Sets a message in the lower left status line. These messages are by%0A     * rcp-design associated with a view.%0A     *%0A     * @param viewId%0A     *            the view from which the message originates%0A     */%0A    public static void setStatusMessage(String viewId, String message) %7B%0A        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();%0A        IViewSite site = window.getActivePage().findView(viewId).getViewSite();%0A        IStatusLineManager mgr = site.getActionBars().getStatusLineManager();%0A        mgr.setMessage(message);%0A    %7D%0A%0A    /**%0A     * Sets an error message in the lower left status line. These messages are%0A     * by rcp-design associated with a view.%0A     *%0A     * @param viewId%0A     *            the view from which the message originates%0A     */%0A    public static void setStatusErrorMessage(String viewId, String message) %7B%0A        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();%0A        IViewSite site = window.getActivePage().findView(viewId).getViewSite();%0A        IStatusLineManager mgr = site.getActionBars().getStatusLineManager();%0A        mgr.setErrorMessage(message);%0A    %7D%0A%0A
     public stati

Add utility methods for status line msgs