Attempting to resolve view threading issues along with some refactoring.
diff --git a/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/HandlerManager.java b/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/HandlerManager.java
index c96134b..f1ed938 100644
--- a/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/HandlerManager.java
+++ b/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/HandlerManager.java
@@ -60,7 +60,7 @@
         handlers.add(handler);

         handlerActivations.add(handlerService.activateHandler(id, handler));

         ModelViewManager manager = AXFWorkbenchPlugin.getDefault().getManager();

-        manager.getWorkbenchListeners().addListener(handler);

+        manager.getActiveModelListeners().addListener(handler);

         manager.getManagerListeners().addModelManagerListener(handler);

     }

 

diff --git a/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/LifeCycleListeners.java b/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/LifeCycleListeners.java
index 0e7ed06..b1e02ec 100644
--- a/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/LifeCycleListeners.java
+++ b/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/LifeCycleListeners.java
@@ -31,7 +31,7 @@
 

     List<ILifeCycleListener> workbenchListeners = new ArrayList<ILifeCycleListener>();

 

-    public synchronized void replaceModel(IModel oldModel, IModel newModel) {

+    public void replaceModel(IModel oldModel, IModel newModel) {

         if (oldModel != null) {

             for (final ILifeCycleListener listener : workbenchListeners) {

                 oldModel.removeModelListener(listener);

diff --git a/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/ModelViewManager.java b/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/ModelViewManager.java
index 728e6a3..4db87c8 100644
--- a/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/ModelViewManager.java
+++ b/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/ModelViewManager.java
@@ -60,6 +60,47 @@
 @SuppressWarnings("restriction")
 public class ModelViewManager implements IAdapterFactory {
 
+    /**
+     * 
+     * @author mparker
+     * 
+     */
+    private final class ActivationListener implements IPartListener {
+        private void assignPart(final IWorkbenchPart part) {
+            if (part instanceof IModelPart && ((IModelPart) part).getAdapter(IModel.class) != null) {
+                new Thread() {
+                    public void run() {
+                        IModel model = (IModel) ((IModelPart) part).getAdapter(IModel.class);
+                        setActiveModel(model);
+                    }
+                }.start();
+            }
+        }
+
+        public void partBroughtToTop(IWorkbenchPart part) {
+            assignPart(part);
+        }
+
+        public void partActivated(final IWorkbenchPart part) {
+            assignPart(part);
+        }
+
+        public void partClosed(final IWorkbenchPart part) {
+            new Thread() {
+                public void run() {
+                    removed((IViewPart) part);
+                }
+            }.start();
+        }
+
+        public void partDeactivated(IWorkbenchPart part) {
+        }
+
+        public void partOpened(IWorkbenchPart part) {
+            assignPart(part);
+        }
+    }
+
     public static final String EXECUTION_PERSPECTIVE_ID = "org.eclipse.amp.axf.ExecutionPerspective";
 
     private IContextService contextService;
@@ -72,15 +113,15 @@
 
     List<IViewPart> views = new ArrayList<IViewPart>();
 
-    Map<IEngine, IModel> runnerForModel = new HashMap<IEngine, IModel>();
+    Map<IEngine, IModel> runnerForModel;
 
     Map<IModel, IEngine> modelForRunner = new HashMap<IModel, IEngine>();
 
     Map<Object, IModel> modelForArbitrary = new HashMap<Object, IModel>();
 
-    Map<IModel, List<IViewPart>> viewsForModel = new HashMap<IModel, List<IViewPart>>();
+    Map<IModel, List<IViewPart>> viewsForModel;
 
-    LifeCycleListeners workbenchListeners = new LifeCycleListeners();
+    LifeCycleListeners activeModelListeners;
 
     ModelManagerListeners managerListeners = new ModelManagerListeners();
 
@@ -98,6 +139,8 @@
 
     private String priorPerspectiveID;
 
+    private StatusLineView statusLineView;
+
     /**
      * Instantiates a new model view manager.
      */
@@ -126,7 +169,7 @@
         final IModel oldModel = this.activeModel;
         this.activeModel = newModel;
         if (newModel != oldModel) {
-            workbenchListeners.replaceModel(oldModel, newModel);
+            getActiveModelListeners().replaceModel(oldModel, newModel);
             if (newModel != null) {
                 managerListeners.notifyModelActivated(newModel);
             }
@@ -391,7 +434,7 @@
         } else {
             StatusManager.getManager().handle(
                                               new Status(Status.WARNING, "org.eclipse.amp.axf.ui",
-                                                         "Removing a model that no longer exists:" + model));
+                                                         "Removing a model that no longer exists:" + model, new Exception()));
         }
     }
 
@@ -401,106 +444,55 @@
         return wb.getWorkbenchWindows()[0].getPages()[0];
     }
 
-    private void activate() {
+    private synchronized void activate() {
         final IWorkbench wb = AXFWorkbenchPlugin.getDefault().getWorkbench();
 
-        if (wb != null) {
-            wb.addWorkbenchListener(new IWorkbenchListener() {
-                public void postShutdown(IWorkbench workbench) {
-                }
+        wb.addWorkbenchListener(new IWorkbenchListener() {
+            public void postShutdown(IWorkbench workbench) {
+            }
 
-                public boolean preShutdown(IWorkbench workbench, boolean forced) {
-                    if (!forced) {
-                        synchronized (ModelViewManager.this) {
-                            for (IModel model : new ArrayList<IModel>(getModels())) {
-                                remove(model);
-                            }
+            public boolean preShutdown(IWorkbench workbench, boolean forced) {
+                if (!forced) {
+                    synchronized (ModelViewManager.this) {
+                        for (IModel model : new ArrayList<IModel>(getModels())) {
+                            remove(model);
                         }
                     }
-                    return true;
                 }
-            });
+                return true;
+            }
+        });
 
-            modelActivationListener = new IPartListener() {
-                public void partActivated(final IWorkbenchPart part) {
-                    assignPart(part);
-                    // if (updated != null) {
-                    // }
+        viewsForModel = new HashMap<IModel, List<IViewPart>>();
+        runnerForModel = new HashMap<IEngine, IModel>();
+        modelActivationListener = new ActivationListener();
+        activeModelListeners = new LifeCycleListeners();
+
+        getPage().addPartListener(modelActivationListener);
+        statusLineView = StatusLineView.getDefault();
+        getPage().addPartListener(statusLineView);
+        getActiveModelListeners().addListener(statusLineView);
+        activatePerspective(wb, EXECUTION_PERSPECTIVE_ID, false);
+        wb.getDisplay().syncExec(new Runnable() {
+            public void run() {
+                try {
+                    contextService = (IContextService) wb.getService(IContextService.class);
+                    ideContext = contextService.activateContext("org.eclipse.amp.axf.executionContext");
+                    partContext = contextService.activateContext("org.eclipse.amp.axf.activeEditorContext");
+
+                } catch (Exception e) {
+                    throw new RuntimeException("Couldn't activate services.", e);
                 }
-
-                private void assignPart(final IWorkbenchPart part) {
-                    if (part instanceof IModelPart && ((IModelPart) part).getAdapter(IModel.class) != null) {
-                        new Thread() {
-                            public void run() {
-                                IModel model = (IModel) ((IModelPart) part).getAdapter(IModel.class);
-                                setActiveModel(model);
-                            }
-                        }.start();
-                    }
-                }
-
-                public void partBroughtToTop(IWorkbenchPart part) {
-                    assignPart(part);
-                }
-
-                public void partClosed(final IWorkbenchPart part) {
-                    new Thread() {
-                        public void run() {
-                            removed((IViewPart) part);
-                        }
-                    }.start();
-                }
-
-                public void partDeactivated(IWorkbenchPart part) {
-                }
-
-                public void partOpened(IWorkbenchPart part) {
-                    assignPart(part);
-                }
-
-                // private SpeedSliderWindowControl getSpeedControl(final IWorkbenchPart part) {
-                // IViewSite site = (IViewSite) part.getSite();
-                // IActionBars actionBars = site.getActionBars();
-                // return (SpeedSliderWindowControl) actionBars.getMenuManager()
-                // .find("org.eclipse.amp.axf.view.ui.slider");
-                // };
-            };
-            getPage().addPartListener(modelActivationListener);
-            activatePerspective(wb, EXECUTION_PERSPECTIVE_ID, false);
-            wb.getDisplay().syncExec(new Runnable() {
-                private StatusLineView statusView;
-
-                public void run() {
-                    try {
-                        contextService = (IContextService) wb.getService(IContextService.class);
-                        ideContext = contextService.activateContext("org.eclipse.amp.axf.executionContext");
-                        partContext = contextService.activateContext("org.eclipse.amp.axf.activeEditorContext");
-
-                        handlers.activate();
-                        handlers.addHandler("org.eclipse.amp.axf.ui.start", new StartHandler());
-                        handlers.addHandler("org.eclipse.amp.axf.ui.stop", new StopHandler());
-                        handlers.addHandler("org.eclipse.amp.axf.ui.pause", new PauseHandler());
-                        handlers.addHandler("org.eclipse.amp.axf.ui.resume", new ResumeHandler());
-                        handlers.addHandler("org.eclipse.amp.axf.ui.restart", new RestartHandler());
-                        handlers.addHandler("org.eclipse.amp.axf.ui.close", new CloseHandler());
-                        handlers.addHandler("org.eclipse.amp.axf.ui.step", new StepHandler());
-                        // if (statusView == null) {
-                        // statusView = new StatusLineView();
-                        // new Thread() {
-                        // public void run() {
-                        // workbenchListeners.addListener(statusView);
-                        // addModelManagerListener(statusView);
-                        // };
-                        // }.start();
-                        // }
-                    } catch (Exception e) {
-                        throw new RuntimeException("Couldn't activate services.", e);
-                    }
-                }
-            });
-        } else {
-            throw new RuntimeException("Attempted to activate a service with no workbench available.");
-        }
+            }
+        });
+        handlers.activate();
+        handlers.addHandler("org.eclipse.amp.axf.ui.start", new StartHandler());
+        handlers.addHandler("org.eclipse.amp.axf.ui.stop", new StopHandler());
+        handlers.addHandler("org.eclipse.amp.axf.ui.pause", new PauseHandler());
+        handlers.addHandler("org.eclipse.amp.axf.ui.resume", new ResumeHandler());
+        handlers.addHandler("org.eclipse.amp.axf.ui.restart", new RestartHandler());
+        handlers.addHandler("org.eclipse.amp.axf.ui.close", new CloseHandler());
+        handlers.addHandler("org.eclipse.amp.axf.ui.step", new StepHandler());
     }
 
     private void activatePerspective(final IWorkbench wb, final String perspectiveID, final boolean editors) {
@@ -522,10 +514,9 @@
         });
     }
 
-    private void deactivate() {
+    private synchronized void deactivate() {
         final IWorkbench wb = AXFWorkbenchPlugin.getDefault().getWorkbench();
         if (wb != null) {
-            workbenchListeners = new LifeCycleListeners();
             wb.getDisplay().syncExec(new Runnable() {
                 public void run() {
                     if (contextService != null) {
@@ -536,29 +527,17 @@
                 }
             });
             getPage().removePartListener(modelActivationListener);
-            // while (views.size() > 0) {
-            // try {
-            // Thread.sleep(50);
-            // } catch (InterruptedException e) {
-            // }
-            // }
+            getPage().removePartListener(statusLineView);
             if (priorPerspectiveID != null) {
                 activatePerspective(wb, priorPerspectiveID, true);
             }
+            // We don't want references to these resources hanging around if not needed
             modelActivationListener = null;
+            statusLineView = null;
+            activeModelListeners = null;
         }
     }
 
-    private void waitForUpdate(int count) {
-        while (updateCount < count) {
-            try {
-                Thread.sleep(50);
-            } catch (InterruptedException e) {
-            }
-        }
-        updateCount = 0;
-    }
-
     /**
      * @param adaptableObject
      * @param adapterType
@@ -607,13 +586,12 @@
         this.editSelection = editSelection;
     }
 
-
     public ModelManagerListeners getManagerListeners() {
         return managerListeners;
     }
 
-    public LifeCycleListeners getWorkbenchListeners() {
-        return workbenchListeners;
+    public LifeCycleListeners getActiveModelListeners() {
+        return activeModelListeners;
     }
 
     public HandlerManager getHandlers() {
diff --git a/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/handlers/ModelRunHandler.java b/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/handlers/ModelRunHandler.java
index 2903dbd..81dad3e 100644
--- a/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/handlers/ModelRunHandler.java
+++ b/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/handlers/ModelRunHandler.java
@@ -100,11 +100,11 @@
         }

     }

 

-    public void observing(IObservationProvider model) {

+    public synchronized void observing(IObservationProvider model) {

         this.model = (IModel) model;

     }

 

-    public void observationEnd(IObservationProvider model) {

+    public synchronized void observationEnd(IObservationProvider model) {

         this.model = null;

     }

 

@@ -113,7 +113,7 @@
         notifyChange();

     }

 

-    public void modelActivated(IObservationProvider model) {

+    public synchronized void modelActivated(IObservationProvider model) {

         this.model = (IModel) model;

         notifyChange();

     }

diff --git a/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/view/StatusLineView.java b/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/view/StatusLineView.java
index 0550450..8fef063 100644
--- a/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/view/StatusLineView.java
+++ b/org.eclipse.amp.axf/plugins/org.eclipse.amp.axf.ide/src/org/eclipse/amp/axf/ide/view/StatusLineView.java
@@ -17,9 +17,9 @@
 

 import org.eclipse.amp.axf.core.IModel;

 import org.eclipse.amp.axf.core.IObservationProvider;

-import org.eclipse.amp.axf.ide.IModelWorkbenchListener;

 import org.eclipse.amp.axf.view.SWTAsyncModelListener;

 import org.eclipse.ui.IActionBars;

+import org.eclipse.ui.IPartListener;

 import org.eclipse.ui.IViewPart;

 import org.eclipse.ui.IViewSite;

 import org.eclipse.ui.IWorkbenchPart;

@@ -28,14 +28,17 @@
 /**

  * The Class StatusLineView.

  */

-public class StatusLineView extends SWTAsyncModelListener implements IModelWorkbenchListener {

+public class StatusLineView extends SWTAsyncModelListener implements IPartListener {

 

     IViewPart part;

 

+    // There should only be one status line view at a time..

+    private static StatusLineView view;

+

     /**

      * Instantiates a new status line view.

      */

-    public StatusLineView() {

+    private StatusLineView() {

         super(null, "Status Line View", 100);

     }

 

@@ -100,31 +103,22 @@
     }

 

     /**

-     * @param model

-     * @see org.eclipse.amp.axf.ide.IModelWorkbenchListener#modelActivated(org.eclipse.amp.axf.core.IObservationProvider)

+     * @param part

+     * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)

      */

-    public void modelActivated(IObservationProvider model) {

-    }

-

-    /**

-     * @param model

-     * @see org.eclipse.amp.axf.ide.IModelWorkbenchListener#modelAdded(org.eclipse.amp.axf.core.IObservationProvider)

-     */

-    public void modelAdded(IObservationProvider model) {

-    }

-

-    /**

-     * @param model

-     * @see org.eclipse.amp.axf.ide.IModelWorkbenchListener#modelRemoved(org.eclipse.amp.axf.core.IObservationProvider)

-     */

-    public void modelRemoved(IObservationProvider model) {

+    public void partActivated(IWorkbenchPart part) {

+        assignPart(part);

     }

 

     /**

      * @param part

-     * @see org.eclipse.amp.axf.ide.IModelWorkbenchListener#viewActivated(org.eclipse.ui.IWorkbenchPart)

+     * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)

      */

-    public void viewActivated(IWorkbenchPart part) {

+    public void partBroughtToTop(IWorkbenchPart part) {

+        assignPart(part);

+    }

+

+    private void assignPart(IWorkbenchPart part) {

         if (part instanceof IViewPart) {

             this.part = (IViewPart) part;

         } else {

@@ -134,18 +128,32 @@
 

     /**

      * @param part

-     * @see org.eclipse.amp.axf.ide.IModelWorkbenchListener#viewAdded(org.eclipse.ui.IWorkbenchPart)

+     * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)

      */

-    public void viewAdded(IWorkbenchPart part) {

+    public void partClosed(IWorkbenchPart part) {

+        if (part == this.part) {

+            this.part = null;

+        }

     }

 

     /**

      * @param part

-     * @see org.eclipse.amp.axf.ide.IModelWorkbenchListener#viewRemoved(org.eclipse.ui.IWorkbenchPart)

+     * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)

      */

-    public void viewRemoved(IWorkbenchPart part) {

-        if (part == this.part) {

-            this.part = null;

+    public void partDeactivated(IWorkbenchPart part) {

+    }

+

+    /**

+     * @param part

+     * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)

+     */

+    public void partOpened(IWorkbenchPart part) {

+    }

+

+    public static StatusLineView getDefault() {

+        if (view == null) {

+            view = new StatusLineView();

         }

+        return view;

     }

 }