Merge "[476811] Activity Explorer Session Listener correction"
diff --git a/plugins/org.eclipse.amalgam.explorer.activity.ui/src/org/eclipse/amalgam/explorer/activity/ui/internal/session/listeners/ActivityExplorerSessionListener.java b/plugins/org.eclipse.amalgam.explorer.activity.ui/src/org/eclipse/amalgam/explorer/activity/ui/internal/session/listeners/ActivityExplorerSessionListener.java
index 0253c76..45550cf 100644
--- a/plugins/org.eclipse.amalgam.explorer.activity.ui/src/org/eclipse/amalgam/explorer/activity/ui/internal/session/listeners/ActivityExplorerSessionListener.java
+++ b/plugins/org.eclipse.amalgam.explorer.activity.ui/src/org/eclipse/amalgam/explorer/activity/ui/internal/session/listeners/ActivityExplorerSessionListener.java
@@ -10,6 +10,8 @@
  *******************************************************************************/
 package org.eclipse.amalgam.explorer.activity.ui.internal.session.listeners;
 
+import java.lang.ref.WeakReference;
+
 import org.eclipse.amalgam.explorer.activity.ui.ActivityExplorerActivator;
 import org.eclipse.amalgam.explorer.activity.ui.api.editor.ActivityExplorerEditor;
 import org.eclipse.amalgam.explorer.activity.ui.api.editor.input.ActivityExplorerEditorInput;
@@ -18,6 +20,7 @@
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.sirius.business.api.session.Session;
 import org.eclipse.sirius.business.api.session.SessionListener;
+import org.eclipse.sirius.business.api.session.SessionManager;
 import org.eclipse.sirius.business.api.session.SessionManagerListener;
 import org.eclipse.sirius.viewpoint.description.Viewpoint;
 import org.eclipse.swt.widgets.Display;
