package org.ntlab.traceanalyzer;

import java.util.Hashtable;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;

public class Activator extends AbstractUIPlugin implements BundleActivator, ServiceListener {

	public static final String PLUGIN_ID = "org.ntlab.traceAnalyzer"; //$NON-NLS-1$
	
	private DictionaryService service;
	private ServiceTracker dictionaryServiceTracker;
	private BundleContext fContext;

	private static Activator plugin;
	
	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		fContext = context;
		service = new DictionaryServiceImpl();

		Hashtable props = new Hashtable();
		// register the service
		context.registerService(DictionaryService.class.getName(), service, props);

		// create a tracker and track the service
		dictionaryServiceTracker = new ServiceTracker(context, DictionaryService.class.getName(), null);
		dictionaryServiceTracker.open();

		// have a service listener to implement the whiteboard pattern
	    fContext.addServiceListener(this, "(objectclass=" + Dictionary.class.getName() + ")");
		
		// grab the service
		service = (DictionaryService) dictionaryServiceTracker.getService();
		
		// register the dictionary
		service.registerDictionary(new DictionaryImpl());
		
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		// close the service tracker
		dictionaryServiceTracker.close();
		dictionaryServiceTracker = null;

		service = null;
		fContext = null;
		plugin = null;
	}

	public void serviceChanged(ServiceEvent ev) {
		ServiceReference sr = ev.getServiceReference();
		switch(ev.getType()) {
			case ServiceEvent.REGISTERED:
			{
				Dictionary dictionary = (Dictionary) fContext.getService(sr);
				service.registerDictionary(dictionary);
			}
			break;
			case ServiceEvent.UNREGISTERING:
			{
				Dictionary dictionary = (Dictionary) fContext.getService(sr);
				service.unregisterDictionary(dictionary);
			}
			break;
		}
	}

	public static Activator getDefault() {
		return plugin;
	}
	
	@Override
	protected void initializeImageRegistry(ImageRegistry reg) {
		reg.put("TraceAnalyzer.STACK_FRAME_IMAGE", getImageDescriptor("/icons/full/stckframe_obj.gif"));
		reg.put("TraceAnalyzer.STACK_FRAME_IMAGE2", getImageDescriptor("/icons/full/stckframe_obj2.gif"));
		reg.put("TraceAnalyzer.STACK_FRAME_IMAGE3", getImageDescriptor("/icons/full/stckframe_obj3.gif"));
		reg.put("TraceAnalyzer.THREAD_IMAGE", getImageDescriptor("/icons/full/threads_obj.gif"));
		reg.put("TraceAnalyzer.THREAD_IMAGE2", getImageDescriptor("/icons/full/threads_obj2.gif"));
		reg.put("TraceAnalyzer.HIGHLIGHT_UNIQUE_IMAGE", getImageDescriptor("/icons/full/read_obj.gif"));
	}
	
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}
}
