1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.styleelement;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Arrays;
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Objects;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
11 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
12 | import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent;
|
---|
13 | import org.openstreetmap.josm.spi.preferences.PreferenceChangedListener;
|
---|
14 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * <p>Provides an abstract parent class and three concrete sub classes for various
|
---|
18 | * strategies on how to compose the text label which can be rendered close to a node
|
---|
19 | * or within an area in an OSM map.</p>
|
---|
20 | *
|
---|
21 | * <p>The three strategies below support three rules for composing a label:
|
---|
22 | * <ul>
|
---|
23 | * <li>{@link StaticLabelCompositionStrategy} - the label is given by a static text
|
---|
24 | * specified in the MapCSS style file</li>
|
---|
25 | *
|
---|
26 | * <li>{@link TagLookupCompositionStrategy} - the label is given by the content of a
|
---|
27 | * tag whose name specified in the MapCSS style file</li>
|
---|
28 | *
|
---|
29 | * <li>{@link DeriveLabelFromNameTagsCompositionStrategy} - the label is given by the value
|
---|
30 | * of one of the configured "name tags". The list of relevant name tags can be configured
|
---|
31 | * in the JOSM preferences
|
---|
32 | * see the preference options <code>mappaint.nameOrder</code> and <code>mappaint.nameComplementOrder</code>.</li>
|
---|
33 | * </ul>
|
---|
34 | * @since 3987 (creation)
|
---|
35 | * @since 10599 (functional interface)
|
---|
36 | */
|
---|
37 | @FunctionalInterface
|
---|
38 | public interface LabelCompositionStrategy {
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Replies the text value to be rendered as label for the primitive {@code primitive}.
|
---|
42 | *
|
---|
43 | * @param primitive the primitive
|
---|
44 | *
|
---|
45 | * @return the text value to be rendered or null, if primitive is null or
|
---|
46 | * if no suitable value could be composed
|
---|
47 | */
|
---|
48 | String compose(IPrimitive primitive);
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Strategy where the label is given by a static text specified in the MapCSS style file.
|
---|
52 | */
|
---|
53 | class StaticLabelCompositionStrategy implements LabelCompositionStrategy {
|
---|
54 | private final String defaultLabel;
|
---|
55 |
|
---|
56 | public StaticLabelCompositionStrategy(String defaultLabel) {
|
---|
57 | this.defaultLabel = defaultLabel;
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public String compose(IPrimitive primitive) {
|
---|
62 | return defaultLabel;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public String getDefaultLabel() {
|
---|
66 | return defaultLabel;
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public String toString() {
|
---|
71 | return '{' + getClass().getSimpleName() + " defaultLabel=" + defaultLabel + '}';
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public int hashCode() {
|
---|
76 | return Objects.hash(defaultLabel);
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public boolean equals(Object obj) {
|
---|
81 | if (this == obj) return true;
|
---|
82 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
83 | StaticLabelCompositionStrategy that = (StaticLabelCompositionStrategy) obj;
|
---|
84 | return Objects.equals(defaultLabel, that.defaultLabel);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Strategy where the label is given by the content of a tag whose name specified in the MapCSS style file.
|
---|
90 | */
|
---|
91 | class TagLookupCompositionStrategy implements LabelCompositionStrategy {
|
---|
92 |
|
---|
93 | private final String defaultLabelTag;
|
---|
94 |
|
---|
95 | public TagLookupCompositionStrategy(String defaultLabelTag) {
|
---|
96 | if (defaultLabelTag != null) {
|
---|
97 | defaultLabelTag = defaultLabelTag.trim();
|
---|
98 | if (defaultLabelTag.isEmpty()) {
|
---|
99 | defaultLabelTag = null;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | this.defaultLabelTag = defaultLabelTag;
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public String compose(IPrimitive primitive) {
|
---|
107 | if (defaultLabelTag == null) return null;
|
---|
108 | if (primitive == null) return null;
|
---|
109 | return primitive.get(defaultLabelTag);
|
---|
110 | }
|
---|
111 |
|
---|
112 | public String getDefaultLabelTag() {
|
---|
113 | return defaultLabelTag;
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public String toString() {
|
---|
118 | return '{' + getClass().getSimpleName() + " defaultLabelTag=" + defaultLabelTag + '}';
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Override
|
---|
122 | public int hashCode() {
|
---|
123 | return Objects.hash(defaultLabelTag);
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Override
|
---|
127 | public boolean equals(Object obj) {
|
---|
128 | if (this == obj) return true;
|
---|
129 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
130 | TagLookupCompositionStrategy that = (TagLookupCompositionStrategy) obj;
|
---|
131 | return Objects.equals(defaultLabelTag, that.defaultLabelTag);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Strategy where the label is given by the value of one of the configured "name tags".
|
---|
137 | * The list of relevant name tags can be configured in the JOSM preferences
|
---|
138 | * see the preference options <code>mappaint.nameOrder</code> and <code>mappaint.nameComplementOrder</code>
|
---|
139 | */
|
---|
140 | class DeriveLabelFromNameTagsCompositionStrategy implements LabelCompositionStrategy, PreferenceChangedListener {
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * The list of default name tags from which a label candidate is derived.
|
---|
144 | */
|
---|
145 | private static final String[] DEFAULT_NAME_TAGS = {
|
---|
146 | "name:" + LanguageInfo.getJOSMLocaleCode(),
|
---|
147 | "name",
|
---|
148 | "int_name",
|
---|
149 | "distance",
|
---|
150 | "ref",
|
---|
151 | "operator",
|
---|
152 | "brand",
|
---|
153 | "addr:housenumber"
|
---|
154 | };
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * The list of default name complement tags from which a label candidate is derived.
|
---|
158 | */
|
---|
159 | private static final String[] DEFAULT_NAME_COMPLEMENT_TAGS = {
|
---|
160 | "capacity"
|
---|
161 | };
|
---|
162 |
|
---|
163 | private List<String> nameTags = new ArrayList<>();
|
---|
164 | private List<String> nameComplementTags = new ArrayList<>();
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * <p>Creates the strategy and initializes its name tags from the preferences.</p>
|
---|
168 | */
|
---|
169 | public DeriveLabelFromNameTagsCompositionStrategy() {
|
---|
170 | Config.getPref().addPreferenceChangeListener(this);
|
---|
171 | initNameTagsFromPreferences();
|
---|
172 | }
|
---|
173 |
|
---|
174 | private static List<String> buildNameTags(List<String> nameTags) {
|
---|
175 | List<String> result = new ArrayList<>();
|
---|
176 | if (nameTags != null) {
|
---|
177 | for (String tag: nameTags) {
|
---|
178 | if (tag == null) {
|
---|
179 | continue;
|
---|
180 | }
|
---|
181 | tag = tag.trim();
|
---|
182 | if (tag.isEmpty()) {
|
---|
183 | continue;
|
---|
184 | }
|
---|
185 | result.add(tag);
|
---|
186 | }
|
---|
187 | }
|
---|
188 | return result;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Sets the name tags to be looked up in order to build up the label.
|
---|
193 | *
|
---|
194 | * @param nameTags the name tags. null values are ignored.
|
---|
195 | */
|
---|
196 | public void setNameTags(List<String> nameTags) {
|
---|
197 | this.nameTags = buildNameTags(nameTags);
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Sets the name complement tags to be looked up in order to build up the label.
|
---|
202 | *
|
---|
203 | * @param nameComplementTags the name complement tags. null values are ignored.
|
---|
204 | * @since 6541
|
---|
205 | */
|
---|
206 | public void setNameComplementTags(List<String> nameComplementTags) {
|
---|
207 | this.nameComplementTags = buildNameTags(nameComplementTags);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Replies an unmodifiable list of the name tags used to compose the label.
|
---|
212 | *
|
---|
213 | * @return the list of name tags
|
---|
214 | */
|
---|
215 | public List<String> getNameTags() {
|
---|
216 | return Collections.unmodifiableList(nameTags);
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Replies an unmodifiable list of the name complement tags used to compose the label.
|
---|
221 | *
|
---|
222 | * @return the list of name complement tags
|
---|
223 | * @since 6541
|
---|
224 | */
|
---|
225 | public List<String> getNameComplementTags() {
|
---|
226 | return Collections.unmodifiableList(nameComplementTags);
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Initializes the name tags to use from a list of default name tags (see
|
---|
231 | * {@link #DEFAULT_NAME_TAGS} and {@link #DEFAULT_NAME_COMPLEMENT_TAGS})
|
---|
232 | * and from name tags configured in the preferences using the keys
|
---|
233 | * <code>mappaint.nameOrder</code> and <code>mappaint.nameComplementOrder</code>.
|
---|
234 | */
|
---|
235 | public final void initNameTagsFromPreferences() {
|
---|
236 | if (Config.getPref() == null) {
|
---|
237 | this.nameTags = new ArrayList<>(Arrays.asList(DEFAULT_NAME_TAGS));
|
---|
238 | this.nameComplementTags = new ArrayList<>(Arrays.asList(DEFAULT_NAME_COMPLEMENT_TAGS));
|
---|
239 | } else {
|
---|
240 | this.nameTags = new ArrayList<>(
|
---|
241 | Config.getPref().getList("mappaint.nameOrder", Arrays.asList(DEFAULT_NAME_TAGS))
|
---|
242 | );
|
---|
243 | this.nameComplementTags = new ArrayList<>(
|
---|
244 | Config.getPref().getList("mappaint.nameComplementOrder", Arrays.asList(DEFAULT_NAME_COMPLEMENT_TAGS))
|
---|
245 | );
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | private String getPrimitiveName(IPrimitive n) {
|
---|
250 | StringBuilder name = new StringBuilder();
|
---|
251 | if (!n.hasKeys()) return null;
|
---|
252 | for (String rn : nameTags) {
|
---|
253 | String val = n.get(rn);
|
---|
254 | if (val != null) {
|
---|
255 | name.append(val);
|
---|
256 | break;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | for (String rn : nameComplementTags) {
|
---|
260 | String comp = n.get(rn);
|
---|
261 | if (comp != null) {
|
---|
262 | if (name.length() == 0) {
|
---|
263 | name.append(comp);
|
---|
264 | } else {
|
---|
265 | name.append(" (").append(comp).append(')');
|
---|
266 | }
|
---|
267 | break;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | return name.toString();
|
---|
271 | }
|
---|
272 |
|
---|
273 | @Override
|
---|
274 | public String compose(IPrimitive primitive) {
|
---|
275 | if (primitive == null) return null;
|
---|
276 | return getPrimitiveName(primitive);
|
---|
277 | }
|
---|
278 |
|
---|
279 | @Override
|
---|
280 | public String toString() {
|
---|
281 | return '{' + getClass().getSimpleName() + '}';
|
---|
282 | }
|
---|
283 |
|
---|
284 | @Override
|
---|
285 | public void preferenceChanged(PreferenceChangeEvent e) {
|
---|
286 | if (e.getKey() != null && e.getKey().startsWith("mappaint.name")) {
|
---|
287 | initNameTagsFromPreferences();
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|