/**
 * Generated by DragonFly.
 *
 */
package minesweeper;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
import org.osgi.framework.ServiceRegistration;

import com.buglabs.application.IDesktopApp;
import com.buglabs.bug.module.lcd.pub.IModuleDisplay;

/**
 * BundleActivator for Minesweeper.
 *
 */
public class Activator implements BundleActivator, ServiceTrackerCustomizer{
	private ServiceRegistration reg;
	private ServiceTracker st;
	private BundleContext context;
	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {		
		//Create the service tracker and run it.
		this.context = context;
		st = new ServiceTracker(context, IModuleDisplay.class.getName(), this);
		st.open();
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		st.close();
	}
	
	public Object addingService(ServiceReference reference) {
		Object svc = context.getService(reference);
		
		//This sets up the actual application
		if (svc instanceof IModuleDisplay) {
			reg = context.registerService(
					IDesktopApp.class.getName(),
					new Minesweeper(context, (IModuleDisplay) svc), null);
		}
		
		return svc;
	}
	//as are these
	public void modifiedService(ServiceReference reference, Object service) {		
	}

	public void removedService(ServiceReference reference, Object service) {
		if (reg != null) {
			reg.unregister();
		}
	}
}