]> git.argeo.org Git - gpl/argeo-suite.git/blob - geo/ux/OpenLayersMapPart.java
Prepare next development cycle
[gpl/argeo-suite.git] / geo / ux / OpenLayersMapPart.java
1 package org.argeo.app.geo.ux;
2
3 import java.util.Map;
4 import java.util.function.Consumer;
5 import java.util.function.Function;
6
7 import org.argeo.app.geo.ux.MapPart.FeaturePopupEvent;
8 import org.argeo.app.geo.ux.MapPart.FeatureSelectedEvent;
9 import org.argeo.app.geo.ux.MapPart.FeatureSingleClickEvent;
10 import org.argeo.app.ol.AbstractOlObject;
11 import org.argeo.app.ol.Layer;
12 import org.argeo.app.ol.OlMap;
13 import org.argeo.app.ol.TileLayer;
14 import org.argeo.app.ol.VectorLayer;
15 import org.argeo.app.ux.js.JsClient;
16 import org.locationtech.jts.geom.Envelope;
17
18 /**
19 * A wrapper around an OpenLayers map, adding specific features, such as SLD
20 * styling.
21 */
22 public class OpenLayersMapPart extends AbstractGeoJsObject {
23 private final String mapPartName;
24
25 public OpenLayersMapPart(JsClient jsClient, String mapPartName) {
26 super(mapPartName);
27 this.mapPartName = mapPartName;
28 create(jsClient, mapPartName);
29 }
30
31 public OlMap getMap() {
32 return new OlMap(getJsClient(), getReference() + ".getMap()");
33 }
34
35 public void setSld(String xml) {
36 executeMethod(getMethodName(), JsClient.escapeQuotes(xml));
37 }
38
39 public void setCenter(Double lat, Double lon) {
40 executeMethod(getMethodName(), lat, lon);
41 }
42
43 public void fit(double[] extent, Map<String, Object> options) {
44 executeMethod(getMethodName(), extent, options);
45 }
46
47 public void fit(Envelope extent, Map<String, Object> options) {
48 fit(new double[] { extent.getMinX(), extent.getMinY(), extent.getMaxX(), extent.getMaxY() }, options);
49 }
50
51 public void applyStyle(String layerName, String styledLayerName) {
52 executeMethod(getMethodName(), layerName, styledLayerName);
53 }
54
55 public Layer getLayer(String name) {
56 // TODO deal with not found
57 String reference = getReference() + ".getLayerByName('" + name + "')";
58 if (getJsClient().isInstanceOf(reference, AbstractOlObject.getJsClassName(VectorLayer.class))) {
59 return new VectorLayer(getJsClient(), reference);
60 } else if (getJsClient().isInstanceOf(reference, AbstractOlObject.getJsClassName(TileLayer.class))) {
61 return new TileLayer(getJsClient(), reference);
62 } else {
63 return new Layer(getJsClient(), reference);
64 }
65 }
66
67 public String getMapPartName() {
68 return mapPartName;
69 }
70
71 public void selectFeatures(String layerName, Object... ids) {
72 executeMethod(getMethodName(), layerName, (Object[]) ids);
73 }
74
75 /*
76 * CALLBACKS
77 */
78 public void onFeatureSelected(Consumer<FeatureSelectedEvent> toDo) {
79 addCallback("FeatureSelected", (arr) -> {
80 toDo.accept(new FeatureSelectedEvent((String) arr[0]));
81 return null;
82 });
83 }
84
85 public void onFeatureSingleClick(Consumer<FeatureSingleClickEvent> toDo) {
86 addCallback("FeatureSingleClick", (arr) -> {
87 toDo.accept(new FeatureSingleClickEvent((String) arr[0]));
88 return null;
89 });
90 }
91
92 public void onFeaturePopup(Function<FeaturePopupEvent, String> toDo) {
93 addCallback("FeaturePopup", (arr) -> {
94 return toDo.apply(new FeaturePopupEvent((String) arr[0]));
95 });
96 }
97
98 protected void addCallback(String suffix, Function<Object[], Object> toDo) {
99 getJsClient().getReadyStage().thenAccept((ready) -> {
100 String functionName = getJsClient().createJsFunction(getMapPartName() + "__on" + suffix, toDo);
101 getJsClient().execute(getJsReference() + ".callbacks['on" + suffix + "']=" + functionName + ";");
102 executeMethod("enable" + suffix);
103 });
104 }
105
106 }