diff --git a/backend/sirius-web-compatibility/src/main/java/org/eclipse/sirius/web/compat/diagrams/ContainerMappingStyleProvider.java b/backend/sirius-web-compatibility/src/main/java/org/eclipse/sirius/web/compat/diagrams/ContainerMappingStyleProvider.java index 628b56dda4c..ba57ed2ce7f 100644 --- a/backend/sirius-web-compatibility/src/main/java/org/eclipse/sirius/web/compat/diagrams/ContainerMappingStyleProvider.java +++ b/backend/sirius-web-compatibility/src/main/java/org/eclipse/sirius/web/compat/diagrams/ContainerMappingStyleProvider.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2021 Obeo. + * Copyright (c) 2019, 2021 Obeo and others. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -33,6 +33,11 @@ */ public class ContainerMappingStyleProvider implements Function { + /** + * Inherit from Sirius Desktop. Currently, the size specified in the VSM is multiplied by 10. + */ + private static final int SIZE_FACTOR = 10; + private final AQLInterpreter interpreter; private final ContainerMapping containerMapping; @@ -75,11 +80,18 @@ private RectangularNodeStyle createRectangularNodeStyle(VariableManager variable Result result = this.interpreter.evaluateExpression(variableManager.getVariables(), flatContainerStyleDescription.getBorderSizeComputationExpression()); int borderSize = result.asInt().getAsInt(); + result = this.interpreter.evaluateExpression(variableManager.getVariables(), flatContainerStyleDescription.getWidthComputationExpression()); + int width = result.asInt().getAsInt() * SIZE_FACTOR; + result = this.interpreter.evaluateExpression(variableManager.getVariables(), flatContainerStyleDescription.getHeightComputationExpression()); + int height = result.asInt().getAsInt() * SIZE_FACTOR; + // @formatter:off return RectangularNodeStyle.newRectangularNodeStyle() .color(color) .borderColor(borderColor) .borderSize(borderSize) + .width(width) + .height(height) .borderStyle(borderStyle) .build(); // @formatter:on diff --git a/backend/sirius-web-compatibility/src/main/java/org/eclipse/sirius/web/compat/diagrams/NodeMappingStyleProvider.java b/backend/sirius-web-compatibility/src/main/java/org/eclipse/sirius/web/compat/diagrams/NodeMappingStyleProvider.java index c321d9005d8..76180870a80 100644 --- a/backend/sirius-web-compatibility/src/main/java/org/eclipse/sirius/web/compat/diagrams/NodeMappingStyleProvider.java +++ b/backend/sirius-web-compatibility/src/main/java/org/eclipse/sirius/web/compat/diagrams/NodeMappingStyleProvider.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2021 Obeo. + * Copyright (c) 2019, 2021 Obeo and others. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -33,6 +33,8 @@ */ public class NodeMappingStyleProvider implements Function { + private static final int SIZE_FACTOR = 10; + private final AQLInterpreter interpreter; private final NodeMapping nodeMapping; @@ -86,6 +88,23 @@ private RectangularNodeStyle createRectangularNodeStyle(VariableManager variable Result result = this.interpreter.evaluateExpression(variableManager.getVariables(), squareDescription.getBorderSizeComputationExpression()); int borderSize = result.asInt().getAsInt(); + Integer width = squareDescription.getWidth() * SIZE_FACTOR; + Integer height = squareDescription.getHeight() * SIZE_FACTOR; + + // If the initial width and/or height have not been set by the specifier, we interpret the size computation + // expression to set the width and/or height + if (width == 0 || height == 0) { + result = this.interpreter.evaluateExpression(variableManager.getVariables(), squareDescription.getSizeComputationExpression()); + int size = result.asInt().getAsInt() * SIZE_FACTOR; + if (size > 0) { + if (width == 0) { + width = size; + } + if (height == 0) { + height = size; + } + } + } // @formatter:off return RectangularNodeStyle.newRectangularNodeStyle() @@ -93,6 +112,8 @@ private RectangularNodeStyle createRectangularNodeStyle(VariableManager variable .borderColor(borderColor) .borderSize(borderSize) .borderStyle(borderStyle) + .width(width) + .height(height) .build(); // @formatter:on } diff --git a/backend/sirius-web-compatibility/src/test/java/org/eclipse/sirius/web/compat/diagrams/ContainerMappingStyleProviderTestCases.java b/backend/sirius-web-compatibility/src/test/java/org/eclipse/sirius/web/compat/diagrams/ContainerMappingStyleProviderTestCases.java index d2aace3ef0c..0456e615800 100644 --- a/backend/sirius-web-compatibility/src/test/java/org/eclipse/sirius/web/compat/diagrams/ContainerMappingStyleProviderTestCases.java +++ b/backend/sirius-web-compatibility/src/test/java/org/eclipse/sirius/web/compat/diagrams/ContainerMappingStyleProviderTestCases.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2020 Obeo. + * Copyright (c) 2019, 2021 Obeo and others. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -23,6 +23,7 @@ import org.eclipse.sirius.diagram.description.ContainerMapping; import org.eclipse.sirius.diagram.description.DescriptionFactory; import org.eclipse.sirius.diagram.description.style.ContainerStyleDescription; +import org.eclipse.sirius.diagram.description.style.FlatContainerStyleDescription; import org.eclipse.sirius.diagram.description.style.StyleFactory; import org.eclipse.sirius.viewpoint.description.FixedColor; import org.eclipse.sirius.web.diagrams.INodeStyle; @@ -75,8 +76,8 @@ private ConditionalContainerStyleDescription createConditionalStyle(String predi return conditionalContainerStyle; } - private ContainerStyleDescription createFlatStyle(int red, int green, int blue) { - ContainerStyleDescription containerStyleDescription = StyleFactory.eINSTANCE.createFlatContainerStyleDescription(); + private FlatContainerStyleDescription createFlatStyle(int red, int green, int blue) { + FlatContainerStyleDescription containerStyleDescription = StyleFactory.eINSTANCE.createFlatContainerStyleDescription(); FixedColor fixedColor = org.eclipse.sirius.viewpoint.description.DescriptionFactory.eINSTANCE.createFixedColor(); fixedColor.setRed(red); @@ -110,4 +111,22 @@ public void testBorderLineStyleConversion() { assertThat(rectangularNodeStyle.getBorderStyle()).isEqualTo(entry.getValue()); } } + + @Test + public void testSizeComputationConversion() { + + ContainerMapping containerMapping = DescriptionFactory.eINSTANCE.createContainerMapping(); + FlatContainerStyleDescription style = this.createFlatStyle(0, 0, 0); + style.setWidthComputationExpression("aql:10"); //$NON-NLS-1$ + style.setHeightComputationExpression("aql:20"); //$NON-NLS-1$ + containerMapping.setStyle(style); + + VariableManager variableManager = new VariableManager(); + AQLInterpreter interpreter = new AQLInterpreter(List.of(), List.of(EcorePackage.eINSTANCE)); + INodeStyle nodeStyle = new ContainerMappingStyleProvider(interpreter, containerMapping).apply(variableManager); + assertThat(nodeStyle).isInstanceOf(RectangularNodeStyle.class); + RectangularNodeStyle rectangularNodeStyle = (RectangularNodeStyle) nodeStyle; + assertThat(rectangularNodeStyle.getWidth()).contains(100); + assertThat(rectangularNodeStyle.getHeight()).contains(200); + } } diff --git a/backend/sirius-web-compatibility/src/test/java/org/eclipse/sirius/web/compat/diagrams/NodeMappingStyleProviderTestCases.java b/backend/sirius-web-compatibility/src/test/java/org/eclipse/sirius/web/compat/diagrams/NodeMappingStyleProviderTestCases.java index d50e4db5321..6f007f26ed6 100644 --- a/backend/sirius-web-compatibility/src/test/java/org/eclipse/sirius/web/compat/diagrams/NodeMappingStyleProviderTestCases.java +++ b/backend/sirius-web-compatibility/src/test/java/org/eclipse/sirius/web/compat/diagrams/NodeMappingStyleProviderTestCases.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2020 Obeo. + * Copyright (c) 2019, 2021 Obeo and others. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -23,6 +23,7 @@ import org.eclipse.sirius.diagram.description.DescriptionFactory; import org.eclipse.sirius.diagram.description.NodeMapping; import org.eclipse.sirius.diagram.description.style.NodeStyleDescription; +import org.eclipse.sirius.diagram.description.style.SquareDescription; import org.eclipse.sirius.diagram.description.style.StyleFactory; import org.eclipse.sirius.viewpoint.description.FixedColor; import org.eclipse.sirius.web.diagrams.INodeStyle; @@ -74,16 +75,16 @@ private ConditionalNodeStyleDescription createConditionalStyle(String predicateE return conditionalNodeStyle; } - private NodeStyleDescription createSquareStyle(int red, int green, int blue) { - NodeStyleDescription nodeStyleDescription = StyleFactory.eINSTANCE.createSquareDescription(); + private SquareDescription createSquareStyle(int red, int green, int blue) { + SquareDescription squareDescription = StyleFactory.eINSTANCE.createSquareDescription(); FixedColor fixedColor = org.eclipse.sirius.viewpoint.description.DescriptionFactory.eINSTANCE.createFixedColor(); fixedColor.setRed(red); fixedColor.setGreen(green); fixedColor.setBlue(blue); - nodeStyleDescription.setBorderColor(fixedColor); - return nodeStyleDescription; + squareDescription.setBorderColor(fixedColor); + return squareDescription; } @Test @@ -109,4 +110,29 @@ public void testBorderLineStyleConversion() { assertThat(rectangularNodeStyle.getBorderStyle()).isEqualTo(entry.getValue()); } } + + @Test + public void testSizeComputationStyleConversion() { + NodeMapping nodeMapping = DescriptionFactory.eINSTANCE.createNodeMapping(); + SquareDescription style = this.createSquareStyle(0, 0, 0); + style.setSizeComputationExpression("3"); //$NON-NLS-1$ + nodeMapping.setStyle(style); + + VariableManager variableManager = new VariableManager(); + AQLInterpreter interpreter = new AQLInterpreter(List.of(), List.of(EcorePackage.eINSTANCE)); + INodeStyle nodeStyle = new NodeMappingStyleProvider(interpreter, nodeMapping).apply(variableManager); + assertThat(nodeStyle).isInstanceOf(RectangularNodeStyle.class); + RectangularNodeStyle rectangularNodeStyle = (RectangularNodeStyle) nodeStyle; + assertThat(rectangularNodeStyle.getWidth()).contains(30); + assertThat(rectangularNodeStyle.getHeight()).contains(30); + + style.setSizeComputationExpression(null); + style.setWidth(2); + style.setHeight(3); + nodeStyle = new NodeMappingStyleProvider(interpreter, nodeMapping).apply(variableManager); + assertThat(nodeStyle).isInstanceOf(RectangularNodeStyle.class); + rectangularNodeStyle = (RectangularNodeStyle) nodeStyle; + assertThat(rectangularNodeStyle.getWidth()).contains(20); + assertThat(rectangularNodeStyle.getHeight()).contains(30); + } } diff --git a/backend/sirius-web-diagrams-layout/src/main/java/org/eclipse/sirius/web/diagrams/layout/incremental/provider/NodeSizeProvider.java b/backend/sirius-web-diagrams-layout/src/main/java/org/eclipse/sirius/web/diagrams/layout/incremental/provider/NodeSizeProvider.java index f09ea535752..58aca98928c 100644 --- a/backend/sirius-web-diagrams-layout/src/main/java/org/eclipse/sirius/web/diagrams/layout/incremental/provider/NodeSizeProvider.java +++ b/backend/sirius-web-diagrams-layout/src/main/java/org/eclipse/sirius/web/diagrams/layout/incremental/provider/NodeSizeProvider.java @@ -16,6 +16,7 @@ import org.eclipse.sirius.web.diagrams.ImageNodeStyle; import org.eclipse.sirius.web.diagrams.ImageNodeStyleSizeProvider; import org.eclipse.sirius.web.diagrams.ImageSizeProvider; +import org.eclipse.sirius.web.diagrams.RectangularNodeStyle; import org.eclipse.sirius.web.diagrams.Size; /** @@ -32,12 +33,29 @@ public class NodeSizeProvider { */ private static final int ELK_SIZE_DIFF = 24; + private static final int DEFAULT_WIDTH = 150; + + private static final int DEFAULT_HEIGHT = 70; + public Size getSize(INodeStyle style) { + double width = DEFAULT_WIDTH; + double height = DEFAULT_HEIGHT; if (style instanceof ImageNodeStyle) { Size size = new ImageNodeStyleSizeProvider(new ImageSizeProvider()).getSize((ImageNodeStyle) style); - return Size.of(size.getWidth() + ELK_SIZE_DIFF, size.getHeight() + ELK_SIZE_DIFF); + width = size.getWidth() + ELK_SIZE_DIFF; + height = size.getHeight() + ELK_SIZE_DIFF; + } else if (style instanceof RectangularNodeStyle) { + RectangularNodeStyle rectangularNodeStyle = (RectangularNodeStyle) style; + //@formatter:off + width = rectangularNodeStyle.getWidth() + .filter(value -> value > 0) + .orElse(DEFAULT_WIDTH); + height = rectangularNodeStyle.getHeight() + .filter(value -> value > 0) + .orElse(DEFAULT_HEIGHT); + //@formatter:on } - return Size.of(150, 70); + return Size.of(width, height); } } diff --git a/backend/sirius-web-diagrams-layout/src/test/java/org/eclipse/sirius/web/diagrams/layout/incremental/NodeSizeProviderTestCases.java b/backend/sirius-web-diagrams-layout/src/test/java/org/eclipse/sirius/web/diagrams/layout/incremental/NodeSizeProviderTestCases.java index 4fa035a8e56..441e977c515 100644 --- a/backend/sirius-web-diagrams-layout/src/test/java/org/eclipse/sirius/web/diagrams/layout/incremental/NodeSizeProviderTestCases.java +++ b/backend/sirius-web-diagrams-layout/src/test/java/org/eclipse/sirius/web/diagrams/layout/incremental/NodeSizeProviderTestCases.java @@ -14,6 +14,8 @@ import static org.assertj.core.api.Assertions.assertThat; +import org.eclipse.sirius.web.diagrams.LineStyle; +import org.eclipse.sirius.web.diagrams.RectangularNodeStyle; import org.eclipse.sirius.web.diagrams.Size; import org.eclipse.sirius.web.diagrams.layout.incremental.provider.NodeSizeProvider; import org.junit.Test; @@ -25,11 +27,56 @@ */ public class NodeSizeProviderTestCases { + private static final int HEIGHT_70 = 70; + + private static final int WIDTH_150 = 150; + + private static final int WIDTH_50 = 50; + + private static final int HEIGHT_20 = 20; + @Test public void testNodeSize() { NodeSizeProvider nodeSizeProvider = new NodeSizeProvider(); Size size = nodeSizeProvider.getSize(null); - assertThat(size).extracting(Size::getHeight).isEqualTo(Double.valueOf(70)); - assertThat(size).extracting(Size::getWidth).isEqualTo(Double.valueOf(150)); + assertThat(size).extracting(Size::getHeight).isEqualTo(Double.valueOf(HEIGHT_70)); + assertThat(size).extracting(Size::getWidth).isEqualTo(Double.valueOf(WIDTH_150)); + } + + @Test + public void testRectangularNodeStyleSize() { + NodeSizeProvider nodeSizeProvider = new NodeSizeProvider(); + Size size = nodeSizeProvider.getSize(this.getRectangularNodeStyle(WIDTH_50, HEIGHT_20)); + assertThat(size).extracting(Size::getHeight).isEqualTo(Double.valueOf(HEIGHT_20)); + assertThat(size).extracting(Size::getWidth).isEqualTo(Double.valueOf(WIDTH_50)); + } + + @Test + public void testRectangularNodeStyleNoWidthSize() { + NodeSizeProvider nodeSizeProvider = new NodeSizeProvider(); + Size size = nodeSizeProvider.getSize(this.getRectangularNodeStyle(0, HEIGHT_20)); + assertThat(size).extracting(Size::getHeight).isEqualTo(Double.valueOf(HEIGHT_20)); + assertThat(size).extracting(Size::getWidth).isEqualTo(Double.valueOf(WIDTH_150)); + } + + @Test + public void testRectangularNodeStyleNoHeightSize() { + NodeSizeProvider nodeSizeProvider = new NodeSizeProvider(); + Size size = nodeSizeProvider.getSize(this.getRectangularNodeStyle(WIDTH_50, 0)); + assertThat(size).extracting(Size::getHeight).isEqualTo(Double.valueOf(HEIGHT_70)); + assertThat(size).extracting(Size::getWidth).isEqualTo(Double.valueOf(WIDTH_50)); + } + + private RectangularNodeStyle getRectangularNodeStyle(int width, int height) { + // @formatter:off + return RectangularNodeStyle.newRectangularNodeStyle() + .borderColor("#000000") //$NON-NLS-1$ + .borderSize(1) + .borderStyle(LineStyle.Solid) + .height(height) + .width(width) + .color("#FFFFFF") //$NON-NLS-1$ + .build(); + // @formatter:on } } diff --git a/backend/sirius-web-diagrams/src/main/java/org/eclipse/sirius/web/diagrams/RectangularNodeStyle.java b/backend/sirius-web-diagrams/src/main/java/org/eclipse/sirius/web/diagrams/RectangularNodeStyle.java index 3002007fe17..0b2b01703c3 100644 --- a/backend/sirius-web-diagrams/src/main/java/org/eclipse/sirius/web/diagrams/RectangularNodeStyle.java +++ b/backend/sirius-web-diagrams/src/main/java/org/eclipse/sirius/web/diagrams/RectangularNodeStyle.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2020 Obeo. + * Copyright (c) 2019, 2021 Obeo and others. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -14,6 +14,7 @@ import java.text.MessageFormat; import java.util.Objects; +import java.util.Optional; import org.eclipse.sirius.web.annotations.Immutable; import org.eclipse.sirius.web.annotations.graphql.GraphQLField; @@ -35,6 +36,10 @@ public final class RectangularNodeStyle implements INodeStyle { private int borderSize; + private int width; + + private int height; + private LineStyle borderStyle; private RectangularNodeStyle() { @@ -59,6 +64,14 @@ public int getBorderSize() { return this.borderSize; } + public Optional getHeight() { + return Optional.ofNullable(this.height); + } + + public Optional getWidth() { + return Optional.ofNullable(this.width); + } + @GraphQLNonNull @GraphQLField public LineStyle getBorderStyle() { @@ -89,6 +102,10 @@ public static final class Builder { private int borderSize; + private int width; + + private int height; + private LineStyle borderStyle; private Builder() { @@ -115,14 +132,27 @@ public Builder borderStyle(LineStyle borderStyle) { return this; } + public Builder width(int width) { + this.width = Objects.requireNonNull(width); + return this; + } + + public Builder height(int height) { + this.height = Objects.requireNonNull(height); + return this; + } + public RectangularNodeStyle build() { RectangularNodeStyle nodeStyleDescription = new RectangularNodeStyle(); nodeStyleDescription.color = Objects.requireNonNull(this.color); nodeStyleDescription.borderColor = Objects.requireNonNull(this.borderColor); nodeStyleDescription.borderSize = Objects.requireNonNull(this.borderSize); + nodeStyleDescription.height = this.height; + nodeStyleDescription.width = this.width; nodeStyleDescription.borderStyle = Objects.requireNonNull(this.borderStyle); return nodeStyleDescription; } + } }