Skip to content

Commit addd4ca

Browse files
authored
feat: customize marker label styles (#4647)
* feat: customize marker label style * rename Text to TextStyle * add null check to test setup * add Javadoc for Style.textStyle property * use int for pixel values
1 parent 7514bdf commit addd4ca

14 files changed

Lines changed: 730 additions & 20 deletions

File tree

‎vaadin-map-flow-parent/vaadin-map-flow-integration-tests/src/main/java/com/vaadin/flow/component/map/FeatureLabelPage.java‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.vaadin.flow.component.html.NativeButton;
55
import com.vaadin.flow.component.map.configuration.Coordinate;
66
import com.vaadin.flow.component.map.configuration.feature.MarkerFeature;
7+
import com.vaadin.flow.component.map.configuration.style.TextStyle;
78
import com.vaadin.flow.router.Route;
89

910
@Route("vaadin-map/feature-label")
@@ -43,7 +44,44 @@ public FeatureLabelPage() {
4344
});
4445
removeLabelText.setId("remove-label-text");
4546

47+
NativeButton setLabelStyle = new NativeButton("Set custom label style",
48+
e -> {
49+
marker1.getStyle().setTextStyle(createCustomLabelStyle());
50+
});
51+
setLabelStyle.setId("set-label-style");
52+
53+
NativeButton updateLabelStyle = new NativeButton(
54+
"Update custom label style", e -> {
55+
if (marker1.getStyle().getTextStyle() != null) {
56+
marker1.getStyle().getTextStyle()
57+
.setFont("15px sans-serif");
58+
}
59+
});
60+
updateLabelStyle.setId("update-label-style");
61+
62+
NativeButton removeLabelStyle = new NativeButton(
63+
"Remove custom label style", e -> {
64+
marker1.getStyle().setTextStyle(null);
65+
});
66+
removeLabelStyle.setId("remove-label-style");
67+
4668
add(map);
47-
add(new Div(updateLabelText, removeLabelText));
69+
add(new Div(updateLabelText, removeLabelText, setLabelStyle,
70+
updateLabelStyle, removeLabelStyle));
71+
}
72+
73+
private TextStyle createCustomLabelStyle() {
74+
TextStyle textStyle = new TextStyle();
75+
textStyle.setFont("bold 13px monospace");
76+
textStyle.setOffset(30, 0);
77+
textStyle.setTextAlign(TextStyle.TextAlign.LEFT);
78+
textStyle.setTextBaseline(TextStyle.TextBaseline.BOTTOM);
79+
textStyle.setFill("#fff");
80+
textStyle.setStroke("#000", 5);
81+
textStyle.setBackgroundFill("#1F6B75");
82+
textStyle.setBackgroundStroke("#fff", 2);
83+
textStyle.setPadding(3);
84+
85+
return textStyle;
4886
}
4987
}

