Skip to content

Commit 6201c03

Browse files
Tidies up element width/height handling, which needs to differ across diagram exporters.
1 parent 6e9f8e7 commit 6201c03

File tree

12 files changed

+518
-365
lines changed

12 files changed

+518
-365
lines changed

structurizr-autolayout/src/main/java/com/structurizr/autolayout/graphviz/DOTExporter.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.structurizr.model.*;
77
import com.structurizr.util.StringUtils;
88
import com.structurizr.view.DeploymentView;
9+
import com.structurizr.view.ElementStyle;
910
import com.structurizr.view.ModelView;
1011
import com.structurizr.view.RelationshipView;
1112

@@ -114,10 +115,12 @@ protected void endDeploymentNodeBoundary(ModelView view, IndentingWriter writer)
114115

115116
@Override
116117
protected void writeElement(ModelView view, Element element, IndentingWriter writer) {
118+
ElementStyle elementStyle = StyleUtils.findElementStyle(view, element);
119+
117120
writer.writeLine(String.format(locale, "%s [width=%f,height=%f,fixedsize=true,id=%s,label=\"%s: %s\"]",
118121
element.getId(),
119-
getElementWidth(view, element.getId()) / Constants.STRUCTURIZR_DPI, // convert Structurizr dimensions to inches
120-
getElementHeight(view, element.getId()) / Constants.STRUCTURIZR_DPI, // convert Structurizr dimensions to inches
122+
elementStyle.getWidth() / Constants.STRUCTURIZR_DPI, // convert Structurizr dimensions to inches
123+
elementStyle.getHeight() / Constants.STRUCTURIZR_DPI, // convert Structurizr dimensions to inches
121124
element.getId(),
122125
element.getId(),
123126
escape(element.getName())
@@ -217,14 +220,4 @@ private Element findElementInside(DeploymentNode deploymentNode, ModelView view)
217220
return null;
218221
}
219222

220-
private int getElementWidth(ModelView view, String elementId) {
221-
Element element = view.getModel().getElement(elementId);
222-
return view.getViewSet().getConfiguration().getStyles().findElementStyle(element).getWidth();
223-
}
224-
225-
private int getElementHeight(ModelView view, String elementId) {
226-
Element element = view.getModel().getElement(elementId);
227-
return view.getViewSet().getConfiguration().getStyles().findElementStyle(element).getHeight();
228-
}
229-
230223
}

structurizr-autolayout/src/main/java/com/structurizr/autolayout/graphviz/SVGReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void parseAndApplyLayout(ModelView view) throws Exception {
8989
minimumX = Math.min(elementView.getX(), minimumX);
9090
minimumY = Math.min(elementView.getY(), minimumY);
9191

92-
ElementStyle style = view.getViewSet().getConfiguration().getStyles().findElementStyle(view.getModel().getElement(elementView.getId()));
92+
ElementStyle style = StyleUtils.findElementStyle(view, view.getModel().getElement(elementView.getId()));
9393

9494
maximumX = Math.max(elementView.getX() + style.getWidth(), maximumX);
9595
maximumY = Math.max(elementView.getY() + style.getHeight(), maximumY);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.structurizr.autolayout.graphviz;
2+
3+
import com.structurizr.model.Element;
4+
import com.structurizr.view.ElementStyle;
5+
import com.structurizr.view.Shape;
6+
import com.structurizr.view.View;
7+
8+
class StyleUtils {
9+
10+
private static final int DEFAULT_WIDTH = 450;
11+
private static final int DEFAULT_HEIGHT = 300;
12+
13+
private static final int DEFAULT_WIDTH_PERSON = 400;
14+
private static final int DEFAULT_HEIGHT_PERSON = 400;
15+
16+
static ElementStyle findElementStyle(View view, Element element) {
17+
ElementStyle style = view.getViewSet().getConfiguration().getStyles().findElementStyle(element);
18+
19+
if (style.getWidth() == null) {
20+
if (style.getShape() == Shape.Person || style.getShape() == Shape.Robot) {
21+
style.setWidth(DEFAULT_WIDTH_PERSON);
22+
} else {
23+
style.setWidth(DEFAULT_WIDTH);
24+
}
25+
}
26+
27+
if (style.getHeight() == null) {
28+
if (style.getShape() == Shape.Person || style.getShape() == Shape.Robot) {
29+
style.setHeight(DEFAULT_HEIGHT_PERSON);
30+
} else {
31+
style.setHeight(DEFAULT_HEIGHT);
32+
}
33+
}
34+
35+
return style;
36+
}
37+
38+
}

structurizr-autolayout/src/test/java/com/structurizr/autolayout/graphviz/GraphvizAutomaticLayoutTests.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.structurizr.view.AutomaticLayout;
99
import com.structurizr.view.Shape;
1010
import com.structurizr.view.SystemContextView;
11-
import com.structurizr.view.SystemLandscapeView;
1211
import org.junit.jupiter.api.Test;
1312

1413
import java.io.File;
@@ -23,36 +22,36 @@ public void apply_Workspace() throws Exception {
2322
File tempDir = Files.createTempDirectory("graphviz").toFile();
2423
GraphvizAutomaticLayout graphviz = new GraphvizAutomaticLayout(tempDir);
2524

26-
Workspace workspace = new Workspace("Name");
27-
SoftwareSystem a = workspace.getModel().addSoftwareSystem("A");
28-
SoftwareSystem b = workspace.getModel().addSoftwareSystem("B");
29-
a.uses(b, "Uses");
25+
Workspace workspace = new Workspace("Name", "");
26+
Person user = workspace.getModel().addPerson("User");
27+
SoftwareSystem softwareSystem = workspace.getModel().addSoftwareSystem("Software System");
28+
user.uses(softwareSystem, "Uses");
3029

31-
SystemLandscapeView view = workspace.getViews().createSystemLandscapeView("key");
30+
SystemContextView view = workspace.getViews().createSystemContextView(softwareSystem, "SystemContext", "");
3231
view.addAllElements();
3332

3433
workspace.getViews().getConfiguration().getStyles().addElementStyle(Tags.PERSON).shape(Shape.Person);
3534

36-
assertEquals(0, view.getElementView(a).getX());
37-
assertEquals(0, view.getElementView(a).getY());
38-
assertEquals(0, view.getElementView(b).getX());
39-
assertEquals(0, view.getElementView(b).getY());
35+
assertEquals(0, view.getElementView(user).getX());
36+
assertEquals(0, view.getElementView(user).getY());
37+
assertEquals(0, view.getElementView(softwareSystem).getX());
38+
assertEquals(0, view.getElementView(softwareSystem).getY());
4039

4140
graphviz.apply(workspace);
4241

4342
// no change - the view doesn't have automatic layout configured
44-
assertEquals(0, view.getElementView(a).getX());
45-
assertEquals(0, view.getElementView(a).getY());
46-
assertEquals(0, view.getElementView(b).getX());
47-
assertEquals(0, view.getElementView(b).getY());
43+
assertEquals(0, view.getElementView(user).getX());
44+
assertEquals(0, view.getElementView(user).getY());
45+
assertEquals(0, view.getElementView(softwareSystem).getX());
46+
assertEquals(0, view.getElementView(softwareSystem).getY());
4847

4948
view.enableAutomaticLayout(AutomaticLayout.RankDirection.TopBottom);
5049
graphviz.apply(workspace);
5150

52-
assertEquals(208, view.getElementView(a).getX());
53-
assertEquals(208, view.getElementView(a).getY());
54-
assertEquals(208, view.getElementView(b).getX());
55-
assertEquals(808, view.getElementView(b).getY());
51+
assertEquals(233, view.getElementView(user).getX());
52+
assertEquals(208, view.getElementView(user).getY());
53+
assertEquals(208, view.getElementView(softwareSystem).getX());
54+
assertEquals(908, view.getElementView(softwareSystem).getY());
5655
}
5756

5857
}

structurizr-client/src/test/java/com/structurizr/view/ThemeUtilsTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ void findElementStyle_WithThemes() {
9494
workspace.getViews().getConfiguration().getStyles().addStylesFromTheme(new Theme(elementStyles, relationshipStyles));
9595

9696
ElementStyle style = workspace.getViews().getConfiguration().getStyles().findElementStyle(softwareSystem);
97-
assertEquals(Integer.valueOf(450), style.getWidth());
98-
assertEquals(Integer.valueOf(300), style.getHeight());
97+
assertNull(style.getWidth());
98+
assertNull(style.getHeight());
9999
assertEquals("#ff0000", style.getBackground()); // from theme 2
100100
assertEquals("#ffffff", style.getColor()); // from theme 1
101101
assertEquals(Integer.valueOf(24), style.getFontSize());

structurizr-core/src/main/java/com/structurizr/view/ElementStyle.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
*/
1010
public final class ElementStyle extends AbstractStyle {
1111

12-
public static final int DEFAULT_WIDTH = 450;
13-
public static final int DEFAULT_HEIGHT = 300;
14-
1512
@JsonInclude(value = JsonInclude.Include.NON_NULL)
1613
private Integer width;
1714

structurizr-core/src/main/java/com/structurizr/view/Styles.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public ElementStyle findElementStyle(Element element) {
270270
* @return an ElementStyle object
271271
*/
272272
public ElementStyle findElementStyle(Element element, ColorScheme colorScheme) {
273-
ElementStyle style = new ElementStyle(Tags.ELEMENT).shape(Shape.Box).width(ElementStyle.DEFAULT_WIDTH).height(ElementStyle.DEFAULT_HEIGHT).fontSize(24).border(Border.Solid).opacity(100).metadata(true).description(true);
273+
ElementStyle style = new ElementStyle(Tags.ELEMENT).shape(Shape.Box).fontSize(24).border(Border.Solid).opacity(100).metadata(true).description(true);
274274

275275
if (element != null) {
276276
Set<String> tagsUsedToComposeStyle = new LinkedHashSet<>();

structurizr-core/src/test/java/com/structurizr/view/StylesTests.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ void test_sortingOfRelationshipStyles() {
5353
@Test
5454
void findElementStyle_ReturnsTheDefaultStyle_WhenPassedNull() {
5555
ElementStyle style = styles.findElementStyle((Element) null);
56-
assertEquals(Integer.valueOf(450), style.getWidth());
57-
assertEquals(Integer.valueOf(300), style.getHeight());
56+
assertNull(style.getWidth());
57+
assertNull(style.getHeight());
5858
assertEquals("#ffffff", style.getBackground());
5959
assertEquals("#444444", style.getColor());
6060
assertEquals("#444444", style.getStroke());
@@ -72,8 +72,8 @@ void findElementStyle_ReturnsTheDefaultStyle_WhenPassedNull() {
7272
void findElementStyle_ReturnsTheDefaultStyle_WhenNoStylesAreDefined() {
7373
SoftwareSystem element = model.addSoftwareSystem("Name", "Description");
7474
ElementStyle style = styles.findElementStyle(element);
75-
assertEquals(Integer.valueOf(450), style.getWidth());
76-
assertEquals(Integer.valueOf(300), style.getHeight());
75+
assertNull(style.getWidth());
76+
assertNull(style.getHeight());
7777
assertEquals("#ffffff", style.getBackground());
7878
assertEquals("#444444", style.getColor());
7979
assertEquals("#444444", style.getStroke());
@@ -91,8 +91,8 @@ void findElementStyle_ReturnsTheDefaultStyle_WhenNoStylesAreDefined() {
9191
void findElementStyleForDarkMode_ReturnsTheDefaultStyle_WhenNoStylesAreDefined() {
9292
SoftwareSystem element = model.addSoftwareSystem("Name", "Description");
9393
ElementStyle style = styles.findElementStyle(element, ColorScheme.Dark);
94-
assertEquals(Integer.valueOf(450), style.getWidth());
95-
assertEquals(Integer.valueOf(300), style.getHeight());
94+
assertNull(style.getWidth());
95+
assertNull(style.getHeight());
9696
assertEquals("#111111", style.getBackground());
9797
assertEquals("#cccccc", style.getColor());
9898
assertEquals("#cccccc", style.getStroke());
@@ -158,20 +158,6 @@ void findElementStyle_ReturnsTheCorrectStyleForAnElementInstance_WhenStylesAreDe
158158
assertEquals("value", style.getProperties().get("name"));
159159
}
160160

161-
@Test
162-
void findElementStyle_ReturnsTheDefaultElementSize_WhenTheShapeIsABox() {
163-
SoftwareSystem element = model.addSoftwareSystem("Name", "Description");
164-
element.addTags("Some Tag");
165-
166-
styles.addElementStyle(Tags.SOFTWARE_SYSTEM).background("#ff0000").color("#ffffff");
167-
styles.addElementStyle("Some Tag").shape(Shape.Box);
168-
169-
ElementStyle style = styles.findElementStyle(element);
170-
assertEquals(Shape.Box, style.getShape());
171-
assertEquals(Integer.valueOf(450), style.getWidth());
172-
assertEquals(Integer.valueOf(300), style.getHeight());
173-
}
174-
175161
@Test
176162
void findRelationshipStyle_ReturnsTheDefaultStyle_WhenPassedNull_ForLightColorScheme() {
177163
RelationshipStyle style = styles.findRelationshipStyle((Relationship) null);

structurizr-export/src/main/java/com/structurizr/export/dot/DOTExporter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
public class DOTExporter extends AbstractDiagramExporter {
1414

15+
private static final int DEFAULT_WIDTH = 450;
16+
private static final int DEFAULT_HEIGHT = 300;
1517
private static final String DEFAULT_FONT = "Arial";
1618

1719
private int clusterInternalMargin = 25;
@@ -209,6 +211,14 @@ protected void endDeploymentNodeBoundary(ModelView view, IndentingWriter writer)
209211
protected void writeElement(ModelView view, Element element, IndentingWriter writer) {
210212
ElementStyle elementStyle = view.getViewSet().getConfiguration().getStyles().findElementStyle(element);
211213

214+
if (elementStyle.getWidth() == null) {
215+
elementStyle.setWidth(DEFAULT_WIDTH);
216+
}
217+
218+
if (elementStyle.getHeight() == null) {
219+
elementStyle.setHeight(DEFAULT_HEIGHT);
220+
}
221+
212222
int nameFontSize = elementStyle.getFontSize() + 10;
213223
int metadataFontSize = elementStyle.getFontSize() - 5;
214224
int descriptionFontSize = elementStyle.getFontSize();
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.structurizr.export.plantuml;
2+
3+
import com.structurizr.export.IndentingWriter;
4+
import com.structurizr.view.Border;
5+
6+
import java.util.Base64;
7+
8+
import static java.lang.String.format;
9+
10+
class PlantUMLDeploymentNodeStyle extends PlantUMLStyle {
11+
12+
private final String background;
13+
private final String color;
14+
private final String stroke;
15+
private final int strokeWidth;
16+
private final String lineStyle;
17+
private final int fontSize;
18+
private final String icon;
19+
private final boolean shadow;
20+
private Integer width; // only used for the legend
21+
22+
PlantUMLDeploymentNodeStyle(String name, String background, String color, String stroke, int strokeWidth, Border border, int fontSize, String icon, boolean shadow) {
23+
super(name);
24+
25+
this.background = background;
26+
this.color = color;
27+
this.stroke = stroke;
28+
this.strokeWidth = strokeWidth;
29+
30+
switch (border) {
31+
case Dotted:
32+
this.lineStyle = (strokeWidth) + "-" + (strokeWidth);
33+
break;
34+
case Dashed:
35+
this.lineStyle = (strokeWidth * 5) + "-" + (strokeWidth * 5);
36+
break;
37+
default:
38+
this.lineStyle = "0";
39+
break;
40+
}
41+
42+
this.fontSize = fontSize;
43+
this.icon = icon;
44+
this.shadow = shadow;
45+
}
46+
47+
public int getFontSize() {
48+
return fontSize;
49+
}
50+
51+
public String getIcon() {
52+
return icon;
53+
}
54+
55+
public void setWidth(int width) {
56+
this.width = width;
57+
}
58+
59+
@Override
60+
String getClassSelector() {
61+
return "DeploymentNode-" + Base64.getEncoder().encodeToString(name.getBytes());
62+
}
63+
64+
@Override
65+
public boolean equals(Object o) {
66+
if (o == null || getClass() != o.getClass()) return false;
67+
PlantUMLDeploymentNodeStyle that = (PlantUMLDeploymentNodeStyle) o;
68+
return getClassSelector().equals(that.getClassSelector());
69+
}
70+
71+
@Override
72+
public String toString() {
73+
IndentingWriter writer = new IndentingWriter();
74+
writer.indent();
75+
writer.writeLine(format("// %s", name));
76+
writer.writeLine(format(".%s {", getClassSelector()));
77+
writer.indent();
78+
79+
writer.writeLine(String.format("BackgroundColor: %s;", background));
80+
writer.writeLine(String.format("LineColor: %s;", stroke));
81+
writer.writeLine(String.format("LineStyle: %s;", lineStyle));
82+
writer.writeLine(String.format("LineThickness: %s;", strokeWidth));
83+
writer.writeLine(String.format("FontColor: %s;", color));
84+
writer.writeLine(String.format("FontSize: %s;", fontSize));
85+
writer.writeLine("HorizontalAlignment: center;");
86+
writer.writeLine(String.format("Shadowing: %s;", shadow ? SHADOW_DISTANCE : 0));
87+
if (width != null) {
88+
writer.writeLine(String.format("MaximumWidth: %s;", width));
89+
}
90+
91+
writer.outdent();
92+
writer.writeLine("}");
93+
writer.outdent();
94+
writer.writeLine();
95+
96+
return writer.toString();
97+
}
98+
99+
}

0 commit comments

Comments
 (0)