[453037] Close the EditingSession before the Session is closed

This changes add a mechanism so that the EditingSession uses the
SessionListener.CLOSING notification to close its editors (while the
Session is still opened).

A RefreshJob which might have been triggered by a previous operation
will be waited for through a join() call so that it does not fail
because of the session being disposed.

Bug: 453037
Change-Id: I5cb536ee5c1b30f85c13ac0406afde8a52a0d393
Signed-off-by: Cedric Brun <cedric.brun@obeo.fr
Cherry-picked-from: 447963
diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/editor/DDiagramEditorImpl.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/editor/DDiagramEditorImpl.java
index 4fb7649..73a5f95 100644
--- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/editor/DDiagramEditorImpl.java
+++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/editor/DDiagramEditorImpl.java
@@ -315,6 +315,8 @@
 
     private Composite parentComposite;
 
+    private Job refreshJob;
+
     /**
      * Create a new instance.
      */
@@ -322,6 +324,18 @@
         super();
     }
 
+    @Override
+    public void close(boolean save) {
+        if (refreshJob != null) {
+            try {
+                refreshJob.join();
+            } catch (InterruptedException e) {
+                DiagramPlugin.getDefault().getLog().log(new Status(IStatus.INFO, DiagramPlugin.ID, "Refresh job got interrupted", e));
+            }
+        }
+        super.close(save);
+    }
+
     /**
      * We have to take care of the case when Eclipse starts up with a session.
      * and diagram already open. {@inheritDoc}
@@ -551,7 +565,7 @@
             // Run refresh in a job to avoid taking lock on the Workspace on
             // representation change, while the lock is already taken on odesign
             // change
-            Job refreshJob = new Job("Refresh ") {
+            refreshJob = new Job("Refresh ") {
 
                 @Override
                 protected IStatus run(IProgressMonitor monitor) {
diff --git a/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/business/internal/session/SessionUIManagerImpl.java b/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/business/internal/session/SessionUIManagerImpl.java
index b041afa..74c4651 100644
--- a/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/business/internal/session/SessionUIManagerImpl.java
+++ b/plugins/org.eclipse.sirius.ui/src/org/eclipse/sirius/ui/business/internal/session/SessionUIManagerImpl.java
@@ -133,6 +133,12 @@
         case SessionListener.OPENED:
             createAndOpenUiSession(updated);
             break;
+        case SessionListener.CLOSING:
+            IEditingSession uiSession = getUISession(updated);
+            if (uiSession != null) {
+                uiSession.close(false);
+            }
+            break;
         default:
             // do nothing as we will be notified in other way
             break;