‎vaadin-map-flow-parent/vaadin-map-flow-integration-tests/src/test/java/com/vaadin/flow/components/map/FeatureLabelIT.java‎

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,25 @@
1111
@TestPath("vaadin-map/feature-label")
1212
public class FeatureLabelIT extends AbstractComponentIT {
1313
private MapElement map;
14-
private TestBenchElement updateLabelText;
15-
private TestBenchElement removeLabelText;
14+
private TestBenchElement updateLabelButton;
15+
private TestBenchElement removeLabelButton;
16+
private TestBenchElement setLabelStyleButton;
17+
private TestBenchElement updateLabelStyleButton;
18+
private TestBenchElement removeLabelStyleButton;
1619

1720
@Before
1821
public void init() {
1922
open();
2023
map = $(MapElement.class).waitForFirst();
21-
updateLabelText = $(TestBenchElement.class).id("update-label-text");
22-
removeLabelText = $(TestBenchElement.class).id("remove-label-text");
24+
updateLabelButton = $(TestBenchElement.class).id("update-label-text");
25+
removeLabelButton = $(TestBenchElement.class).id("remove-label-text");
26+
setLabelStyleButton = $(TestBenchElement.class).id("set-label-style");
27+
updateLabelStyleButton = $(TestBenchElement.class)
28+
.id("update-label-style");
29+
removeLabelStyleButton = $(TestBenchElement.class)
30+
.id("remove-label-style");
31+
32+
trackRenderCount();
2333
}
2434

2535
@Test
@@ -46,7 +56,7 @@ public void initialLabels() {
4656

4757
@Test
4858
public void updateLabels() {
49-
updateLabelText.click();
59+
updateLabelButton.click();
5060

5161
Assert.assertEquals("Updated label 1",
5262
getMarkerTextStyle("marker1").getText());
@@ -58,13 +68,64 @@ public void updateLabels() {
5868

5969
@Test
6070
public void removeLabels() {
61-
removeLabelText.click();
71+
removeLabelButton.click();
6272

6373
Assert.assertNull(getMarkerTextStyle("marker1").getText());
6474
Assert.assertNull(getMarkerTextStyle("marker2").getText());
6575
Assert.assertNull(getMarkerTextStyle("marker3").getText());
6676
}
6777

78+
@Test
79+
public void setSetLabelStyle() {
80+
setLabelStyleButton.click();
81+
82+
MapElement.TextReference text = getMarkerTextStyle("marker1");
83+
Assert.assertEquals("Marker label 1", text.getText());
84+
Assert.assertEquals("bold 13px monospace", text.getFont());
85+
Assert.assertEquals(30, text.getOffsetX());
86+
Assert.assertEquals(0, text.getOffsetY());
87+
Assert.assertEquals("left", text.getTextAlign());
88+
Assert.assertEquals("bottom", text.getTextBaseline());
89+
Assert.assertEquals("#fff", text.getFill().getColor());
90+
Assert.assertEquals("#000", text.getStroke().getColor());
91+
Assert.assertEquals(5, text.getStroke().getWidth());
92+
Assert.assertEquals("#1F6B75", text.getBackgroundFill().getColor());
93+
Assert.assertEquals("#fff", text.getBackgroundStroke().getColor());
94+
Assert.assertEquals(2, text.getBackgroundStroke().getWidth());
95+
96+
waitUntil(driver -> getRenderCount() == 1);
97+
}
98+
99+
@Test
100+
public void updateLabelStyle() {
101+
setLabelStyleButton.click();
102+
103+
waitUntil(driver -> getRenderCount() == 1);
104+
105+
updateLabelStyleButton.click();
106+
107+
MapElement.TextReference text = getMarkerTextStyle("marker1");
108+
Assert.assertEquals("Marker label 1", text.getText());
109+
Assert.assertEquals("15px sans-serif", text.getFont());
110+
111+
waitUntil(driver -> getRenderCount() == 2);
112+
}
113+
114+
@Test
115+
public void removeLabelStyle() {
116+
setLabelStyleButton.click();
117+
118+
waitUntil(driver -> getRenderCount() == 1);
119+
120+
removeLabelStyleButton.click();
121+
122+
MapElement.TextReference text = getMarkerTextStyle("marker1");
123+
Assert.assertEquals("Marker label 1", text.getText());
124+
Assert.assertEquals("13px sans-serif", text.getFont());
125+
126+
waitUntil(driver -> getRenderCount() == 2);
127+
}
128+
68129
private MapElement.TextReference getMarkerTextStyle(String markerId) {
69130
MapElement.MapReference mapReference = map.getMapReference();
70131
MapElement.VectorSourceReference vectorSource = mapReference.getLayers()
@@ -73,4 +134,16 @@ private MapElement.TextReference getMarkerTextStyle(String markerId) {
73134
.getFeature(markerId);
74135
return feature.getStyle().getText();
75136
}
137+
138+
private long getRenderCount() {
139+
return (long) getCommandExecutor().executeScript(
140+
"const map = arguments[0];" + "return map.__renderCount;", map);
141+
}
142+
143+
private void trackRenderCount() {
144+
getCommandExecutor().executeScript("const map = arguments[0];"
145+
+ "map.__renderCount = 0;"
146+
+ "map.configuration.on('rendercomplete', () => { map.__renderCount = map.__renderCount + 1 });",
147+
map);
148+
}
76149
}

‎vaadin-map-flow-parent/vaadin-map-flow/src/main/java/com/vaadin/flow/component/map/configuration/AbstractConfigurationObject.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.beans.PropertyChangeSupport;
1919
import java.io.Serializable;
2020
import java.util.LinkedHashSet;
21+
import java.util.Objects;
2122
import java.util.Set;
2223
import java.util.UUID;
2324
import java.util.function.Consumer;
@@ -161,6 +162,8 @@ protected void deepMarkAsDirty() {
161162
* resolved on the client-side.
162163
*/
163164
protected void addChild(AbstractConfigurationObject configurationObject) {
165+
Objects.requireNonNull(configurationObject,
166+
"Child configuration object must not be null");
164167
children.add(configurationObject);
165168
configurationObject.addPropertyChangeListener(this::notifyChange);
166169
markAsDirty();
@@ -172,6 +175,18 @@ protected void addChild(AbstractConfigurationObject configurationObject) {
172175
configurationObject.deepMarkAsDirty();
173176
}
174177

178+
/**
179+
* Convenience wrapper for {@link #addChild(AbstractConfigurationObject)}
180+
* that allows {@code configurationObject} to be a null reference.
181+
*/
182+
protected void addNullableChild(
183+
AbstractConfigurationObject configurationObject) {
184+
if (configurationObject == null) {
185+
return;
186+
}
187+
addChild(configurationObject);
188+
}
189+
175190
/**
176191
* Removes a nested object reference from tracking. This removes the object
177192
* from the internal set used for collecting changes, and removes the change

‎vaadin-map-flow-parent/vaadin-map-flow/src/main/java/com/vaadin/flow/component/map/configuration/Constants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Constants {
3131
public static final String OL_STYLE_FILL = "ol/style/Fill";
3232
public static final String OL_STYLE_STROKE = "ol/style/Stroke";
3333
public static final String OL_STYLE_STYLE = "ol/style/Style";
34+
public static final String OL_STYLE_TEXT = "ol/style/Text";
3435

3536
public static final String OL_MAP = "ol/Map";
3637
public static final String OL_VIEW = "ol/View";

‎vaadin-map-flow-parent/vaadin-map-flow/src/main/java/com/vaadin/flow/component/map/configuration/style/Fill.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ public class Fill extends AbstractConfigurationObject {
1515

1616
private String color;
1717

18+
public Fill() {
19+
}
20+
21+
public Fill(String color) {
22+
this.color = color;
23+
}
24+
1825
@Override
1926
public String getType() {
2027
return Constants.OL_STYLE_FILL;

‎vaadin-map-flow-parent/vaadin-map-flow/src/main/java/com/vaadin/flow/component/map/configuration/style/Stroke.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ public class Stroke extends AbstractConfigurationObject {
1616
private String color;
1717
private double width;
1818

19+
public Stroke() {
20+
}
21+
22+
public Stroke(String color, double width) {
23+
this.color = color;
24+
this.width = width;
25+
}
26+
1927
@Override
2028
public String getType() {
2129
return Constants.OL_STYLE_STROKE;

‎vaadin-map-flow-parent/vaadin-map-flow/src/main/java/com/vaadin/flow/component/map/configuration/style/Style.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
package com.vaadin.flow.component.map.configuration.style;
1010

11+
import com.fasterxml.jackson.annotation.JsonGetter;
1112
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
1213
import com.fasterxml.jackson.annotation.JsonIdentityReference;
1314
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@@ -23,6 +24,7 @@ public class Style extends AbstractConfigurationObject {
2324
private ImageStyle image;
2425
private Fill fill;
2526
private Stroke stroke;
27+
private TextStyle textStyle;
2628

2729
@Override
2830
public String getType() {
@@ -64,4 +66,32 @@ public void setStroke(Stroke stroke) {
6466
this.stroke = stroke;
6567
addChild(stroke);
6668
}
69+
70+
/**
71+
* The text style used for rendering texts, such as feature labels. This is
72+
* {@code null} by default, which means that a default text style will be
73+
* used instead.
74+
*
75+
* @return the text style, or {@code null} if no text style is defined
76+
*/
77+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
78+
@JsonIdentityReference(alwaysAsId = true)
79+
@JsonGetter("text")
80+
public TextStyle getTextStyle() {
81+
return textStyle;
82+
}
83+
84+
/**
85+
* Sets the text style to use for rendering texts, such as feature labels.
86+
* Set this to {@code null} to clear the text style and use a default text
87+
* style instead.
88+
*
89+
* @param textStyle
90+
* the new text style, or {@code null}
91+
*/
92+
public void setTextStyle(TextStyle textStyle) {
93+
removeChild(this.textStyle);
94+
this.textStyle = textStyle;
95+
addNullableChild(textStyle);
96+
}
6797
}

0 commit comments

Comments
 (0)