@@ -29,163 +32,161 @@
  * 
  */
 public class ActivityExplorerSessionListener implements SessionManagerListener {
-	public Session session = null;
+  public WeakReference<Session> session = null;
 
-	@Override
-	public void notify(Session session_p, int notification_p) {
-		// Filter on event for other sessions.
-		session = session_p;
-		final ActivityExplorerEditor editor = ActivityExplorerManager.INSTANCE.getEditorFromSession(session_p);
+  @Override
+  public void notify(Session sessionp, int notification) {
+    // Filter on event for other sessions.
+    session = new WeakReference<Session>(sessionp);
+    final ActivityExplorerEditor editor = ActivityExplorerManager.INSTANCE.getEditorFromSession(sessionp);
 
-		Runnable runnable = null;
-		switch (notification_p) {
-		case SessionListener.CLOSING: /*
-									 * Closing event is used to have a chance to
-									 * persist the editor input at workbench
-									 * shutdown
-									 */
+    Runnable runnable = null;
+    switch (notification) {
+    case SessionListener.CLOSING: /*
+                                   * Closing event is used to have a chance to persist the editor input at workbench
+                                   * shutdown
+                                   */
 
-			runnable = new Runnable() {
-				/**
-				 * @see java.lang.Runnable#run()
-				 */
-				public void run() {
-					if (editor != null)
-						// Close this editor.
-						editor.close(false);
-				}
-			};
-			break;
-		case SessionListener.REPRESENTATION_CHANGE:
-			runnable = new Runnable() {
-				/**
-				 * @see java.lang.Runnable#run()
-				 */
-				@SuppressWarnings("synthetic-access")
-				public void run() {
-					// Handle fpages to mark them as dirty.
-					if (editor != null) {
-						IManagedForm headerForm = editor.getHeaderForm();
-						if (null != headerForm) {
-							headerForm.dirtyStateChanged();
-						}
-					}
-				}
-			};
-			break;
+      runnable = new Runnable() {
+        /**
+         * @see java.lang.Runnable#run()
+         */
+        public void run() {
+          if (editor != null)
+            // Close this editor.
+            editor.close(false);
+        }
+      };
+      break;
+    case SessionListener.REPRESENTATION_CHANGE:
+      runnable = new Runnable() {
+        /**
+         * @see java.lang.Runnable#run()
+         */
+        @SuppressWarnings("synthetic-access")
+        public void run() {
+          // Handle fpages to mark them as dirty.
+          if (editor != null) {
+            IManagedForm headerForm = editor.getHeaderForm();
+            if (null != headerForm) {
+              headerForm.dirtyStateChanged();
+            }
+          }
+        }
+      };
+      break;
 
-		case SessionListener.OPENED:
-			if (!(session.getSemanticResources().isEmpty())) {
-				runnable = new Runnable() {
-					@SuppressWarnings("synthetic-access")
-					public void run() {
-						try {
+    case SessionListener.OPENED:
+      if (!(session.get().getSemanticResources().isEmpty())) {
+        runnable = new Runnable() {
+          @SuppressWarnings("synthetic-access")
+          public void run() {
+            try {
 
-							final boolean open = ActivityExplorerActivator.getDefault().getPreferenceStore()
-									.getBoolean(PreferenceConstants.P_OPEN_ACTIVITY_EXPLORER);
+              final boolean open = ActivityExplorerActivator.getDefault().getPreferenceStore()
+                  .getBoolean(PreferenceConstants.P_OPEN_ACTIVITY_EXPLORER);
 
-							if (open) {
+              if (open) {
 
-								IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
-										.getActivePage();
-								if (activePage != null) {
-									activePage
-											.openEditor(
-													new ActivityExplorerEditorInput(
-															session,
-															org.eclipse.amalgam.explorer.activity.ui.api.editor.pages.helper.SessionHelper
-																	.getRootSemanticModel(session)), ActivityExplorerEditor.ID);
-								}
-							}
-						} catch (Exception exception_p) {
-							StringBuilder loggerMessage = new StringBuilder(".run(..) _ ActivityExplorer not Found."); //$NON-NLS-1$
-							loggerMessage.append(exception_p.getMessage());
-							ActivityExplorerActivator.getDefault().sentToLogger(loggerMessage.toString(), IStatus.ERROR);
-							// __logger.warn(new
-							// EmbeddedMessage(loggerMessage.toString(),
-							// IReportManagerDefaultComponents.UI),
-							// exception_p);
-						}
-					}
-				};
-			}
-			break;
-		case SessionListener.DIRTY:
-		case SessionListener.SYNC:
-		case SessionListener.SEMANTIC_CHANGE: // Listening to changes to mark
-												// the ActivityExplorerEditor editor dirty
-												// hence saveable.
-			runnable = new Runnable() {
-				/**
-				 * {@inheritDoc}
-				 */
-				public void run() {
-					if (editor != null) {
-						if (editor.getEditorInput() != null && !session.equals(editor.getEditorInput().getSession())) {
-							return;
-						}
-						IManagedForm headerForm = editor.getHeaderForm();
-						if (null != headerForm) {
-							headerForm.dirtyStateChanged();
-						}
-					}
-				}
-			};
-			break;
-		}
-		if (null != runnable) {
-			Display display = Display.getCurrent();
-			if (null == display) {
-				PlatformUI.getWorkbench().getDisplay().asyncExec(runnable);
-			} else {
-				runnable.run();
-			}
-		}
-	}
+                IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+                if (activePage != null) {
+                  activePage.openEditor(
+                      new ActivityExplorerEditorInput(session.get(),
+                          org.eclipse.amalgam.explorer.activity.ui.api.editor.pages.helper.SessionHelper
+                              .getRootSemanticModel(session.get())), ActivityExplorerEditor.ID);
+                }
+              }
+            } catch (Exception exception) {
+              StringBuilder loggerMessage = new StringBuilder(".run(..) _ ActivityExplorer not Found."); //$NON-NLS-1$
+              loggerMessage.append(exception.getMessage());
+              ActivityExplorerActivator.getDefault().sentToLogger(loggerMessage.toString(), IStatus.ERROR);
+              // __logger.warn(new
+              // EmbeddedMessage(loggerMessage.toString(),
+              // IReportManagerDefaultComponents.UI),
+              // exception);
+            }
+          }
+        };
+      }
+      break;
+    case SessionListener.DIRTY:
+    case SessionListener.SYNC:
+    case SessionListener.SEMANTIC_CHANGE: // Listening to changes to mark
+      // the ActivityExplorerEditor editor dirty
+      // hence saveable.
+      runnable = new Runnable() {
+        /**
+         * {@inheritDoc}
+         */
+        public void run() {
+          if (editor != null) {
+            if (editor.getEditorInput() != null && !session.get().equals(editor.getEditorInput().getSession())) {
+              return;
+            }
+            IManagedForm headerForm = editor.getHeaderForm();
+            if (null != headerForm) {
+              headerForm.dirtyStateChanged();
+            }
+          }
+        }
+      };
+      break;
+    }
+    if (null != runnable) {
+      Display display = Display.getCurrent();
+      if (null == display) {
+        PlatformUI.getWorkbench().getDisplay().asyncExec(runnable);
+      } else {
+        runnable.run();
+      }
+    }
+  }
 
-	public void notifyAddSession(Session newSession_p) {
-		// Do nothing.
-	}
+  public void notifyAddSession(Session newSession) {
+    // Do nothing.
+  }
 
-	public void notifyRemoveSession(Session removedSession_p) {
-		// Do nothing.
-	}
+  public void notifyRemoveSession(Session removedSession) {
+    // Do nothing.
+  }
 
-	public void notifyUpdatedSession(Session updatedSession_p) {
-		// Fake a representation change
-		notify(updatedSession_p, SessionListener.REPRESENTATION_CHANGE);
-	}
+  public void notifyUpdatedSession(Session updatedSession) {
+    // Fake a representation change
+    notify(updatedSession, SessionListener.REPRESENTATION_CHANGE);
+  }
 
   @Override
   public void viewpointSelected(Viewpoint selectedViewpoint) {
-      update(selectedViewpoint);
+    update(selectedViewpoint);
   }
 
   @Override
   public void viewpointDeselected(Viewpoint deselectedViewpoint) {
-      update(deselectedViewpoint);
+    update(deselectedViewpoint);
   }
 
-	/**
-	 * Update the ActivityExplorer Editor.
-	 * 
-	 * @param selectedViewpoint
-	 */
-	private void update(Viewpoint selectedViewpoint) {
-		if (selectedViewpoint != null && session != null && session.isOpen()) {
+  /**
+   * Update the ActivityExplorer Editor.
+   * 
+   * @param selectedViewpoint
+   */
+  private void update(Viewpoint selectedViewpoint) {
+    final Session currentSession = SessionManager.INSTANCE.getSession(selectedViewpoint);
 
-			Runnable refresh = new Runnable() {
+    if (selectedViewpoint != null && currentSession != null && currentSession.isOpen()) {
 
-				@Override
-				public void run() {
-					ActivityExplorerEditor editor = ActivityExplorerManager.INSTANCE.getEditorFromSession(session);
-					if (editor != null) {
-						editor.updateEditorPages(0);
-					}
-				}
-			};
-			Display.getDefault().syncExec(refresh);
-		}
-	}
+      Runnable refresh = new Runnable() {
+
+        @Override
+        public void run() {
+          ActivityExplorerEditor editor = ActivityExplorerManager.INSTANCE.getEditorFromSession(currentSession);
+          if (editor != null) {
+            editor.updateEditorPages(0);
+          }
+        }
+      };
+      Display.getDefault().syncExec(refresh);
+    }
+  }
 
 }