Skip to content

Commit f26b711

Browse files
paodbjavier-godoy
authored andcommitted
feat: add support for adding custom control buttons
Close #115
1 parent d8573b6 commit f26b711

File tree

6 files changed

+193
-5
lines changed

6 files changed

+193
-5
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
/**
24+
* Enum representing supported control positions for map control buttons.
25+
*/
26+
public enum ControlPosition {
27+
28+
/**
29+
* Indicates that the control should be placed along the top center of the map.
30+
*/
31+
TOP_CENTER,
32+
33+
/**
34+
* Indicates that the control should be placed along the top left of the map, with any
35+
* sub-elements of the control "flowing" towards the top center.
36+
*/
37+
TOP_LEFT,
38+
39+
/**
40+
* Indicates that the control should be placed along the top right of the map, with any
41+
* sub-elements of the control "flowing" towards the top center.
42+
*/
43+
TOP_RIGHT,
44+
45+
/**
46+
* Indicates that the control should be placed along the top left of the map, but below any
47+
* TOP_LEFT elements.
48+
*/
49+
LEFT_TOP,
50+
51+
/**
52+
* Indicates that the control should be placed along the top right of the map, but below any
53+
* TOP_RIGHT elements.
54+
*/
55+
RIGHT_TOP,
56+
57+
/**
58+
* Indicates that the control should be placed along the left side of the map, centered between
59+
* the TOP_LEFT and BOTTOM_LEFT positions
60+
*/
61+
LEFT_CENTER,
62+
63+
/**
64+
* Indicates that the control should be placed along the right side of the map, centered between
65+
* the TOP_RIGHT and BOTTOM_RIGHT positions.
66+
*/
67+
RIGHT_CENTER,
68+
69+
/**
70+
* Indicates that the control should be placed along the bottom left of the map, but above any
71+
* BOTTOM_LEFT elements.
72+
*/
73+
LEFT_BOTTOM,
74+
75+
/**
76+
* Indicates that the control should be placed along the bottom right of the map, but above any
77+
* BOTTOM_RIGHT elements.
78+
*/
79+
RIGHT_BOTTOM,
80+
81+
/**
82+
* Indicates that the control should be placed along the bottom center of the map.
83+
*/
84+
BOTTOM_CENTER,
85+
86+
/**
87+
* Indicates that the control should be placed along the bottom left of the map, with any
88+
* sub-elements of the control "flowing" towards the bottom center.
89+
*/
90+
BOTTOM_LEFT,
91+
92+
/**
93+
* Indicates that the control should be placed along the bottom right of the map, with any
94+
* sub-elements of the control "flowing" towards the bottom center.
95+
*/
96+
BOTTOM_RIGHT;
97+
98+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.button.Button;
24+
import elemental.json.Json;
25+
import elemental.json.JsonObject;
26+
import java.io.Serializable;
27+
import java.util.Optional;
28+
29+
/**
30+
* Represents a custom control that can be added to the map.
31+
*
32+
* A button to represent the custom control and the {@link ControlPosition position} where the
33+
* button should be displayed within the map should be specified.
34+
*
35+
*/
36+
public class CustomControl implements Serializable {
37+
38+
private static final long serialVersionUID = -1821466465711569857L;
39+
40+
private Button controlButton;
41+
42+
private ControlPosition controlPosition;
43+
44+
public CustomControl(Button controlButton, ControlPosition controlPosition) {
45+
this.controlButton = controlButton;
46+
this.controlPosition = controlPosition;
47+
}
48+
49+
public Button getControlButton() {
50+
return controlButton;
51+
}
52+
53+
public void setControlButton(Button controlButton) {
54+
this.controlButton = controlButton;
55+
}
56+
57+
public ControlPosition getControlPosition() {
58+
return controlPosition;
59+
}
60+
61+
public void setControlPosition(ControlPosition controlPosition) {
62+
this.controlPosition = controlPosition;
63+
}
64+
65+
protected JsonObject getJson(int id) {
66+
JsonObject js = Json.createObject();
67+
js.put("id", id);
68+
Optional.ofNullable(controlPosition)
69+
.ifPresent(value -> js.put("position", controlPosition.name()));
70+
return js;
71+
}
72+
73+
}

src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMap.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import com.vaadin.flow.dom.DebouncePhase;
3636
import com.vaadin.flow.dom.DomListenerRegistration;
3737
import com.vaadin.flow.shared.Registration;
38+
import elemental.json.Json;
39+
import elemental.json.JsonArray;
3840
import elemental.json.JsonObject;
3941
import elemental.json.JsonValue;
4042
import java.util.List;
@@ -44,7 +46,7 @@
4446
@SuppressWarnings("serial")
4547
@Tag("google-map")
4648
@JsModule("@flowingcode/google-map/google-map.js")
47-
@NpmPackage(value = "@flowingcode/google-map", version = "3.6.1")
49+
@NpmPackage(value = "@flowingcode/google-map", version = "3.7.1")
4850
@NpmPackage(value = "@googlemaps/markerclusterer", version = "2.0.8")
4951
@JsModule("./googlemaps/geolocation.js")
5052
public class GoogleMap extends Component implements HasSize {
@@ -625,5 +627,20 @@ public LatLonBounds getBounds() {
625627
return bounds;
626628
}
627629
}
628-
630+
631+
/**
632+
* Adds custom control buttons to the map.
633+
*
634+
* @param customControls list of custom controls to add to the map
635+
*/
636+
public void addCustomControls(CustomControl... customControls) {
637+
JsonArray jsonArray = Json.createArray();
638+
for (int i = 0; i < customControls.length; i++) {
639+
CustomControl customControl = customControls[i];
640+
jsonArray.set(i, customControl.getJson(i));
641+
customControl.getControlButton().getElement().setAttribute("slot", "customControlSlot_" + i);
642+
this.getElement().appendChild(customControl.getControlButton().getElement());
643+
}
644+
this.getElement().setPropertyJson("customControls", jsonArray);
645+
}
629646
}

src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMapMarker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
@SuppressWarnings("serial")
4040
@Tag("google-map-marker")
4141
@JsModule("@flowingcode/google-map/google-map-marker.js")
42-
@NpmPackage(value = "@flowingcode/google-map", version = "3.6.1")
42+
@NpmPackage(value = "@flowingcode/google-map", version = "3.7.1")
4343
@NpmPackage(value = "@googlemaps/markerclusterer", version = "2.0.8")
4444
public class GoogleMapMarker extends Component {
4545

src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMapPoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
@SuppressWarnings("serial")
2929
@Tag("google-map-point")
3030
@JsModule("@flowingcode/google-map/google-map-point.js")
31-
@NpmPackage(value = "@flowingcode/google-map", version = "3.6.1")
31+
@NpmPackage(value = "@flowingcode/google-map", version = "3.7.1")
3232
@NpmPackage(value = "@googlemaps/markerclusterer", version = "2.0.8")
3333
public class GoogleMapPoint extends Component {
3434

src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMapPoly.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
@Tag("google-map-poly")
4040
@JsModule("@flowingcode/google-map/google-map-poly.js")
4141
@JsModule("@flowingcode/google-map/google-map-point.js")
42-
@NpmPackage(value = "@flowingcode/google-map", version = "3.6.1")
42+
@NpmPackage(value = "@flowingcode/google-map", version = "3.7.1")
4343
@NpmPackage(value = "@googlemaps/markerclusterer", version = "2.0.8")
4444
public abstract class GoogleMapPoly extends Component {
4545

0 commit comments

Comments
 (0)