package am.ui;

import javax.swing.JFrame;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

import am.ui.api.AMVisualizationComponent;
import am.ui.osgi.VisualizationComponentTrackerCustomizer;

public class UIActivator implements BundleActivator {

	private static BundleContext context;

	private ServiceTracker<AMVisualizationComponent, AMVisualizationComponent> visTracker;
	
	static BundleContext getContext() {
		return context;
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext bundleContext) throws Exception {
		UIActivator.context = bundleContext;
		
		Thread ui_start = new Thread(new Runnable() {

			@Override
			public void run() {
				System.setProperty("apple.laf.useScreenMenuBar", "true");
				System.setProperty("apple.awt.brushMetalLook", "true");
				
				UI newUI = new UI();
				UICore.setUI(newUI);
				final JFrame uiFrame = newUI.getUIFrame();
				
				java.awt.EventQueue.invokeLater(new Runnable() {
				    @Override
				    public void run() {
				        uiFrame.toFront();
				        uiFrame.repaint();
				    }
				});
				
				// start the visualization tracker
				visTracker = new ServiceTracker<AMVisualizationComponent, AMVisualizationComponent>(context, 
						AMVisualizationComponent.class, new VisualizationComponentTrackerCustomizer(context));
				visTracker.open();
			}
			
		});
		ui_start.start();
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		visTracker.close();
		UIActivator.context = null;
	}

}

@@ -121,16 +121,63 @@
 ontext;%0A
+import org.osgi.framework.ServiceRegistration;%0A
 import o
@@ -502,16 +502,56 @@
 cker;%0A%09%0A
+%09private ServiceRegistration%3CUI%3E reg;%0A%09%0A
 %09static 
@@ -825,95 +825,8 @@
 %0A%09%09%0A
-%09%09Thread ui_start = new Thread(new Runnable() %7B%0A%0A%09%09%09@Override%0A%09%09%09public void run() %7B%0A%09%09
 %09%09Sy
@@ -883,18 +883,16 @@
 ue%22);%0A%09%09
-%09
-%09
 System.s
@@ -941,21 +941,17 @@
 ue%22);%0A%09%09
-%09%09%0A%09%09
+%0A
 %09%09UI new
@@ -965,18 +965,16 @@
 w UI();%0A
-%09%09
 %09%09UICore
@@ -990,16 +990,76 @@
 wUI);%0A%09%09
+%0A%09%09reg = context.registerService(UI.class, newUI, null);%0A%09%09%0A
 %09%09final 
@@ -1097,21 +1097,17 @@
 me();%0A%09%09
-%09
-%09%0A%09%09
+%0A
 %09%09java.a
@@ -1145,26 +1145,24 @@
 unnable() %7B%0A
-%09%09
 %09%09    @Overr
@@ -1167,18 +1167,16 @@
 rride%0A%09%09
-%09
-%09
     publ
@@ -1191,18 +1191,16 @@
 run() %7B%0A
-%09%09
 %09%09      
@@ -1218,26 +1218,24 @@
 oFront();%0A%09%09
-%09
-%09
         uiFr
@@ -1253,18 +1253,16 @@
 ();%0A
-%09%09
 %09%09    %7D%0A
 %09%09%09%09
@@ -1261,18 +1261,16 @@
   %7D%0A
-%09%09
 %09%09%7D);%0A%09%09
 %09%09%0A%09
@@ -1265,21 +1265,17 @@
 %09%09%7D);%0A%09%09
-%09
-%09%0A%09%09
+%0A
 %09%09// sta
@@ -1305,18 +1305,16 @@
 acker%0A%09%09
-%09
-%09
 visTrack
@@ -1403,18 +1403,16 @@
 t, %0A%09%09%09%09
-%09%09
 AMVisual
@@ -1492,18 +1492,16 @@
 xt));%0A%09%09
-%09
-%09
 visTrack
@@ -1515,43 +1515,8 @@
 ();%0A
-%09%09%09%7D%0A%09%09%09%0A%09%09%7D);%0A%09%09ui_start.start();%0A
 %09%7D%0A%0A
@@ -1697,26 +1697,81 @@
 %7B%0A%09%09
-visTracker.close()
+reg.unregister();%0A%09%09reg = null;%0A%09%09visTracker.close();%0A%09%09visTracker = null
 ;%0A%09%09

Make UI register as a service.