Skip to content

Commit

Permalink
[285] Compute element initial size by using the VSM information
Browse files Browse the repository at this point in the history
Bug: #285
Signed-off-by: Florian Barbin <florian.barbin@obeo.fr>
  • Loading branch information
florianbarbin committed Mar 3, 2021
1 parent dad4389 commit d4ea022
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,6 +33,11 @@
*/
public class ContainerMappingStyleProvider implements Function<VariableManager, INodeStyle> {

/**
* 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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,6 +33,8 @@
*/
public class NodeMappingStyleProvider implements Function<VariableManager, INodeStyle> {

private static final int SIZE_FACTOR = 10;

private final AQLInterpreter interpreter;

private final NodeMapping nodeMapping;
Expand Down Expand Up @@ -86,13 +88,32 @@ 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()
.color(color)
.borderColor(borderColor)
.borderSize(borderSize)
.borderStyle(borderStyle)
.width(width)
.height(height)
.build();
// @formatter:on
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -35,6 +36,10 @@ public final class RectangularNodeStyle implements INodeStyle {

private int borderSize;

private int width;

private int height;

private LineStyle borderStyle;

private RectangularNodeStyle() {
Expand All @@ -59,6 +64,14 @@ public int getBorderSize() {
return this.borderSize;
}

public Optional<Integer> getHeight() {
return Optional.ofNullable(this.height);
}

public Optional<Integer> getWidth() {
return Optional.ofNullable(this.width);
}

@GraphQLNonNull
@GraphQLField
public LineStyle getBorderStyle() {
Expand Down Expand Up @@ -89,6 +102,10 @@ public static final class Builder {

private int borderSize;

private int width;

private int height;

private LineStyle borderStyle;

private Builder() {
Expand All @@ -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;
}

}

}

0 comments on commit d4ea022

Please sign in to comment.