|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Google Maps Addon |
| 4 | + * %% |
| 5 | + * Copyright (C) 2020 - 2025 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 | +package com.flowingcode.vaadin.addons.googlemaps; |
| 21 | + |
| 22 | +import elemental.json.Json; |
| 23 | +import elemental.json.JsonObject; |
| 24 | +import lombok.Getter; |
| 25 | +import lombok.NonNull; |
| 26 | +import lombok.Setter; |
| 27 | + |
| 28 | +import java.util.Objects; |
| 29 | +import java.util.Optional; |
| 30 | + |
| 31 | +/** |
| 32 | + * Class representing a marker label. |
| 33 | + * |
| 34 | + * @see <a href= |
| 35 | + * "https://developers.google.com/maps/documentation/javascript/reference/marker#MarkerLabel">Google |
| 36 | + * Maps MarkerLabel</a> |
| 37 | + */ |
| 38 | +@Getter |
| 39 | +@Setter |
| 40 | +public class MarkerLabel { |
| 41 | + /** |
| 42 | + * The text to be displayed in the label. Required field. |
| 43 | + */ |
| 44 | + @NonNull |
| 45 | + private String text; |
| 46 | + |
| 47 | + /** |
| 48 | + * Optional. The color of the label text. Defaults to: <code>'black'</code>. |
| 49 | + */ |
| 50 | + private String color; |
| 51 | + |
| 52 | + /** |
| 53 | + * Optional. The font family of the label text (equivalent to the CSS font-family property). |
| 54 | + */ |
| 55 | + private String fontFamily; |
| 56 | + |
| 57 | + /** |
| 58 | + * Optional. The font size of the label text (equivalent to the CSS font-size property). Defaults |
| 59 | + * to: <code>'14px'</code>. |
| 60 | + */ |
| 61 | + private String fontSize; |
| 62 | + |
| 63 | + /** |
| 64 | + * Optional. The font weight of the label text (equivalent to the CSS font-weight property). |
| 65 | + */ |
| 66 | + private String fontWeight; |
| 67 | + |
| 68 | + /** |
| 69 | + * The className property of the label's element (equivalent to the element's class attribute). |
| 70 | + * Multiple space-separated CSS classes can be added. The font color, size, weight, and family can |
| 71 | + * only be set via the other properties of <code>MarkerLabel</code>. |
| 72 | + */ |
| 73 | + private String className; |
| 74 | + |
| 75 | + public MarkerLabel(String text) { |
| 76 | + this.text = Objects.requireNonNull(text, "Text cannot be null"); |
| 77 | + } |
| 78 | + |
| 79 | + public MarkerLabel(String text, String color, String fontSize) { |
| 80 | + this.text = Objects.requireNonNull(text, "Text cannot be null"); |
| 81 | + this.color = color; |
| 82 | + this.fontSize = fontSize; |
| 83 | + } |
| 84 | + |
| 85 | + protected JsonObject getJson() { |
| 86 | + JsonObject js = Json.createObject(); |
| 87 | + Optional.of(getText()).ifPresent(value -> js.put("text", value)); |
| 88 | + Optional.ofNullable(getColor()).ifPresent(value -> js.put("color", value)); |
| 89 | + Optional.ofNullable(getFontFamily()).ifPresent(value -> js.put("fontFamily", value)); |
| 90 | + Optional.ofNullable(getFontSize()).ifPresent(value -> js.put("fontSize", value)); |
| 91 | + Optional.ofNullable(getFontWeight()).ifPresent(value -> js.put("fontWeight", value)); |
| 92 | + Optional.ofNullable(getClassName()).ifPresent(value -> js.put("className", value)); |
| 93 | + return js; |
| 94 | + } |
| 95 | +} |
0 commit comments