Skip to content

Commit abd5756

Browse files
committed
Add the abillity to add css-classes to POI and HTML-Markers and add detail field to the POI-Marker
1 parent 5b4093e commit abd5756

File tree

7 files changed

+319
-79
lines changed

7 files changed

+319
-79
lines changed

src/main/java/de/bluecolored/bluemap/api/WebApp.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public interface WebApp {
9494
*/
9595
void registerScript(String url);
9696

97+
// ------
98+
9799
/**
98100
* @deprecated You should use the {@link #getWebRoot()} method to create the image-files you need, or store map/marker
99101
* specific images in the map's storage (See: {@link BlueMapMap#getAssetStorage()})!
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package de.bluecolored.bluemap.api.markers;
2+
3+
/**
4+
* @see POIMarker
5+
* @see ShapeMarker
6+
* @see ExtrudeMarker
7+
* @see LineMarker
8+
*/
9+
public interface DetailMarker {
10+
11+
/**
12+
* Getter for the detail of this marker. The label can include html-tags.
13+
* @return the detail of this {@link Marker}
14+
*/
15+
String getDetail();
16+
17+
/**
18+
* Sets the detail of this {@link Marker}. The detail can include html-tags.<br>
19+
* This is the text that will be displayed on the popup when you click on this marker.
20+
* <p>
21+
* <b>Important:</b><br>
22+
* Html-tags in the label will not be escaped, so you can use them to style the {@link Marker}-detail.<br>
23+
* Make sure you escape all html-tags from possible user inputs to prevent possible
24+
* <a href="https://en.wikipedia.org/wiki/Cross-site_scripting">XSS-Attacks</a> on the web-client!
25+
* </p>
26+
*
27+
* @param detail the new detail for this {@link ObjectMarker}
28+
*/
29+
void setDetail(String detail);
30+
31+
interface Builder<B> {
32+
33+
/**
34+
* Sets the detail of the {@link Marker}. The detail can include html-tags.<br>
35+
* This is the text that will be displayed on the popup when you click on the marker.
36+
* <p>
37+
* <b>Important:</b><br>
38+
* Html-tags in the label will not be escaped, so you can use them to style the {@link Marker}-detail.<br>
39+
* Make sure you escape all html-tags from possible user inputs to prevent possible
40+
* <a href="https://en.wikipedia.org/wiki/Cross-site_scripting">XSS-Attacks</a> on the web-client!
41+
* </p>
42+
*
43+
* @param detail the new detail for the {@link Marker}
44+
* @return this builder for chaining
45+
*/
46+
B detail(String detail);
47+
48+
}
49+
50+
}

src/main/java/de/bluecolored/bluemap/api/markers/DistanceRangedMarker.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
import com.flowpowered.math.vector.Vector3d;
2828
import de.bluecolored.bluemap.api.debug.DebugDump;
2929

30+
/**
31+
* @see HtmlMarker
32+
* @see POIMarker
33+
* @see ShapeMarker
34+
* @see ExtrudeMarker
35+
* @see LineMarker
36+
*/
3037
@DebugDump
3138
public abstract class DistanceRangedMarker extends Marker {
3239

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package de.bluecolored.bluemap.api.markers;
2+
3+
import com.flowpowered.math.vector.Vector2i;
4+
5+
import java.util.Arrays;
6+
import java.util.Collection;
7+
import java.util.regex.Pattern;
8+
9+
/**
10+
* @see HtmlMarker
11+
* @see POIMarker
12+
*/
13+
public interface ElementMarker {
14+
15+
Pattern STYLE_CLASS_PATTERN = Pattern.compile("-?[_a-zA-Z]+[_a-zA-Z0-9-]*");
16+
17+
/**
18+
* Getter for the position (in pixels) where the element is anchored to the map.
19+
* @return the anchor-position in pixels
20+
*/
21+
Vector2i getAnchor();
22+
23+
/**
24+
* Sets the position (in pixels) where the element is anchored to the map.
25+
* @param anchor the anchor-position in pixels
26+
*/
27+
void setAnchor(Vector2i anchor);
28+
29+
/**
30+
* Sets the position (in pixels) where the element is anchored to the map.
31+
* @param x the anchor-x-position in pixels
32+
* @param y the anchor-y-position in pixels
33+
*/
34+
default void setAnchor(int x, int y) {
35+
setAnchor(new Vector2i(x, y));
36+
}
37+
38+
/**
39+
* Getter for an (unmodifiable) collection of CSS-classed that the element of this marker will have.
40+
* @return the style-classes of this element-marker
41+
*/
42+
Collection<String> getStyleClasses();
43+
44+
/**
45+
* Sets the CSS-classes that the element of this marker will have.<br>
46+
* All classes must match <code>-?[_a-zA-Z]+[_a-zA-Z0-9-]*</code><br>
47+
* Any existing classes on this marker will be removed.
48+
* @param styleClasses the style-classes this element-marker will have
49+
*/
50+
default void setStyleClasses(String... styleClasses) {
51+
this.setStyleClasses(Arrays.asList(styleClasses));
52+
}
53+
54+
/**
55+
* Sets the CSS-classes that the element of this marker will have.<br>
56+
* All classes must match <code>-?[_a-zA-Z]+[_a-zA-Z0-9-]*</code><br>
57+
* Any existing classes on this marker will be removed.
58+
* @param styleClasses the style-classes this element-marker will have
59+
*/
60+
void setStyleClasses(Collection<String> styleClasses);
61+
62+
/**
63+
* Adds the CSS-classes that the element of this marker will have.<br>
64+
* All classes must match <code>-?[_a-zA-Z]+[_a-zA-Z0-9-]*</code><br>
65+
* @param styleClasses the style-classes this element-marker will have
66+
*/
67+
default void addStyleClasses(String... styleClasses) {
68+
this.setStyleClasses(Arrays.asList(styleClasses));
69+
}
70+
71+
/**
72+
* Adds the CSS-classes that the element of this marker will have.<br>
73+
* All classes must match <code>-?[_a-zA-Z]+[_a-zA-Z0-9-]*</code><br>
74+
* @param styleClasses the style-classes this element-marker will have
75+
*/
76+
void addStyleClasses(Collection<String> styleClasses);
77+
78+
interface Builder<B> {
79+
80+
/**
81+
* Sets the position (in pixels) where the element is anchored to the map.
82+
* @param anchor the anchor-position in pixels
83+
* @return this builder for chaining
84+
*/
85+
B anchor(Vector2i anchor);
86+
87+
/**
88+
* <b>Adds</b> the CSS-classes that the element of this marker will have.<br>
89+
* All classes must match <code>-?[_a-zA-Z]+[_a-zA-Z0-9-]*</code><br>
90+
* @return this builder for chaining
91+
*/
92+
B styleClasses(String... styleClasses);
93+
94+
/**
95+
* Removes any existing style-classes from this builder.
96+
* @see #styleClasses(String...)
97+
* @return this builder for chaining
98+
*/
99+
B clearStyleClasses();
100+
101+
/**
102+
* Sets the position (in pixels) where the element is anchored to the map.
103+
* @param x the anchor-x-position in pixels
104+
* @param y the anchor-y-position in pixels
105+
* @return this builder for chaining
106+
*/
107+
default B anchor(int x, int y) {
108+
return anchor(new Vector2i(x, y));
109+
}
110+
111+
}
112+
113+
}

src/main/java/de/bluecolored/bluemap/api/markers/HtmlMarker.java

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@
2929
import com.flowpowered.math.vector.Vector3d;
3030
import de.bluecolored.bluemap.api.debug.DebugDump;
3131

32-
import java.util.Objects;
32+
import java.util.*;
3333

3434
/**
3535
* A marker that is a html-element placed somewhere on the map.
3636
*/
37+
@SuppressWarnings("FieldMayBeFinal")
3738
@DebugDump
38-
public class HtmlMarker extends DistanceRangedMarker {
39+
public class HtmlMarker extends DistanceRangedMarker implements ElementMarker {
40+
41+
private Set<String> classes = new HashSet<>();
3942

4043
private Vector2i anchor;
4144
private String html;
@@ -82,31 +85,16 @@ public HtmlMarker(String label, Vector3d position, String html, Vector2i anchor)
8285
this.anchor = Objects.requireNonNull(anchor, "anchor must not be null");
8386
}
8487

85-
/**
86-
* Getter for the position (in pixels) where the html-element is anchored to the map.
87-
* @return the anchor-position in pixels
88-
*/
88+
@Override
8989
public Vector2i getAnchor() {
9090
return anchor;
9191
}
9292

93-
/**
94-
* Sets the position (in pixels) where the html-element is anchored to the map.
95-
* @param anchor the anchor-position in pixels
96-
*/
93+
@Override
9794
public void setAnchor(Vector2i anchor) {
9895
this.anchor = Objects.requireNonNull(anchor, "anchor must not be null");
9996
}
10097

101-
/**
102-
* Sets the position (in pixels) where the html-element is anchored to the map.
103-
* @param x the anchor-x-position in pixels
104-
* @param y the anchor-y-position in pixels
105-
*/
106-
public void setAnchor(int x, int y) {
107-
setAnchor(new Vector2i(x, y));
108-
}
109-
11098
/**
11199
* Getter for the html-code of this HTML marker
112100
* @return the html-code
@@ -130,6 +118,28 @@ public void setHtml(String html) {
130118
this.html = Objects.requireNonNull(html, "html must not be null");
131119
}
132120

121+
@Override
122+
public Collection<String> getStyleClasses() {
123+
return Collections.unmodifiableCollection(this.classes);
124+
}
125+
126+
@Override
127+
public void setStyleClasses(Collection<String> styleClasses) {
128+
if (!styleClasses.stream().allMatch(STYLE_CLASS_PATTERN.asMatchPredicate()))
129+
throw new IllegalArgumentException("One of the provided style-classes has an invalid format!");
130+
131+
this.classes.clear();
132+
this.classes.addAll(styleClasses);
133+
}
134+
135+
@Override
136+
public void addStyleClasses(Collection<String> styleClasses) {
137+
if (!styleClasses.stream().allMatch(STYLE_CLASS_PATTERN.asMatchPredicate()))
138+
throw new IllegalArgumentException("One of the provided style-classes has an invalid format!");
139+
140+
this.classes.addAll(styleClasses);
141+
}
142+
133143
@Override
134144
public boolean equals(Object o) {
135145
if (this == o) return true;
@@ -158,32 +168,20 @@ public static Builder builder() {
158168
return new Builder();
159169
}
160170

161-
public static class Builder extends DistanceRangedMarker.Builder<HtmlMarker, Builder> {
171+
public static class Builder extends DistanceRangedMarker.Builder<HtmlMarker, Builder>
172+
implements ElementMarker.Builder<Builder> {
173+
174+
Set<String> classes = new HashSet<>();
162175

163176
Vector2i anchor;
164177
String html;
165178

166-
/**
167-
* Sets the position (in pixels) where the html-element is anchored to the map.
168-
* @param anchor the anchor-position in pixels
169-
* @return this builder for chaining
170-
*/
179+
@Override
171180
public Builder anchor(Vector2i anchor) {
172181
this.anchor = anchor;
173182
return this;
174183
}
175184

176-
/**
177-
* Sets the position (in pixels) where the html-element is anchored to the map.
178-
* @param x the anchor-x-position in pixels
179-
* @param y the anchor-y-position in pixels
180-
* @return this builder for chaining
181-
*/
182-
public Builder anchor(int x, int y) {
183-
this.anchor = new Vector2i(x, y);
184-
return this;
185-
}
186-
187185
/**
188186
* Sets the html for the {@link HtmlMarker}.
189187
*
@@ -200,6 +198,22 @@ public Builder html(String html) {
200198
return this;
201199
}
202200

201+
@Override
202+
public Builder styleClasses(String... styleClasses) {
203+
Collection<String> styleClassesCollection = Arrays.asList(styleClasses);
204+
if (!styleClassesCollection.stream().allMatch(STYLE_CLASS_PATTERN.asMatchPredicate()))
205+
throw new IllegalArgumentException("One of the provided style-classes has an invalid format!");
206+
207+
this.classes.addAll(styleClassesCollection);
208+
return this;
209+
}
210+
211+
@Override
212+
public Builder clearStyleClasses() {
213+
this.classes.clear();
214+
return this;
215+
}
216+
203217
/**
204218
* Creates a new {@link HtmlMarker} with the current builder-settings.<br>
205219
* The minimum required settings to build this marker are:

0 commit comments

Comments
 (0)