Skip to content

Commit c3a3a64

Browse files
authored
refactor: simplify accessing features from cluster click event (#8137)
1 parent ec36f37 commit c3a3a64

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.vaadin.flow.component.html.Div;
1414
import com.vaadin.flow.component.html.NativeButton;
1515
import com.vaadin.flow.component.map.configuration.Coordinate;
16+
import com.vaadin.flow.component.map.configuration.Feature;
1617
import com.vaadin.flow.component.map.configuration.feature.MarkerFeature;
1718
import com.vaadin.flow.component.map.configuration.layer.FeatureLayer;
1819
import com.vaadin.flow.component.map.configuration.style.Icon;
@@ -46,10 +47,14 @@ public ClusterPage() {
4647
eventLog.getElement().getStyle().set("white-space", "pre");
4748

4849
map.addClusterClickListener(event -> {
49-
String features = event.getFeatures().stream()
50-
.map(info -> info.getFeature().getId()).sorted()
51-
.collect(Collectors.joining(", "));
52-
eventLog.add(new Div("cluster-click: " + features));
50+
String featureIds = event.getFeatures().stream().map(Feature::getId)
51+
.sorted().collect(Collectors.joining(", "));
52+
String layerId = event.getLayer().getId();
53+
String sourceId = event.getVectorSource().getId();
54+
String eventText = String.format(
55+
"cluster-click: features=[%s], layer=%s, source=%s",
56+
featureIds, layerId, sourceId);
57+
eventLog.add(new Div(eventText));
5358
});
5459

5560
map.addFeatureClickListener(event -> {
@@ -67,6 +72,8 @@ private void configureClustering(Map map) {
6772
layer.setClusterDistance(50);
6873
layer.setClusterMinDistance(50);
6974

75+
layer.getSource().setId("cluster-source");
76+
7077
// Cluster feature in some region
7178
MarkerFeature marker = new MarkerFeature(new Coordinate(0, 0));
7279
marker.setId("m1");

vaadin-map-flow-parent/vaadin-map-flow-integration-tests/src/main/java/com/vaadin/flow/component/map/demo/ClusterDemo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public ClusterDemo() {
3535
layer.addFeature(marker);
3636
}
3737

38+
map.addClusterClickListener(event -> {
39+
map.zoomToFit(event.getFeatures());
40+
});
41+
3842
add(map);
3943
}
4044
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ public void clickCluster_triggersClusterClickEvent() {
181181
// Click events are delayed by around 250ms, wait for event
182182
waitSeconds(1);
183183

184-
Assert.assertEquals("cluster-click: m1, m2, m3", eventLog.getText());
184+
Assert.assertEquals(
185+
"cluster-click: features=[m1, m2, m3], layer=feature-layer, source=cluster-source",
186+
eventLog.getText());
185187
}
186188

187189
@Test

vaadin-map-flow-parent/vaadin-map-flow/src/main/java/com/vaadin/flow/component/map/events/MapClusterClickEvent.java

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88
*/
99
package com.vaadin.flow.component.map.events;
1010

11-
import java.util.ArrayList;
12-
import java.util.Collections;
1311
import java.util.List;
1412

1513
import com.vaadin.flow.component.ComponentEvent;
1614
import com.vaadin.flow.component.DomEvent;
1715
import com.vaadin.flow.component.EventData;
1816
import com.vaadin.flow.component.map.Map;
1917
import com.vaadin.flow.component.map.MapBase;
18+
import com.vaadin.flow.component.map.configuration.Feature;
19+
import com.vaadin.flow.component.map.configuration.layer.VectorLayer;
20+
import com.vaadin.flow.component.map.configuration.source.VectorSource;
21+
import com.vaadin.flow.internal.JacksonUtils;
2022

23+
import tools.jackson.databind.JsonNode;
2124
import tools.jackson.databind.node.ArrayNode;
2225

2326
/**
@@ -26,7 +29,9 @@
2629
@DomEvent("map-cluster-click")
2730
public class MapClusterClickEvent extends ComponentEvent<MapBase> {
2831

29-
private final List<FeatureEventDetails> features;
32+
private final List<Feature> features;
33+
private final VectorLayer layer;
34+
private final VectorSource vectorSource;
3035
private final MouseEventDetails details;
3136

3237
public MapClusterClickEvent(Map source, boolean fromClient,
@@ -41,15 +46,19 @@ public MapClusterClickEvent(Map source, boolean fromClient,
4146
@EventData("event.detail.originalEvent.button") int button) {
4247
super(source, fromClient);
4348

44-
List<FeatureEventDetails> features = new ArrayList<>();
45-
for (int i = 0; i < featureIds.size(); i++) {
46-
String featureId = featureIds.get(i).asString();
47-
FeatureEventDetails featureEventDetails = MapEventUtil
48-
.getFeatureEventDetails(source.getRawConfiguration(),
49-
layerId, featureId);
50-
features.add(featureEventDetails);
51-
}
52-
this.features = Collections.unmodifiableList(features);
49+
layer = source.getRawConfiguration().getLayers().stream()
50+
.filter(l -> l instanceof VectorLayer && l.getId() != null
51+
&& l.getId().equals(layerId))
52+
.map(l -> (VectorLayer) l).findFirst()
53+
.orElseThrow(() -> new IllegalStateException(
54+
"No vector layer with id " + layerId));
55+
vectorSource = (VectorSource) layer.getSource();
56+
57+
List<String> featureIdList = JacksonUtils.stream(featureIds)
58+
.map(JsonNode::asString).toList();
59+
features = vectorSource.getFeatures().stream()
60+
.filter(feature -> featureIdList.contains(feature.getId()))
61+
.toList();
5362

5463
details = new MouseEventDetails();
5564
details.setAbsoluteX(pageX);
@@ -66,10 +75,28 @@ public MapClusterClickEvent(Map source, boolean fromClient,
6675
*
6776
* @return the list of features in the cluster
6877
*/
69-
public List<FeatureEventDetails> getFeatures() {
78+
public List<Feature> getFeatures() {
7079
return features;
7180
}
7281

82+
/**
83+
* Gets the layer that contains the cluster's features.
84+
*
85+
* @return the layer
86+
*/
87+
public VectorLayer getLayer() {
88+
return layer;
89+
}
90+
91+
/**
92+
* Gets the source that contains the cluster's features.
93+
*
94+
* @return the source
95+
*/
96+
public VectorSource getVectorSource() {
97+
return vectorSource;
98+
}
99+
73100
/**
74101
* Gets the click's mouse event details.
75102
*

0 commit comments

Comments
 (0)