diff --git a/core/plugins/org.polarsys.capella.core.sirius.analysis/src/org/polarsys/capella/core/sirius/analysis/DiagramServices.java b/core/plugins/org.polarsys.capella.core.sirius.analysis/src/org/polarsys/capella/core/sirius/analysis/DiagramServices.java index 4d5a4adcb5..503d57c0dc 100644 --- a/core/plugins/org.polarsys.capella.core.sirius.analysis/src/org/polarsys/capella/core/sirius/analysis/DiagramServices.java +++ b/core/plugins/org.polarsys.capella.core.sirius.analysis/src/org/polarsys/capella/core/sirius/analysis/DiagramServices.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2023 THALES GLOBAL SERVICES. + * Copyright (c) 2006, 2024 THALES GLOBAL SERVICES and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -9,6 +9,7 @@ * * Contributors: * Thales - initial API and implementation + * Glenn Plouhinec, Maxime Porhel (Obeo) - Avoid potential deadlocks in refresh *******************************************************************************/ package org.polarsys.capella.core.sirius.analysis; @@ -44,7 +45,6 @@ import org.eclipse.sirius.business.api.session.SessionManager; import org.eclipse.sirius.common.tools.api.interpreter.IInterpreter; import org.eclipse.sirius.common.tools.api.util.RefreshIdsHolder; -import org.eclipse.sirius.common.ui.tools.api.util.EclipseUIUtil; import org.eclipse.sirius.diagram.AbstractDNode; import org.eclipse.sirius.diagram.DDiagram; import org.eclipse.sirius.diagram.DDiagramElement; @@ -93,6 +93,8 @@ import org.eclipse.sirius.ecore.extender.business.api.accessor.ModelAccessor; import org.eclipse.sirius.tools.api.SiriusPlugin; import org.eclipse.sirius.ui.business.api.dialect.DialectEditor; +import org.eclipse.sirius.ui.business.api.session.IEditingSession; +import org.eclipse.sirius.ui.business.api.session.SessionUIManager; import org.eclipse.sirius.viewpoint.DRepresentation; import org.eclipse.sirius.viewpoint.DSemanticDecorator; import org.eclipse.sirius.viewpoint.ViewpointPackage; @@ -2100,16 +2102,22 @@ public void refreshRepresentationOfEditor(IEditorPart editor) { * @return */ public EditPart getEditPart(DDiagramElement diagramElement) { - IEditorPart editor = EclipseUIUtil.getActiveEditor(); - if (editor instanceof DiagramEditor) { - Session session = new EObjectQuery(diagramElement).getSession(); - View gmfView = SiriusGMFHelper.getGmfView(diagramElement, session); - - if (gmfView != null && editor instanceof DiagramEditor) { - final Map editPartRegistry = ((DiagramEditor) editor).getDiagramGraphicalViewer().getEditPartRegistry(); - final Object editPart = editPartRegistry.get(gmfView); - if (editPart instanceof EditPart) { - return (EditPart) editPart; + DDiagram parentDiagram = diagramElement == null ? null : diagramElement.getParentDiagram(); + Session session = parentDiagram == null ? null : new EObjectQuery(parentDiagram).getSession(); + IEditingSession uiSession = SessionUIManager.INSTANCE.getUISession(session); + + if (uiSession != null && parentDiagram != null) { + DialectEditor editor = uiSession.getEditor(parentDiagram); + + if (editor instanceof DiagramEditor diagramEditor) { + View gmfView = SiriusGMFHelper.getGmfView(diagramElement, session); + + if (gmfView != null && diagramEditor.getDiagramGraphicalViewer() != null) { + final Map editPartRegistry = diagramEditor.getDiagramGraphicalViewer().getEditPartRegistry(); + final Object editPart = editPartRegistry.get(gmfView); + if (editPart instanceof EditPart) { + return (EditPart) editPart; + } } } } diff --git a/tests/plugins/org.polarsys.capella.test.diagram.common.ju/src/org/polarsys/capella/test/diagram/common/ju/wrapper/utils/DiagramHelper.java b/tests/plugins/org.polarsys.capella.test.diagram.common.ju/src/org/polarsys/capella/test/diagram/common/ju/wrapper/utils/DiagramHelper.java index 3a2e6f9160..27e84f2c0a 100644 --- a/tests/plugins/org.polarsys.capella.test.diagram.common.ju/src/org/polarsys/capella/test/diagram/common/ju/wrapper/utils/DiagramHelper.java +++ b/tests/plugins/org.polarsys.capella.test.diagram.common.ju/src/org/polarsys/capella/test/diagram/common/ju/wrapper/utils/DiagramHelper.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2020 THALES GLOBAL SERVICES. + * Copyright (c) 2006, 2024 THALES GLOBAL SERVICES and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -9,6 +9,7 @@ * * Contributors: * Thales - initial API and implementation + * Glenn Plouhinec, Maxime Porhel (Obeo) - Avoid potential deadlocks in refresh *******************************************************************************/ package org.polarsys.capella.test.diagram.common.ju.wrapper.utils; @@ -671,11 +672,12 @@ public static void pasteLayout(DDiagram diagram) { } /** - * Close a 'diagram' + * Close a diagram editor from the ui session. * * @param session + * the Sirius session * @param diagram - * @return + * the representation associated to the editor to close */ public static void closeEditor(Session session, DDiagram diagram) { IEditorPart editor = getDiagramEditor(session, diagram); @@ -684,6 +686,16 @@ public static void closeEditor(Session session, DDiagram diagram) { } } + /** + * Close a diagram editor. + * + * @param editor + * the editor to close + */ + public static void closeEditor(IEditorPart editor) { + DialectUIManager.INSTANCE.closeEditor(editor, true); + } + /** * Open a 'diagram' e.g. a {@link DRepresentation}. * diff --git a/tests/plugins/org.polarsys.capella.test.diagram.misc.ju/src/org/polarsys/capella/test/diagram/misc/ju/testcases/PieIconTest.java b/tests/plugins/org.polarsys.capella.test.diagram.misc.ju/src/org/polarsys/capella/test/diagram/misc/ju/testcases/PieIconTest.java index cac75740f6..44cdba6175 100644 --- a/tests/plugins/org.polarsys.capella.test.diagram.misc.ju/src/org/polarsys/capella/test/diagram/misc/ju/testcases/PieIconTest.java +++ b/tests/plugins/org.polarsys.capella.test.diagram.misc.ju/src/org/polarsys/capella/test/diagram/misc/ju/testcases/PieIconTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2024 THALES GLOBAL SERVICES. + * Copyright (c) 2024 THALES GLOBAL SERVICES and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -9,6 +9,7 @@ * * Contributors: * Thales - initial API and implementation + * Glenn Plouhinec, Maxime Porhel (Obeo) - Avoid potential deadlocks in refresh *******************************************************************************/ package org.polarsys.capella.test.diagram.misc.ju.testcases; @@ -20,12 +21,12 @@ import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; import org.eclipse.sirius.business.api.session.Session; import org.eclipse.sirius.diagram.DDiagram; -import org.eclipse.sirius.diagram.DDiagramElement; import org.eclipse.sirius.diagram.DEdge; import org.eclipse.sirius.diagram.ui.internal.edit.parts.DEdgeBeginNameEditPart; import org.eclipse.sirius.diagram.ui.internal.edit.parts.DEdgeEndNameEditPart; import org.eclipse.sirius.diagram.ui.tools.api.graphical.edit.styles.StyleConfiguration; import org.eclipse.sirius.diagram.ui.tools.internal.graphical.edit.styles.StyleConfigurationRegistry; +import org.eclipse.ui.IEditorPart; import org.polarsys.capella.core.data.cs.PhysicalLink; import org.polarsys.capella.core.data.fa.FunctionalExchange; import org.polarsys.capella.core.sirius.analysis.CapellaServices; @@ -44,15 +45,23 @@ public class PieIconTest extends BasicTestCase { private static final String PROJECT_NAME = "testPie"; //$NON-NLS-1$ protected Session session; + protected SessionContext context; public static String OAIB = "_uMIigLCvEe6Fv9U2y24pjw"; + public static String OAB = "_97h-ILCwEe6Fv9U2y24pjw"; + public static String SAB = "_CZ_7QLC2Ee6Fv9U2y24pjw"; + public static String SDFB = "_MWE0wLC0Ee6Fv9U2y24pjw"; + public static String LAB = "_1jTKwK-mEe6L-OKa_6GDIw"; + public static String LDFB = "_c61CELEYEe6Fv9U2y24pjw"; + public static String PAB = "_S542sLEaEe6Fv9U2y24pjw"; + public static String PDFB = "_0DlQ4LEZEe6Fv9U2y24pjw"; @Override @@ -65,7 +74,6 @@ protected void setUp() throws Exception { super.setUp(); this.session = getSession(PROJECT_NAME); this.context = new SessionContext(session); - } @Override @@ -80,22 +88,22 @@ public void testOA() { testPieIconsOnFunctionalExchanges(session, OAB, false); testPieIconsOnFunctionalExchanges(session, OAIB, false); } - + public void testSA() { testPieIconsOnFunctionalExchanges(session, SAB, true); testPieIconsOnFunctionalExchanges(session, SDFB, false); - } - + } + public void testLA() { testPieIconsOnFunctionalExchanges(session, LAB, true); testPieIconsOnFunctionalExchanges(session, LDFB, false); - } - + } + public void testPA() { testPieIconsOnFunctionalExchanges(session, PAB, true); testPieIconsOnFunctionalExchanges(session, PDFB, false); - } - + } + public void testPieIconsOnFunctionalExchanges(Session session, String diagramUID, boolean testPL) { DEdgeIconCache.getInstance().reset(); @@ -103,7 +111,7 @@ public void testPieIconsOnFunctionalExchanges(Session session, String diagramUID DiagramHelper.setPreferenceAutoRefresh(false); DDiagram diagram = (DDiagram) DiagramHelper.getDRepresentationByUID(session, diagramUID); - DiagramHelper.opendiagramEditor(session, diagram); + IEditorPart editor = DiagramHelper.opendiagramEditor(session, diagram); assertFalse(hasPieIconOnFunctionalExchange(diagram)); if (testPL) assertFalse(hasPieIconOnPhysicalLink(diagram)); @@ -114,97 +122,85 @@ public void testPieIconsOnFunctionalExchanges(Session session, String diagramUID assertTrue(hasPieIconOnFunctionalExchange(diagram)); if (testPL) assertTrue(hasPieIconOnPhysicalLink(diagram)); - DiagramHelper.closeEditor(session, diagram); + DiagramHelper.closeEditor(editor); DiagramHelper.setPrefereneRefreshOnOpening(true); DiagramHelper.setPreferenceAutoRefresh(true); // Icon shall not be true on opening with refresh diagram = (DDiagram) DiagramHelper.getDRepresentationByUID(session, diagramUID); - DiagramHelper.opendiagramEditor(session, diagram); + editor = DiagramHelper.opendiagramEditor(session, diagram); assertTrue(hasPieIconOnFunctionalExchange(diagram)); if (testPL) assertTrue(hasPieIconOnPhysicalLink(diagram)); - DiagramHelper.closeEditor(session, diagram); + DiagramHelper.closeEditor(editor); } - - public void testPieIconsOnPhysicalLinks(Session session, String diagramUID){ + + public void testPieIconsOnPhysicalLinks(Session session, String diagramUID) { DEdgeIconCache.getInstance().reset(); DiagramHelper.setPrefereneRefreshOnOpening(false); DiagramHelper.setPreferenceAutoRefresh(false); DDiagram diagram = (DDiagram) DiagramHelper.getDRepresentationByUID(session, diagramUID); - DiagramHelper.opendiagramEditor(session, diagram); + IEditorPart editor = DiagramHelper.opendiagramEditor(session, diagram); assertFalse(hasPieIconOnPhysicalLink(diagram)); // Icon shall be null as semanticElements are still FunctionalExchange, and not FunctionalChain DiagramHelper.refreshDiagram(diagram); // Icon shall not be null after refresh assertTrue(hasPieIconOnPhysicalLink(diagram)); - DiagramHelper.closeEditor(session, diagram); + DiagramHelper.closeEditor(editor); DiagramHelper.setPrefereneRefreshOnOpening(true); DiagramHelper.setPreferenceAutoRefresh(true); // Icon shall not be true on opening with refresh diagram = (DDiagram) DiagramHelper.getDRepresentationByUID(session, diagramUID); - DiagramHelper.opendiagramEditor(session, diagram); + editor = DiagramHelper.opendiagramEditor(session, diagram); assertTrue(hasPieIconOnPhysicalLink(diagram)); - DiagramHelper.closeEditor(session, diagram); + DiagramHelper.closeEditor(editor); } private boolean hasPieIconOnFunctionalExchange(DDiagram diagram) { - for (DDiagramElement element : diagram.getDiagramElements()) { - DEdge edge = (DEdge) element; - EObject target = edge.getTarget(); - if (target instanceof FunctionalExchange) { - FunctionalExchange fe = (FunctionalExchange) target; - EditPart edgeEditPart = DiagramServices.getDiagramServices().getEditPart(edge); - if (CapellaServices.getService().getDisplayedInvolvingFunctionalChains(edge).size() > 1) { - if (edgeEditPart != null) { - EditPart nameEditPart = edgeEditPart.getChildren().stream() - .filter(child -> child instanceof DEdgeBeginNameEditPart || child instanceof DEdgeEndNameEditPart) - .findAny().get(); - if (nameEditPart != null) { - StyleConfiguration c = StyleConfigurationRegistry.getInstance() - .getStyleConfiguration(edge.getDiagramElementMapping(), edge.getStyle()); - - return c.getLabelIcon(element, (IGraphicalEditPart) nameEditPart) != null; - } - - return nameEditPart != null; + for (DEdge edge : diagram.getEdges()) { + EObject target = edge.getTarget(); + if (target instanceof FunctionalExchange) { + FunctionalExchange fe = (FunctionalExchange) target; + EditPart edgeEditPart = DiagramServices.getDiagramServices().getEditPart(edge); + if (CapellaServices.getService().getDisplayedInvolvingFunctionalChains(edge).size() > 1) { + if (edgeEditPart != null) { + EditPart nameEditPart = edgeEditPart.getChildren().stream().filter(child -> child instanceof DEdgeBeginNameEditPart || child instanceof DEdgeEndNameEditPart).findAny().get(); + if (nameEditPart != null) { + StyleConfiguration c = StyleConfigurationRegistry.getInstance().getStyleConfiguration(edge.getDiagramElementMapping(), edge.getStyle()); + return c.getLabelIcon(edge, (IGraphicalEditPart) nameEditPart) != null; } + return nameEditPart != null; + } } } } return false; } - + private boolean hasPieIconOnPhysicalLink(DDiagram diagram) { - for (DDiagramElement element : diagram.getDiagramElements()) { - DEdge edge = (DEdge) element; - EObject target = edge.getTarget(); - if (target instanceof PhysicalLink) { - PhysicalLink fe = (PhysicalLink) target; - EditPart edgeEditPart = DiagramServices.getDiagramServices().getEditPart(edge); - if (CapellaServices.getService().getDisplayedInvolvingPhysicalPaths(edge).size() > 1) { - if (edgeEditPart != null) { - EditPart nameEditPart = edgeEditPart.getChildren().stream() - .filter(child -> child instanceof DEdgeBeginNameEditPart || child instanceof DEdgeEndNameEditPart) - .findAny().get(); - if (nameEditPart != null) { - StyleConfiguration c = StyleConfigurationRegistry.getInstance() - .getStyleConfiguration(edge.getDiagramElementMapping(), edge.getStyle()); - - return c.getLabelIcon(element, (IGraphicalEditPart) nameEditPart) != null; - } - - return nameEditPart != null; + for (DEdge edge : diagram.getEdges()) { + EObject target = edge.getTarget(); + if (target instanceof PhysicalLink) { + PhysicalLink fe = (PhysicalLink) target; + EditPart edgeEditPart = DiagramServices.getDiagramServices().getEditPart(edge); + if (CapellaServices.getService().getDisplayedInvolvingPhysicalPaths(edge).size() > 1) { + if (edgeEditPart != null) { + EditPart nameEditPart = edgeEditPart.getChildren().stream().filter(child -> child instanceof DEdgeBeginNameEditPart || child instanceof DEdgeEndNameEditPart).findAny().get(); + if (nameEditPart != null) { + StyleConfiguration c = StyleConfigurationRegistry.getInstance().getStyleConfiguration(edge.getDiagramElementMapping(), edge.getStyle()); + return c.getLabelIcon(edge, (IGraphicalEditPart) nameEditPart) != null; } + return nameEditPart != null; + } } } } return false; } -} \ No newline at end of file +}