|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Google Maps Addon |
| 4 | + * %% |
| 5 | + * Copyright (C) 2020 - 2024 Flowing Code |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | + |
| 21 | +package com.flowingcode.vaadin.addons.googlemaps; |
| 22 | + |
| 23 | +import com.vaadin.flow.component.ClickEvent; |
| 24 | +import com.vaadin.flow.component.Component; |
| 25 | +import com.vaadin.flow.component.ComponentEventListener; |
| 26 | +import com.vaadin.flow.component.DomEvent; |
| 27 | +import com.vaadin.flow.component.EventData; |
| 28 | +import com.vaadin.flow.component.Tag; |
| 29 | +import com.vaadin.flow.component.dependency.JsModule; |
| 30 | +import com.vaadin.flow.component.dependency.NpmPackage; |
| 31 | +import com.vaadin.flow.shared.Registration; |
| 32 | +import elemental.json.Json; |
| 33 | +import elemental.json.JsonArray; |
| 34 | +import elemental.json.JsonObject; |
| 35 | +import elemental.json.JsonValue; |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +@SuppressWarnings("serial") |
| 39 | +@Tag("google-map-poly") |
| 40 | +@JsModule("@flowingcode/google-map/google-map-poly.js") |
| 41 | +@JsModule("@flowingcode/google-map/google-map-point.js") |
| 42 | +@NpmPackage(value = "@flowingcode/google-map", version = "3.6.1") |
| 43 | +@NpmPackage(value = "@googlemaps/markerclusterer", version = "2.0.8") |
| 44 | +public abstract class GoogleMapPoly extends Component { |
| 45 | + |
| 46 | + private static final double DEFAULT_FILL_OPACITY = 0.5d; |
| 47 | + private static final String DEFAULT_FILL_COLOR = "blue"; |
| 48 | + |
| 49 | + public enum StrokePosition { |
| 50 | + CENTER, |
| 51 | + INSIDE, |
| 52 | + OUTSIDE |
| 53 | + } |
| 54 | + |
| 55 | + public GoogleMapPoly(List<GoogleMapPoint> points) { |
| 56 | + getElement().removeProperty("draggable"); |
| 57 | + setFillColor(DEFAULT_FILL_COLOR); |
| 58 | + setFillOpacity(DEFAULT_FILL_OPACITY); |
| 59 | + setPoints(points); |
| 60 | + } |
| 61 | + |
| 62 | + public void setFillOpacity(double opacity) { |
| 63 | + this.getElement().setProperty("fillOpacity", opacity); |
| 64 | + } |
| 65 | + |
| 66 | + public double getFillOpacity() { |
| 67 | + return this.getElement().getProperty("fillOpacity", 1d); |
| 68 | + } |
| 69 | + |
| 70 | + public void setStrokeOpacity(double opacity) { |
| 71 | + this.getElement().setProperty("strokeOpacity", opacity); |
| 72 | + } |
| 73 | + |
| 74 | + public double getStrokeOpacity() { |
| 75 | + return this.getElement().getProperty("strokeOpacity", 1d); |
| 76 | + } |
| 77 | + |
| 78 | + public void setStrokePosition(StrokePosition position) { |
| 79 | + this.getElement().setProperty("strokePosition", position.name().toLowerCase()); |
| 80 | + } |
| 81 | + |
| 82 | + public StrokePosition getStrokePosition() { |
| 83 | + return StrokePosition.valueOf(this.getElement().getProperty("strokePosition").toUpperCase()); |
| 84 | + } |
| 85 | + |
| 86 | + public void setStrokeWeight(double weight) { |
| 87 | + this.getElement().setProperty("strokeWeight", weight); |
| 88 | + } |
| 89 | + |
| 90 | + public double getStrokeWeight() { |
| 91 | + return this.getElement().getProperty("strokeWeight", 1d); |
| 92 | + } |
| 93 | + |
| 94 | + public void setZIndex(double zindex) { |
| 95 | + this.getElement().setProperty("zIndex", zindex); |
| 96 | + } |
| 97 | + |
| 98 | + public double getZIndex() { |
| 99 | + return this.getElement().getProperty("zIndex", 1d); |
| 100 | + } |
| 101 | + |
| 102 | + public void setFillColor(String string) { |
| 103 | + this.getElement().setProperty("fillColor", string); |
| 104 | + } |
| 105 | + |
| 106 | + public String getFillColor() { |
| 107 | + return this.getElement().getProperty("fillColor"); |
| 108 | + } |
| 109 | + |
| 110 | + public void setStrokeColor(String string) { |
| 111 | + this.getElement().setProperty("strokeColor", string); |
| 112 | + } |
| 113 | + |
| 114 | + public String getStrokeColor() { |
| 115 | + return this.getElement().getProperty("strokeColor"); |
| 116 | + } |
| 117 | + |
| 118 | + public void setClosed(boolean b) { |
| 119 | + this.getElement().setProperty("closed", b); |
| 120 | + } |
| 121 | + |
| 122 | + public boolean isClosed() { |
| 123 | + return this.getElement().getProperty("closed", false); |
| 124 | + } |
| 125 | + |
| 126 | + public void setGeodesic(boolean b) { |
| 127 | + this.getElement().setProperty("geodesic", b); |
| 128 | + } |
| 129 | + |
| 130 | + public boolean isGeodesic() { |
| 131 | + return this.getElement().getProperty("geodesic", false); |
| 132 | + } |
| 133 | + |
| 134 | + public void setPoints(Iterable<GoogleMapPoint> points) { |
| 135 | + points.forEach(point -> this.getElement().appendChild(point.getElement())); |
| 136 | + } |
| 137 | + |
| 138 | + public GoogleMapPoint addPoint(LatLon position) { |
| 139 | + GoogleMapPoint point = new GoogleMapPoint(position.getLat(), position.getLon()); |
| 140 | + this.getElement().appendChild(point.getElement()); |
| 141 | + return point; |
| 142 | + } |
| 143 | + |
| 144 | + public void addPoint(GoogleMapPoint point) { |
| 145 | + this.getElement().appendChild(point.getElement()); |
| 146 | + } |
| 147 | + |
| 148 | + @SuppressWarnings("squid:S3242") |
| 149 | + public void removePoint(GoogleMapPoint point) { |
| 150 | + this.getElement().removeChild(point.getElement()); |
| 151 | + } |
| 152 | + |
| 153 | + @DomEvent("google-map-poly-click") |
| 154 | + public static class GoogleMapPolyClickEvent extends ClickEvent<GoogleMapPolygon> { |
| 155 | + |
| 156 | + private final double lat; |
| 157 | + private final double lon; |
| 158 | + |
| 159 | + public GoogleMapPolyClickEvent( |
| 160 | + GoogleMapPoly source, |
| 161 | + boolean fromClient, |
| 162 | + @EventData(value = "event.detail.latLng") JsonValue latLng) { |
| 163 | + super(source); |
| 164 | + this.lat = ((JsonObject) latLng).getNumber("lat"); |
| 165 | + this.lon = ((JsonObject) latLng).getNumber("lng"); |
| 166 | + } |
| 167 | + |
| 168 | + public double getLatitude() { |
| 169 | + return this.lat; |
| 170 | + } |
| 171 | + |
| 172 | + public double getLongitude() { |
| 173 | + return this.lon; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + public Registration addClickListener( |
| 178 | + ComponentEventListener<GoogleMapPolyClickEvent> listener) { |
| 179 | + this.getElement().setProperty("clickable", true); |
| 180 | + this.getElement().setProperty("clickEvents", true); |
| 181 | + return addListener(GoogleMapPolyClickEvent.class, listener); |
| 182 | + } |
| 183 | + |
| 184 | + public void setIcons(Icon... icons) { |
| 185 | + JsonArray jsonArray = Json.createArray(); |
| 186 | + for (int i = 0; i < icons.length; i++) { |
| 187 | + jsonArray.set(i, icons[i].getJson()); |
| 188 | + } |
| 189 | + this.getElement().setPropertyJson("icons", jsonArray); |
| 190 | + } |
| 191 | +} |
0 commit comments