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