Skip to content
This repository was archived by the owner on Mar 28, 2026. It is now read-only.

Commit 190c631

Browse files
Adds some additional functionality for getting and finding element/relationship styles.
1 parent aaaeacd commit 190c631

3 files changed

Lines changed: 94 additions & 0 deletions

File tree

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.15.1 (23rd September 2022)
4+
5+
- Adds some additional functionality for getting and finding element/relationship styles.
6+
37
## 1.15.0 (13th September 2022)
48

59
- Adds documentation section filenames into the model.

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ public RelationshipStyle addRelationshipStyle(String tag) {
8383
return relationshipStyle;
8484
}
8585

86+
/**
87+
* Gets the element style that has been defined (in this workspace) for the given tag.
88+
*
89+
* @param tag the tag (a String)
90+
* @return an ElementStyle instance, or null if no element style has been defined in this workspace
91+
*/
92+
public ElementStyle getElementStyle(String tag) {
93+
if (StringUtils.isNullOrEmpty(tag)) {
94+
throw new IllegalArgumentException("A tag must be specified.");
95+
}
96+
97+
return elements.stream().filter(es -> es.getTag().equals(tag)).findFirst().orElse(null);
98+
}
99+
86100
/**
87101
* Finds the element style for the given tag. This method creates an empty style,
88102
* and copies properties from any element styles (from the workspace and any themes) for the given tag.
@@ -120,6 +134,20 @@ public ElementStyle findElementStyle(String tag) {
120134
}
121135
}
122136

137+
/**
138+
* Gets the relationship style that has been defined (in this workspace) for the given tag.
139+
*
140+
* @param tag the tag (a String)
141+
* @return an RelationshipStyle instance, or null if no relationship style has been defined in this workspace
142+
*/
143+
public RelationshipStyle getRelationshipStyle(String tag) {
144+
if (StringUtils.isNullOrEmpty(tag)) {
145+
throw new IllegalArgumentException("A tag must be specified.");
146+
}
147+
148+
return relationships.stream().filter(rs -> rs.getTag().equals(tag)).findFirst().orElse(null);
149+
}
150+
123151
/**
124152
* Finds the relationship style for the given tag. This method creates an empty style,
125153
* and copies properties from any relationship styles (from the workspace and any themes) for the given tag.

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,66 @@ void clearRelationshipStyles_RemovesAllRelationshipStyles() {
333333
assertEquals(0, styles.getRelationships().size());
334334
}
335335

336+
@Test
337+
void getElementStyle_ThrowsAnException_WhenGivenNoTag() {
338+
try {
339+
styles.getElementStyle(null);
340+
fail();
341+
} catch (IllegalArgumentException iae) {
342+
assertEquals("A tag must be specified.", iae.getMessage());
343+
}
344+
345+
try {
346+
styles.getElementStyle(" ");
347+
fail();
348+
} catch (IllegalArgumentException iae) {
349+
assertEquals("A tag must be specified.", iae.getMessage());
350+
}
351+
}
352+
353+
@Test
354+
void getElementStyle_ReturnsNull_WhenThereIsNoStyleForTheGivenTag() {
355+
ElementStyle style = styles.getElementStyle("Tag");
356+
assertNull(style);
357+
}
358+
359+
@Test
360+
void getElementStyle_ReturnsTheElementStyle_WhenThereIsAStyleForTheGivenTag() {
361+
styles.addElementStyle("Tag").background("#ffffff");
362+
363+
ElementStyle style = styles.getElementStyle("Tag");
364+
assertEquals("#ffffff", style.getBackground());
365+
}
366+
367+
@Test
368+
void getRelationshipStyle_ThrowsAnException_WhenGivenNoTag() {
369+
try {
370+
styles.getRelationshipStyle(null);
371+
fail();
372+
} catch (IllegalArgumentException iae) {
373+
assertEquals("A tag must be specified.", iae.getMessage());
374+
}
375+
376+
try {
377+
styles.getRelationshipStyle(" ");
378+
fail();
379+
} catch (IllegalArgumentException iae) {
380+
assertEquals("A tag must be specified.", iae.getMessage());
381+
}
382+
}
383+
384+
@Test
385+
void getRelationshipStyle_ReturnsNull_WhenThereIsNoStyleForTheGivenTag() {
386+
RelationshipStyle style = styles.getRelationshipStyle("Tag");
387+
assertNull(style);
388+
}
389+
390+
@Test
391+
void getRelationshipStyle_ReturnsTheRelationshipStyle_WhenThereIsAStyleForTheGivenTag() {
392+
styles.addRelationshipStyle("Tag").color("#ffffff");
393+
394+
RelationshipStyle style = styles.getRelationshipStyle("Tag");
395+
assertEquals("#ffffff", style.getColor());
396+
}
397+
336398
}

0 commit comments

Comments
 (0)