package it.mice.voila2.sync.plugin;

import java.io.IOException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.widgets.Display;
import org.eclipse.team.core.TeamException;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "it.mice.voila2.sync.plugin"; //$NON-NLS-1$

	/**
	 * This is the provider ID of the plugin as defined in the plugin.xml
	 */
	public static final String PROVIDER_ID = PLUGIN_ID + ".FileSystemProvider"; //$NON-NLS-1$

	// The shared instance
	private static Activator plugin;
	
	/**
	 * The constructor
	 */
	public Activator() {
	}

	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	/**
	 * Helper method to convert a CoreException into a TeamException.
	 * We do this to maintain the core status and code. This type of
	 * mapping may not be appropriate in more complicated exception
	 * handling situations.
	 *
	 * @param e the CoreException
	 */
	public static TeamException wrapException(CoreException e) {
		return new TeamException(e.getStatus());
	}

	/**
	 * Helper method to convert an IOException into a TeamException.
	 * This type of mapping may not be appropriate in more complicated
	 * exception handling situations.
	 *
	 * @param e the CoreException
	 */
	public static TeamException wrapException(IOException e) {
		return new TeamException(new Status(IStatus.ERROR, PLUGIN_ID,
			TeamException.IO_FAILED, e.getMessage(), e));
	}

	/**
	 * Helper method to log an exception status.
	 *
	 * @param status the status to be logged
	 */
	public static void log(IStatus status) {
		plugin.getLog().log(status);
	}

	/**
	 * Returns the standard display to be used. The method first checks, if
	 * the thread calling this method has an associated display. If so, this
	 * display is returned. Otherwise the method returns the default display.
	 */
	public static Display getStandardDisplay() {
		Display display= Display.getCurrent();
		if (display == null) {
			display= Display.getDefault();
		}
		return display;
	}

}
