Bug 583164 - [Sirius][All Diagrams] Closing one Sirius diagram affects the functionality of all open Sirius diagrams

Signed-off-by: Dilan EESHVARAN <dilan.eeshvaran@cea.fr>
Change-Id: If1b780d14313b96733b2e8f53fa01be8c0b562bd
diff --git a/plugins/editor/org.eclipse.papyrus.sirius.editor/src/org/eclipse/papyrus/sirius/editor/internal/editor/NestedSiriusDiagramViewEditor.java b/plugins/editor/org.eclipse.papyrus.sirius.editor/src/org/eclipse/papyrus/sirius/editor/internal/editor/NestedSiriusDiagramViewEditor.java
index 9691fe2..3de0596 100755
--- a/plugins/editor/org.eclipse.papyrus.sirius.editor/src/org/eclipse/papyrus/sirius/editor/internal/editor/NestedSiriusDiagramViewEditor.java
+++ b/plugins/editor/org.eclipse.papyrus.sirius.editor/src/org/eclipse/papyrus/sirius/editor/internal/editor/NestedSiriusDiagramViewEditor.java
@@ -1,5 +1,5 @@
 /******************************************************************************
- * Copyright (c) 2021-2023 CEA LIST, Artal Technologies, Obeo and others.
+ * Copyright (c) 2021-2024 CEA LIST, Artal Technologies, Obeo and others.
  *
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -12,6 +12,7 @@
  *  Aurelien Didier (ARTAL) - aurelien.didier51@gmail.com - Initial API and implementation
  *  Jessy MALLET (OBEO) <jessy.mallet@obeo.fr> - Bug 579782
  *  Vincent LORENZO (CEA-LIST) - vincent.lorenzo@cea.fr - Bug 581387
+ *  Dilan EESHVARAN (CEA-LIST) - dilan.eeshvaran@cea.fr - Bug 583164
  *****************************************************************************/
 package org.eclipse.papyrus.sirius.editor.internal.editor;
 
@@ -39,6 +40,7 @@
 import org.eclipse.gef.ui.actions.ZoomInAction;
 import org.eclipse.gef.ui.actions.ZoomOutAction;
 import org.eclipse.gmf.runtime.common.ui.util.IPartSelector;
+import org.eclipse.gmf.runtime.diagram.core.listener.DiagramEventBroker;
 import org.eclipse.gmf.runtime.diagram.ui.actions.ActionIds;
 import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
 import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeCompartmentEditPart;
@@ -54,6 +56,7 @@
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.papyrus.infra.core.sashwindows.di.service.IPageManager;
 import org.eclipse.papyrus.infra.core.services.ServiceException;
 import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
 import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
@@ -67,6 +70,7 @@
 import org.eclipse.sirius.business.api.session.Session;
 import org.eclipse.sirius.business.api.session.SessionManager;
 import org.eclipse.sirius.diagram.DSemanticDiagram;
+import org.eclipse.sirius.diagram.model.business.internal.spec.DSemanticDiagramSpec;
 import org.eclipse.sirius.diagram.ui.internal.edit.parts.DEdgeEditPart;
 import org.eclipse.sirius.diagram.ui.internal.edit.parts.DNode3EditPart;
 import org.eclipse.sirius.diagram.ui.internal.edit.parts.DNodeContainer2EditPart;
@@ -80,6 +84,7 @@
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IEditorReference;
 import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.PartInitException;
@@ -113,7 +118,7 @@
 	// TODO check if the code with keyhandler is useful or useless
 	private KeyHandler keyHandler;
 
-	/** Command stack listener used to propagate Dirty state*/
+	/** Command stack listener used to propagate Dirty state */
 	private CommandStackListener commandStackListener = new CommandStackListener() {
 
 		@Override
@@ -145,7 +150,7 @@
 		try {
 			saveAndDirtyService = servicesRegistry.getService(ISaveAndDirtyService.class);
 		} catch (ServiceException e) {
-			Activator.log.error("Unexpected Error",e); //$NON-NLS-1$
+			Activator.log.error("Unexpected Error", e); //$NON-NLS-1$
 		}
 		saveAndDirtyService.registerIsaveablePart(this);
 
@@ -159,7 +164,7 @@
 			Assert.isNotNull(this.servicesRegistry, "The papyrus ServicesRegistry is null. The Diagram Editor creation failed."); //$NON-NLS-1$
 			initializeEditingDomain();
 		} catch (ServiceException e) {
-			Activator.log.error("Unexpected Error",e); //$NON-NLS-1$
+			Activator.log.error("Unexpected Error", e); //$NON-NLS-1$
 		}
 	}
 
@@ -172,7 +177,7 @@
 		try {
 			return (ISiriusSessionService) this.servicesRegistry.getService(ISiriusSessionService.SERVICE_ID);
 		} catch (ServiceException e) {
-			Activator.log.error("Unexpected Error",e); //$NON-NLS-1$
+			Activator.log.error("Unexpected Error", e); //$NON-NLS-1$
 		}
 		return null;
 	}
@@ -316,7 +321,7 @@
 				try {
 					NestedSiriusDiagramViewEditor.super.init(site, diagramViewEditorInput);
 				} catch (PartInitException e) {
-					Activator.log.error("Unexpected Error",e); //$NON-NLS-1$
+					Activator.log.error("Unexpected Error", e); //$NON-NLS-1$
 				}
 			}
 		});
@@ -557,5 +562,38 @@
 		}
 		super.dispose();
 	}
+	
+	/**
+	 * @see org.eclipse.sirius.diagram.ui.tools.internal.editor.DDiagramEditorImpl#stopDiagramEventBrokerListener()
+	 * @see bug 583164
+	 * 
+	 */
+	protected void stopDiagramEventBrokerListener(TransactionalEditingDomain ted) {
+		// Try to remove the diagram event broker if it's no more needed to avoid memory leak
+		int nbGMFDiagramEditorsOfSameTED = 0;
+		if (getSite() != null && getSite().getPage() != null) {
+			for (IEditorReference editorRef : getSite().getPage().getEditorReferences()) {
+				IEditorPart editorPart = editorRef.getEditor(false);
+				if (editorPart instanceof IMultiDiagramEditor papyrusEditor) {
+					ServicesRegistry servReg = papyrusEditor.getServicesRegistry();
+					IPageManager pageM = null;
+					try {
+						pageM = servReg.getService(IPageManager.class);
+					} catch (ServiceException e) {
+						Activator.log.error("Unexpected Error", e); //$NON-NLS-1$
+					}
+					for (Object tmp : pageM.allPages()) {
+						if (tmp instanceof DSemanticDiagramSpec semD && pageM.isOpen(semD)) {
+							nbGMFDiagramEditorsOfSameTED++;
+						}
+					}
+				}
+			}
+		}
+		if (nbGMFDiagramEditorsOfSameTED == 0 && ted != null) {
+			// Remove the diagram event broker because there is no more opened diagram
+			DiagramEventBroker.stopListening(ted);
+		}
+	}
 
 }