From 08782256760fc6511a23d3de2c6c368abda9cb7c Mon Sep 17 00:00:00 2001 From: Laurent Redor Date: Mon, 9 Sep 2024 12:07:00 +0200 Subject: [PATCH] [426] Cleanup of the GMFHelper class Bug: https://github.com/eclipse-sirius/sirius-desktop/issues/426 --- .../ui/internal/refresh/GMFHelper.java | 230 ++++++------------ 1 file changed, 79 insertions(+), 151 deletions(-) diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/internal/refresh/GMFHelper.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/internal/refresh/GMFHelper.java index d90d335d93..3bffd4e39d 100644 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/internal/refresh/GMFHelper.java +++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/internal/refresh/GMFHelper.java @@ -167,14 +167,12 @@ public static Point getAbsoluteLocation(Node node, boolean insetsAware, boolean ViewQuery viewQuery = new ViewQuery(node); if (adaptBorderNodeLocation && viewQuery.isBorderedNode() && layoutConstraint instanceof Bounds gmfBounds) { // manage location of bordered node with closest side - if (node.getElement() instanceof DNode && node.getElement().eContainer() instanceof AbstractDNode) { - DNode dNode = (DNode) node.getElement(); + if (node.getElement() instanceof DNode dNode && node.getElement().eContainer() instanceof AbstractDNode) { AbstractDNode parentAbstractDNode = (AbstractDNode) dNode.eContainer(); if (parentAbstractDNode.getOwnedBorderedNodes().contains(dNode)) { Node parentNode = (Node) node.eContainer(); LayoutConstraint parentLayoutConstraint = parentNode.getLayoutConstraint(); - if (parentLayoutConstraint instanceof Bounds) { - Bounds parentBounds = (Bounds) parentLayoutConstraint; + if (parentLayoutConstraint instanceof Bounds parentBounds) { int position = CanonicalDBorderItemLocator.findClosestSideOfParent(new Rectangle(gmfBounds.getX(), gmfBounds.getY(), gmfBounds.getWidth(), gmfBounds.getHeight()), new Rectangle(parentBounds.getX(), parentBounds.getY(), parentBounds.getWidth(), parentBounds.getHeight())); centerLocationIfZero(location, position, parentBounds, gmfBounds); @@ -281,11 +279,10 @@ public static Dimension getTopLeftInsets(Node container) { NodeQuery nodeQuery = new NodeQuery(container); if (nodeQuery.isContainer()) { EObject element = container.getElement(); - if (element instanceof DDiagramElementContainer) { - DDiagramElementContainer ddec = (DDiagramElementContainer) element; + if (element instanceof DDiagramElementContainer ddec) { // RegionContainer do not have containers insets - if (ddec instanceof DNodeContainer) { - if (new DNodeContainerExperimentalQuery((DNodeContainer) ddec).isRegionContainer()) { + if (ddec instanceof DNodeContainer dNodeContainer) { + if (new DNodeContainerExperimentalQuery(dNodeContainer).isRegionContainer()) { result.setHeight(HV_STACK_CONTAINER_INSETS.top); } else if (hasFullLabelBorder(ddec)) { result.setHeight(FREEFORM_CONTAINER_INSETS.top); @@ -335,8 +332,7 @@ public static Dimension getTopLeftInsets(Node container) { public static Dimension getContainerTopLeftInsets(Node node, boolean searchFirstParentContainer) { Dimension result = new Dimension(0, 0); EObject nodeContainer = node.eContainer(); - if (nodeContainer instanceof Node) { - Node parentNode = (Node) nodeContainer; + if (nodeContainer instanceof Node parentNode) { NodeQuery nodeQuery = new NodeQuery(parentNode); if (nodeQuery.isContainer() || nodeQuery.isListCompartment() || nodeQuery.isRegionContainerCompartment()) { result = getTopLeftInsets(parentNode); @@ -360,11 +356,10 @@ private static Dimension getBottomRightInsets(Node container) { NodeQuery nodeQuery = new NodeQuery(container); if (nodeQuery.isContainer()) { EObject element = container.getElement(); - if (element instanceof DDiagramElementContainer) { - DDiagramElementContainer ddec = (DDiagramElementContainer) element; + if (element instanceof DDiagramElementContainer ddec) { // RegionContainer do not have containers insets - if (ddec instanceof DNodeContainer) { - if (new DNodeContainerExperimentalQuery((DNodeContainer) ddec).isRegionContainer()) { + if (ddec instanceof DNodeContainer dNodeContainer) { + if (new DNodeContainerExperimentalQuery(dNodeContainer).isRegionContainer()) { result.setWidth(LIST_CONTAINER_INSETS.right); result.setHeight(LIST_CONTAINER_INSETS.bottom); // TODO: to verify @@ -421,16 +416,15 @@ private static Dimension getBottomRightInsets(Node container) { public static Dimension getContainerTopLeftInsetsAfterLabel(Node node, boolean searchFirstParentContainer) { Dimension result = new Dimension(0, 0); EObject nodeContainer = node.eContainer(); - if (nodeContainer instanceof Node) { - Node parentNode = (Node) nodeContainer; + if (nodeContainer instanceof Node parentNode) { NodeQuery nodeQuery = new NodeQuery(parentNode); if (nodeQuery.isContainer()) { EObject element = parentNode.getElement(); - if (element instanceof DDiagramElementContainer) { + if (element instanceof DDiagramElementContainer dDiagramElementContainer) { result.setWidth(FREEFORM_CONTAINER_INSETS.left); result.setHeight(FREEFORM_CONTAINER_INSETS.top); - Dimension borderSize = getBorderSize((DDiagramElementContainer) element); + Dimension borderSize = getBorderSize(dDiagramElementContainer); result.setWidth(result.width() + borderSize.width()); result.setHeight(result.height() + borderSize.height()); } @@ -482,11 +476,9 @@ private static void translateWithInsets(Point locationToTranslate, Node currentN // Border nodes are not concerned by those insets. if (!nodeQuery.isBorderedNode()) { locationToTranslate.translate(getContainerTopLeftInsets(currentNode, false)); - if (currentNode.eContainer() instanceof Node container) { - if (new ViewQuery(currentNode).isListItem() && container.getChildren().get(0) == currentNode) { - // This is the first list item, add a one margin border over it. - locationToTranslate.translate(0, 1); - } + if (currentNode.eContainer() instanceof Node container && new ViewQuery(currentNode).isListItem() && container.getChildren().get(0) == currentNode) { + // This is the first list item, add a one margin border over it. + locationToTranslate.translate(0, 1); } } } @@ -498,8 +490,8 @@ private static boolean hasFullLabelBorder(DDiagramElementContainer ddec) { private static boolean isFirstRegion(DDiagramElementContainer ddec) { EObject potentialRegionContainer = ddec.eContainer(); - if (potentialRegionContainer instanceof DNodeContainer) { - Iterable regions = Iterables.filter(((DNodeContainer) potentialRegionContainer).getOwnedDiagramElements(), DDiagramElementContainer.class); + if (potentialRegionContainer instanceof DNodeContainer dNodeContainer) { + Iterable regions = Iterables.filter((dNodeContainer).getOwnedDiagramElements(), DDiagramElementContainer.class); return !Iterables.isEmpty(regions) && ddec == Iterables.getFirst(regions, null); } return false; @@ -534,14 +526,12 @@ public static Point getLocation(Node node) { private static void centerLocationIfZero(Point location, int position, Bounds parentBounds, Bounds gmfBounds) { switch (position) { - case PositionConstants.NORTH: - case PositionConstants.SOUTH: + case PositionConstants.NORTH, PositionConstants.SOUTH: if (location.x == 0) { location.setX(location.x + (parentBounds.getWidth() - gmfBounds.getWidth()) / 2); } break; - case PositionConstants.WEST: - case PositionConstants.EAST: + case PositionConstants.WEST, PositionConstants.EAST: if (location.y == 0) { location.setY(location.y + (parentBounds.getHeight() - gmfBounds.getHeight()) / 2); } @@ -550,56 +540,6 @@ private static void centerLocationIfZero(Point location, int position, Bounds pa break; } } - // - // /** - // * Get the absolute bounds relative to the origin (Diagram). - // * - // * @param node - // * the GMF Node - // * @param adaptBorderNodeLocation - // * Useful for specific border nodes, like in sequence diagrams, to center the border nodes if the - // * coordinate is 0 (x for EAST or WEST side, y for NORTH or SOUTH side) - // * - // * @return the absolute bounds of the node relative to the origin (Diagram) - // */ - // public static Rectangle getAbsoluteBounds(Node node, boolean adaptBorderNodeLocation) { - // return getAbsoluteBounds(node, false, false, false, adaptBorderNodeLocation); - // } - // /** - // * Get the absolute bounds relative to the origin (Diagram). - // * - // * @param node - // * the GMF Node - // * @param insetsAware - // * true to consider the draw2D figures insets. Warning: Those insets are based on the - // * current Sirius editParts and could become wrong if a developer customizes them. - // * @param adaptBorderNodeLocation - // * Useful for specific border nodes, like in sequence diagrams, to center the border nodes if the - // * coordinate is 0 (x for EAST or WEST side, y for NORTH or SOUTH side) - // * - // * @return the absolute bounds of the node relative to the origin (Diagram) - // */ - // public static Rectangle getAbsoluteBounds2(Node node, boolean insetsAware, boolean adaptBorderNodeLocation) { - // return getAbsoluteBounds(node, insetsAware, false, false, adaptBorderNodeLocation); - // } - // - // /** - // * Get the absolute bounds relative to the origin (Diagram). - // * - // * @param node - // * the GMF Node - // * @param insetsAware - // * true to consider the draw2D figures insets. Warning: Those insets are based on the - // * current Sirius editParts and could become wrong if a developer customizes them. - // * @param boxForConnection - // * true if we want to have the bounds used to compute connection anchor from source or target, false - // * otherwise - // * @return the absolute bounds of the node relative to the origin (Diagram) - // */ - // public static Rectangle getAbsoluteBounds(Node node, boolean insetsAware, boolean boxForConnection, boolean - // adaptBorderNodeLocation) { - // return getAbsoluteBounds(node, insetsAware, boxForConnection, false, adaptBorderNodeLocation); - // } /** * Get the absolute bounds relative to the origin (Diagram). @@ -790,11 +730,10 @@ public static Rectangle getBounds(Node node, boolean useFigureForAutoSizeConstra PrecisionRectangle bounds = new PrecisionRectangle(computedAbsoluteLocation.preciseX(), computedAbsoluteLocation.preciseY(), 0, 0); LayoutConstraint layoutConstraint = node.getLayoutConstraint(); EObject element = node.getElement(); - if (element instanceof AbstractDNode) { - AbstractDNode abstractDNode = (AbstractDNode) element; - if (layoutConstraint instanceof Size) { - bounds.setWidth(((Size) layoutConstraint).getWidth()); - bounds.setHeight(((Size) layoutConstraint).getHeight()); + if (element instanceof AbstractDNode abstractDNode) { + if (layoutConstraint instanceof Size size) { + bounds.setWidth(size.getWidth()); + bounds.setHeight(size.getHeight()); } else { bounds.setWidth(-1); bounds.setHeight(-1); @@ -858,8 +797,7 @@ private static boolean isShadowBorderNeeded(Node node) { boolean needShadowBorder = false; EObject element = node.getElement(); ViewQuery viewQuery = new ViewQuery(node); - if (!viewQuery.isFreeFormCompartment() && !viewQuery.isListCompartment() && !viewQuery.isForNameEditPart() && element instanceof DDiagramElementContainer) { - DDiagramElementContainer ddec = (DDiagramElementContainer) element; + if (!viewQuery.isFreeFormCompartment() && !viewQuery.isListCompartment() && !viewQuery.isForNameEditPart() && element instanceof DDiagramElementContainer ddec) { needShadowBorder = !(new DDiagramElementContainerExperimentalQuery(ddec).isRegion() || ddec.getOwnedStyle() instanceof WorkspaceImage); } return needShadowBorder; @@ -890,14 +828,12 @@ private static void replaceAutoSize(Node node, PrecisionRectangle bounds, boolea ViewQuery nodeQuery = new ViewQuery(node); if (nodeQuery.isFreeFormCompartment() || nodeQuery.isListCompartment()) { defaultSize = new Dimension(ResizableCompartmentFigure.MIN_CLIENT_DP, ResizableCompartmentFigure.MIN_CLIENT_DP); - if (node.getChildren().isEmpty()) { - if (nodeQuery.isListCompartment() || nodeQuery.isVerticalRegionContainerCompartment() || nodeQuery.isHorizontalRegionContainerCompartment()) { - // Add one margin border (even if empty) - defaultSize.expand(0, 1); - } + if (node.getChildren().isEmpty() && (nodeQuery.isListCompartment() || nodeQuery.isVerticalRegionContainerCompartment() || nodeQuery.isHorizontalRegionContainerCompartment())) { + // Add one margin border (even if empty) + defaultSize.expand(0, 1); } - } else if (element instanceof AbstractDNode) { - defaultSize = getDefaultSize((AbstractDNode) element); + } else if (element instanceof AbstractDNode abstractDNode) { + defaultSize = getDefaultSize(abstractDNode); } } if (useFigureForAutoSizeConstraint) { @@ -907,8 +843,8 @@ private static void replaceAutoSize(Node node, PrecisionRectangle bounds, boolea // CHECKSTYLE:OFF if (optionalTargetEditPart.some()) { GraphicalEditPart graphicalEditPart = optionalTargetEditPart.get(); - if (graphicalEditPart instanceof AbstractDiagramElementContainerEditPart) { - ((AbstractDiagramElementContainerEditPart) graphicalEditPart).forceFigureAutosize(); + if (graphicalEditPart instanceof AbstractDiagramElementContainerEditPart abstractDiagramElementContainerEditPart) { + abstractDiagramElementContainerEditPart.forceFigureAutosize(); ((GraphicalEditPart) graphicalEditPart.getParent()).getFigure().validate(); } @@ -996,18 +932,16 @@ private static int getBorderNodesSides(Node container, Rectangle containerChildr private static void lookForNextRegionLocation(Rectangle bounds, Node node) { EObject element = node.getElement(); - if (element instanceof DDiagramElementContainer && node.eContainer() instanceof Node) { - DDiagramElementContainer ddec = (DDiagramElementContainer) element; + if (element instanceof DDiagramElementContainer ddec && node.eContainer() instanceof Node nodeContainer) { DDiagramElementContainerExperimentalQuery query = new DDiagramElementContainerExperimentalQuery(ddec); boolean isRegion = query.isRegion(); - EList children = ((Node) node.eContainer()).getChildren(); + EList children = nodeContainer.getChildren(); int currentIndex = children.indexOf(node); if (!(currentIndex != 0 && bounds.equals(new Rectangle(0, 0, -1, -1)))) { // We are not in the case of a new region insertion (in this // case, we use the default size) int nextIndex = currentIndex + 1; - if (isRegion && nextIndex != 0 && nextIndex < children.size() && children.get(nextIndex) instanceof Node) { - Node nextNode = (Node) children.get(nextIndex); + if (isRegion && nextIndex != 0 && nextIndex < children.size() && children.get(nextIndex) instanceof Node nextNode) { int visualID = SiriusVisualIDRegistry.getVisualID(nextNode.getType()); if (DNodeContainer2EditPart.VISUAL_ID == visualID || DNodeListEditPart.VISUAL_ID == visualID || DNodeList2EditPart.VISUAL_ID == visualID) { // DNodeContainerEditPart.VISUAL_ID == visualID is not @@ -1015,8 +949,7 @@ private static void lookForNextRegionLocation(Rectangle bounds, Node node) { // DNodeContainerEditPart as it is directly contained by // the diagram part. LayoutConstraint layoutConstraint = nextNode.getLayoutConstraint(); - if (layoutConstraint instanceof Location) { - Location nextLocation = (Location) layoutConstraint; + if (layoutConstraint instanceof Location nextLocation) { // Update only the parent stack direction if some // layout has already been done. if (bounds.width == -1 && query.isRegionInHorizontalStack() && nextLocation.getX() != 0) { @@ -1102,10 +1035,10 @@ private static Node getLastChild(Node container, boolean considerBorderNodes) { private static Dimension getDefaultSize(AbstractDNode abstractDNode) { Dimension defaultSize = new Dimension(-1, -1); - if (abstractDNode instanceof DNode) { - defaultSize = new DNodeQuery((DNode) abstractDNode).getDefaultDimension(); - } else if (abstractDNode instanceof DNodeContainer) { - defaultSize = new DNodeContainerQuery((DNodeContainer) abstractDNode).getDefaultDimension(); + if (abstractDNode instanceof DNode dNode) { + defaultSize = new DNodeQuery(dNode).getDefaultDimension(); + } else if (abstractDNode instanceof DNodeContainer dNodeContainer) { + defaultSize = new DNodeContainerQuery(dNodeContainer).getDefaultDimension(); } else if (abstractDNode instanceof DNodeList) { defaultSize = LayoutUtils.NEW_DEFAULT_CONTAINER_DIMENSION; } @@ -1127,10 +1060,10 @@ public static Option getGraphicalEditPart(View view) { IEditorPart editor = EclipseUIUtil.getActiveEditor(); if (isEditorFor(editor, gmfDiagram)) { return getGraphicalEditPart(view, (DiagramEditor) editor); - } else if (gmfDiagram.getElement() instanceof DDiagram) { + } else if (gmfDiagram.getElement() instanceof DDiagram dDiagram) { // Otherwise check all active Sirius editors for (IEditingSession uiSession : SessionUIManager.INSTANCE.getUISessions()) { - DialectEditor dialectEditor = uiSession.getEditor((DDiagram) gmfDiagram.getElement()); + DialectEditor dialectEditor = uiSession.getEditor(dDiagram); if (isEditorFor(dialectEditor, gmfDiagram)) { return getGraphicalEditPart(view, (DiagramEditor) dialectEditor); } @@ -1141,7 +1074,7 @@ public static Option getGraphicalEditPart(View view) { } private static boolean isEditorFor(IEditorPart editor, Diagram diagram) { - return editor instanceof DiagramEditor && ((DiagramEditor) editor).getDiagram() == diagram; + return editor instanceof DiagramEditor diagramEditor && diagramEditor.getDiagram() == diagram; } /** @@ -1158,8 +1091,8 @@ public static Option getGraphicalEditPart(View view, DiagramE Option result = Options.newNone(); final Map editPartRegistry = editor.getDiagramGraphicalViewer().getEditPartRegistry(); final EditPart targetEditPart = (EditPart) editPartRegistry.get(view); - if (targetEditPart instanceof GraphicalEditPart) { - result = Options.newSome((GraphicalEditPart) targetEditPart); + if (targetEditPart instanceof GraphicalEditPart graphicalEditPart) { + result = Options.newSome(graphicalEditPart); } return result; } @@ -1174,10 +1107,8 @@ public static Option getGraphicalEditPart(View view, DiagramE * when the edgeEditPart is not as expected */ public static List getPointsFromSource(ConnectionEditPart edgeEditPart) throws IllegalArgumentException { - if (edgeEditPart.getModel() instanceof Edge && edgeEditPart.getFigure() instanceof Connection) { + if (edgeEditPart.getModel() instanceof Edge gmfEdge && edgeEditPart.getFigure() instanceof Connection connectionFigure) { List result = new ArrayList<>(); - Edge gmfEdge = (Edge) edgeEditPart.getModel(); - Connection connectionFigure = (Connection) edgeEditPart.getFigure(); Point srcAnchorLoc = connectionFigure.getSourceAnchor().getReferencePoint(); connectionFigure.translateToRelative(srcAnchorLoc); @@ -1202,10 +1133,8 @@ public static List getPointsFromSource(ConnectionEditPart edgeEditPart) t * when the edgeEditPart is not as expected */ public static List getPointsFromTarget(ConnectionEditPart edgeEditPart) throws IllegalArgumentException { - if (edgeEditPart.getModel() instanceof Edge && edgeEditPart.getFigure() instanceof Connection) { + if (edgeEditPart.getModel() instanceof Edge gmfEdge && edgeEditPart.getFigure() instanceof Connection connectionFigure) { List result = new ArrayList<>(); - Edge gmfEdge = (Edge) edgeEditPart.getModel(); - Connection connectionFigure = (Connection) edgeEditPart.getFigure(); Point tgtAnchorLoc = connectionFigure.getTargetAnchor().getReferencePoint(); connectionFigure.translateToRelative(tgtAnchorLoc); @@ -1234,35 +1163,31 @@ public static Dimension getLabelDimension(Node node, Dimension defaultDimension) Dimension labelSize = defaultDimension; ViewQuery viewQuery = new ViewQuery(node); EObject element = node.getElement(); - if (element instanceof DDiagramElement) { - DDiagramElement dDiagramElement = (DDiagramElement) element; + if (element instanceof DDiagramElement dDiagramElement) { org.eclipse.sirius.viewpoint.Style siriusStyle = dDiagramElement.getStyle(); - if (!new DDiagramElementQuery(dDiagramElement).isLabelHidden()) { - if (siriusStyle instanceof BasicLabelStyle) { - BasicLabelStyle bls = (BasicLabelStyle) siriusStyle; - String fontName = (String) viewQuery.getDefaultValue(NotationPackage.Literals.FONT_STYLE__FONT_NAME); - Optional