|
| 1 | +package io.github.makbn.jlmap.layer; |
| 2 | + |
| 3 | +import io.github.makbn.jlmap.JLMapCallbackHandler; |
| 4 | +import io.github.makbn.jlmap.exception.JLException; |
| 5 | +import io.github.makbn.jlmap.geojson.JLGeoJsonContent; |
| 6 | +import io.github.makbn.jlmap.geojson.JLGeoJsonFile; |
| 7 | +import io.github.makbn.jlmap.geojson.JLGeoJsonObject; |
| 8 | +import io.github.makbn.jlmap.geojson.JLGeoJsonURL; |
| 9 | +import io.github.makbn.jlmap.layer.leaflet.LeafletGeoJsonLayerInt; |
| 10 | +import javafx.scene.web.WebEngine; |
| 11 | +import lombok.NonNull; |
| 12 | +import netscape.javascript.JSObject; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.util.UUID; |
| 16 | + |
| 17 | +/** |
| 18 | + * Represents the GeoJson (other) layer on Leaflet map. |
| 19 | + * @author Mehdi Akbarian Rastaghi (@makbn) |
| 20 | + */ |
| 21 | +public class JLGeoJsonLayer extends JLLayer implements LeafletGeoJsonLayerInt { |
| 22 | + private static final String MEMBER_PREFIX = "geoJson"; |
| 23 | + private static final String WINDOW_OBJ = "window"; |
| 24 | + JLGeoJsonURL fromUrl; |
| 25 | + JLGeoJsonFile fromFile; |
| 26 | + JLGeoJsonContent fromContent; |
| 27 | + JSObject window; |
| 28 | + |
| 29 | + public JLGeoJsonLayer(WebEngine engine, JLMapCallbackHandler callbackHandler) { |
| 30 | + super(engine, callbackHandler); |
| 31 | + this.fromUrl = new JLGeoJsonURL(); |
| 32 | + this.fromFile = new JLGeoJsonFile(); |
| 33 | + this.fromContent = new JLGeoJsonContent(); |
| 34 | + this.window = (JSObject) engine.executeScript(WINDOW_OBJ); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public JLGeoJsonObject addFromFile(@NonNull File file) throws JLException { |
| 39 | + String json = fromFile.load(file); |
| 40 | + return addGeoJson(json); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public JLGeoJsonObject addFromUrl(@NonNull String url) throws JLException { |
| 45 | + String json = fromUrl.load(url); |
| 46 | + return addGeoJson(json); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public JLGeoJsonObject addFromContent(@NonNull String content) throws JLException { |
| 51 | + String json = fromContent.load(content); |
| 52 | + return addGeoJson(json); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean removeGeoJson(@NonNull JLGeoJsonObject object) { |
| 57 | + return Boolean.parseBoolean(engine.executeScript(String.format("removeGeoJson(%d)", object.getId())).toString()); |
| 58 | + } |
| 59 | + |
| 60 | + private JLGeoJsonObject addGeoJson(String jlGeoJsonObject) { |
| 61 | + try { |
| 62 | + String identifier = MEMBER_PREFIX + UUID.randomUUID(); |
| 63 | + this.window.setMember(identifier, jlGeoJsonObject); |
| 64 | + String returnedId = engine.executeScript(String.format("addGeoJson(\"%s\")", identifier)).toString(); |
| 65 | + |
| 66 | + return JLGeoJsonObject.builder() |
| 67 | + .id(Integer.parseInt(returnedId)) |
| 68 | + .geoJsonContent(jlGeoJsonObject) |
| 69 | + .build(); |
| 70 | + } catch (Exception e) { |
| 71 | + throw new JLException(e); |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | +} |
0 commit comments