1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trcLazy;
|
---|
7 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
8 |
|
---|
9 | import java.awt.ComponentOrientation;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.Comparator;
|
---|
15 | import java.util.HashSet;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Locale;
|
---|
19 | import java.util.Map;
|
---|
20 | import java.util.Set;
|
---|
21 | import java.util.stream.Collectors;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.data.coor.CoordinateFormat;
|
---|
25 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
26 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
27 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
28 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
29 | import org.openstreetmap.josm.data.osm.NameFormatter;
|
---|
30 | import org.openstreetmap.josm.data.osm.Node;
|
---|
31 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
32 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
33 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
34 | import org.openstreetmap.josm.data.osm.Way;
|
---|
35 | import org.openstreetmap.josm.data.osm.history.HistoryNameFormatter;
|
---|
36 | import org.openstreetmap.josm.data.osm.history.HistoryNode;
|
---|
37 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
38 | import org.openstreetmap.josm.data.osm.history.HistoryRelation;
|
---|
39 | import org.openstreetmap.josm.data.osm.history.HistoryWay;
|
---|
40 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
41 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetNameTemplateList;
|
---|
42 | import org.openstreetmap.josm.tools.AlphanumComparator;
|
---|
43 | import org.openstreetmap.josm.tools.I18n;
|
---|
44 | import org.openstreetmap.josm.tools.Utils;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * This is the default implementation of a {@link NameFormatter} for names of {@link OsmPrimitive}s
|
---|
48 | * and {@link HistoryOsmPrimitive}s.
|
---|
49 | * @since 1990
|
---|
50 | */
|
---|
51 | public class DefaultNameFormatter implements NameFormatter, HistoryNameFormatter {
|
---|
52 |
|
---|
53 | private static DefaultNameFormatter instance;
|
---|
54 |
|
---|
55 | private static final List<NameFormatterHook> FORMAT_HOOKS = new LinkedList<>();
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Replies the unique instance of this formatter
|
---|
59 | *
|
---|
60 | * @return the unique instance of this formatter
|
---|
61 | */
|
---|
62 | public static synchronized DefaultNameFormatter getInstance() {
|
---|
63 | if (instance == null) {
|
---|
64 | instance = new DefaultNameFormatter();
|
---|
65 | }
|
---|
66 | return instance;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Registers a format hook. Adds the hook at the first position of the format hooks.
|
---|
71 | * (for plugins)
|
---|
72 | *
|
---|
73 | * @param hook the format hook. Ignored if null.
|
---|
74 | */
|
---|
75 | public static void registerFormatHook(NameFormatterHook hook) {
|
---|
76 | if (hook == null) return;
|
---|
77 | if (!FORMAT_HOOKS.contains(hook)) {
|
---|
78 | FORMAT_HOOKS.add(0, hook);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Unregisters a format hook. Removes the hook from the list of format hooks.
|
---|
84 | *
|
---|
85 | * @param hook the format hook. Ignored if null.
|
---|
86 | */
|
---|
87 | public static void unregisterFormatHook(NameFormatterHook hook) {
|
---|
88 | if (hook == null) return;
|
---|
89 | if (FORMAT_HOOKS.contains(hook)) {
|
---|
90 | FORMAT_HOOKS.remove(hook);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** The default list of tags which are used as naming tags in relations.
|
---|
95 | * A ? prefix indicates a boolean value, for which the key (instead of the value) is used.
|
---|
96 | */
|
---|
97 | private static final String[] DEFAULT_NAMING_TAGS_FOR_RELATIONS = {"name", "ref", "restriction", "landuse", "natural",
|
---|
98 | "leisure", "amenity", "public_transport", ":LocationCode", "note", "?building"};
|
---|
99 |
|
---|
100 | /** the current list of tags used as naming tags in relations */
|
---|
101 | private static List<String> namingTagsForRelations;
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Replies the list of naming tags used in relations. The list is given (in this order) by:
|
---|
105 | * <ul>
|
---|
106 | * <li>by the tag names in the preference <tt>relation.nameOrder</tt></li>
|
---|
107 | * <li>by the default tags in {@link #DEFAULT_NAMING_TAGS_FOR_RELATIONS}
|
---|
108 | * </ul>
|
---|
109 | *
|
---|
110 | * @return the list of naming tags used in relations
|
---|
111 | */
|
---|
112 | public static synchronized List<String> getNamingtagsForRelations() {
|
---|
113 | if (namingTagsForRelations == null) {
|
---|
114 | namingTagsForRelations = new ArrayList<>(
|
---|
115 | Main.pref.getCollection("relation.nameOrder", Arrays.asList(DEFAULT_NAMING_TAGS_FOR_RELATIONS))
|
---|
116 | );
|
---|
117 | }
|
---|
118 | return namingTagsForRelations;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Decorates the name of primitive with its id, if the preference
|
---|
123 | * <tt>osm-primitives.showid</tt> is set. Shows unique id if osm-primitives.showid.new-primitives is set
|
---|
124 | *
|
---|
125 | * @param name the name without the id
|
---|
126 | * @param primitive the primitive
|
---|
127 | */
|
---|
128 | protected void decorateNameWithId(StringBuilder name, IPrimitive primitive) {
|
---|
129 | if (Main.pref.getBoolean("osm-primitives.showid")) {
|
---|
130 | if (Main.pref.getBoolean("osm-primitives.showid.new-primitives")) {
|
---|
131 | name.append(tr(" [id: {0}]", primitive.getUniqueId()));
|
---|
132 | } else {
|
---|
133 | name.append(tr(" [id: {0}]", primitive.getId()));
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Formats a name for an {@link OsmPrimitive}.
|
---|
140 | *
|
---|
141 | * @param osm the primitive
|
---|
142 | * @return the name
|
---|
143 | * @since 10991
|
---|
144 | */
|
---|
145 | public String format(OsmPrimitive osm) {
|
---|
146 | if (osm instanceof Node) {
|
---|
147 | return format((Node) osm);
|
---|
148 | } else if (osm instanceof Way) {
|
---|
149 | return format((Way) osm);
|
---|
150 | } else if (osm instanceof Relation) {
|
---|
151 | return format((Relation) osm);
|
---|
152 | }
|
---|
153 | return null;
|
---|
154 | }
|
---|
155 |
|
---|
156 | @Override
|
---|
157 | public String format(Node node) {
|
---|
158 | StringBuilder name = new StringBuilder();
|
---|
159 | if (node.isIncomplete()) {
|
---|
160 | name.append(tr("incomplete"));
|
---|
161 | } else {
|
---|
162 | TaggingPreset preset = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(node);
|
---|
163 | if (preset == null) {
|
---|
164 | String n;
|
---|
165 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
---|
166 | n = node.getLocalName();
|
---|
167 | } else {
|
---|
168 | n = node.getName();
|
---|
169 | }
|
---|
170 | if (n == null) {
|
---|
171 | String s = node.get("addr:housename");
|
---|
172 | if (s != null) {
|
---|
173 | /* I18n: name of house as parameter */
|
---|
174 | n = tr("House {0}", s);
|
---|
175 | }
|
---|
176 | if (n == null && (s = node.get("addr:housenumber")) != null) {
|
---|
177 | String t = node.get("addr:street");
|
---|
178 | if (t != null) {
|
---|
179 | /* I18n: house number, street as parameter, number should remain
|
---|
180 | before street for better visibility */
|
---|
181 | n = tr("House number {0} at {1}", s, t);
|
---|
182 | } else {
|
---|
183 | /* I18n: house number as parameter */
|
---|
184 | n = tr("House number {0}", s);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (n == null) {
|
---|
190 | n = node.isNew() ? tr("node") : Long.toString(node.getId());
|
---|
191 | }
|
---|
192 | name.append(n);
|
---|
193 | } else {
|
---|
194 | preset.nameTemplate.appendText(name, node);
|
---|
195 | }
|
---|
196 | if (node.getCoor() != null) {
|
---|
197 | name.append(" \u200E(").append(node.getCoor().latToString(CoordinateFormat.getDefaultFormat())).append(", ")
|
---|
198 | .append(node.getCoor().lonToString(CoordinateFormat.getDefaultFormat())).append(')');
|
---|
199 | }
|
---|
200 | }
|
---|
201 | decorateNameWithId(name, node);
|
---|
202 |
|
---|
203 |
|
---|
204 | String result = name.toString();
|
---|
205 | for (NameFormatterHook hook: FORMAT_HOOKS) {
|
---|
206 | String hookResult = hook.checkFormat(node, result);
|
---|
207 | if (hookResult != null)
|
---|
208 | return hookResult;
|
---|
209 | }
|
---|
210 |
|
---|
211 | return result;
|
---|
212 | }
|
---|
213 |
|
---|
214 | private final Comparator<Node> nodeComparator = (n1, n2) -> format(n1).compareTo(format(n2));
|
---|
215 |
|
---|
216 | @Override
|
---|
217 | public Comparator<Node> getNodeComparator() {
|
---|
218 | return nodeComparator;
|
---|
219 | }
|
---|
220 |
|
---|
221 | @Override
|
---|
222 | public String format(Way way) {
|
---|
223 | StringBuilder name = new StringBuilder();
|
---|
224 |
|
---|
225 | char mark;
|
---|
226 | // If current language is left-to-right (almost all languages)
|
---|
227 | if (ComponentOrientation.getOrientation(Locale.getDefault()).isLeftToRight()) {
|
---|
228 | // will insert Left-To-Right Mark to ensure proper display of text in the case when object name is right-to-left
|
---|
229 | mark = '\u200E';
|
---|
230 | } else {
|
---|
231 | // otherwise will insert Right-To-Left Mark to ensure proper display in the opposite case
|
---|
232 | mark = '\u200F';
|
---|
233 | }
|
---|
234 | // Initialize base direction of the string
|
---|
235 | name.append(mark);
|
---|
236 |
|
---|
237 | if (way.isIncomplete()) {
|
---|
238 | name.append(tr("incomplete"));
|
---|
239 | } else {
|
---|
240 | TaggingPreset preset = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(way);
|
---|
241 | if (preset == null) {
|
---|
242 | String n;
|
---|
243 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
---|
244 | n = way.getLocalName();
|
---|
245 | } else {
|
---|
246 | n = way.getName();
|
---|
247 | }
|
---|
248 | if (n == null) {
|
---|
249 | n = way.get("ref");
|
---|
250 | }
|
---|
251 | if (n == null) {
|
---|
252 | n = way.hasKey("highway") ? tr("highway") :
|
---|
253 | way.hasKey("railway") ? tr("railway") :
|
---|
254 | way.hasKey("waterway") ? tr("waterway") :
|
---|
255 | way.hasKey("landuse") ? tr("landuse") : null;
|
---|
256 | }
|
---|
257 | if (n == null) {
|
---|
258 | String s = way.get("addr:housename");
|
---|
259 | if (s != null) {
|
---|
260 | /* I18n: name of house as parameter */
|
---|
261 | n = tr("House {0}", s);
|
---|
262 | }
|
---|
263 | if (n == null && (s = way.get("addr:housenumber")) != null) {
|
---|
264 | String t = way.get("addr:street");
|
---|
265 | if (t != null) {
|
---|
266 | /* I18n: house number, street as parameter, number should remain
|
---|
267 | before street for better visibility */
|
---|
268 | n = tr("House number {0} at {1}", s, t);
|
---|
269 | } else {
|
---|
270 | /* I18n: house number as parameter */
|
---|
271 | n = tr("House number {0}", s);
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|
275 | if (n == null && way.hasKey("building")) {
|
---|
276 | n = tr("building");
|
---|
277 | }
|
---|
278 | if (n == null || n.isEmpty()) {
|
---|
279 | n = String.valueOf(way.getId());
|
---|
280 | }
|
---|
281 |
|
---|
282 | name.append(n);
|
---|
283 | } else {
|
---|
284 | preset.nameTemplate.appendText(name, way);
|
---|
285 | }
|
---|
286 |
|
---|
287 | int nodesNo = way.getRealNodesCount();
|
---|
288 | /* note: length == 0 should no longer happen, but leave the bracket code
|
---|
289 | nevertheless, who knows what future brings */
|
---|
290 | /* I18n: count of nodes as parameter */
|
---|
291 | String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
|
---|
292 | name.append(mark).append(" (").append(nodes).append(')');
|
---|
293 | }
|
---|
294 | decorateNameWithId(name, way);
|
---|
295 |
|
---|
296 | String result = name.toString();
|
---|
297 | for (NameFormatterHook hook: FORMAT_HOOKS) {
|
---|
298 | String hookResult = hook.checkFormat(way, result);
|
---|
299 | if (hookResult != null)
|
---|
300 | return hookResult;
|
---|
301 | }
|
---|
302 |
|
---|
303 | return result;
|
---|
304 | }
|
---|
305 |
|
---|
306 | private final Comparator<Way> wayComparator = (w1, w2) -> format(w1).compareTo(format(w2));
|
---|
307 |
|
---|
308 | @Override
|
---|
309 | public Comparator<Way> getWayComparator() {
|
---|
310 | return wayComparator;
|
---|
311 | }
|
---|
312 |
|
---|
313 | @Override
|
---|
314 | public String format(Relation relation) {
|
---|
315 | StringBuilder name = new StringBuilder();
|
---|
316 | if (relation.isIncomplete()) {
|
---|
317 | name.append(tr("incomplete"));
|
---|
318 | } else {
|
---|
319 | TaggingPreset preset = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(relation);
|
---|
320 |
|
---|
321 | formatRelationNameAndType(relation, name, preset);
|
---|
322 |
|
---|
323 | int mbno = relation.getMembersCount();
|
---|
324 | name.append(trn("{0} member", "{0} members", mbno, mbno));
|
---|
325 |
|
---|
326 | if (relation.hasIncompleteMembers()) {
|
---|
327 | name.append(", ").append(tr("incomplete"));
|
---|
328 | }
|
---|
329 |
|
---|
330 | name.append(')');
|
---|
331 | }
|
---|
332 | decorateNameWithId(name, relation);
|
---|
333 |
|
---|
334 | String result = name.toString();
|
---|
335 | for (NameFormatterHook hook: FORMAT_HOOKS) {
|
---|
336 | String hookResult = hook.checkFormat(relation, result);
|
---|
337 | if (hookResult != null)
|
---|
338 | return hookResult;
|
---|
339 | }
|
---|
340 |
|
---|
341 | return result;
|
---|
342 | }
|
---|
343 |
|
---|
344 | private static StringBuilder formatRelationNameAndType(Relation relation, StringBuilder result, TaggingPreset preset) {
|
---|
345 | if (preset == null) {
|
---|
346 | result.append(getRelationTypeName(relation));
|
---|
347 | String relationName = getRelationName(relation);
|
---|
348 | if (relationName == null) {
|
---|
349 | relationName = Long.toString(relation.getId());
|
---|
350 | } else {
|
---|
351 | relationName = '\"' + relationName + '\"';
|
---|
352 | }
|
---|
353 | result.append(" (").append(relationName).append(", ");
|
---|
354 | } else {
|
---|
355 | preset.nameTemplate.appendText(result, relation);
|
---|
356 | result.append('(');
|
---|
357 | }
|
---|
358 | return result;
|
---|
359 | }
|
---|
360 |
|
---|
361 | private final Comparator<Relation> relationComparator = (r1, r2) -> {
|
---|
362 | //TODO This doesn't work correctly with formatHooks
|
---|
363 |
|
---|
364 | TaggingPreset preset1 = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(r1);
|
---|
365 | TaggingPreset preset2 = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(r2);
|
---|
366 |
|
---|
367 | if (preset1 != null || preset2 != null) {
|
---|
368 | String name11 = formatRelationNameAndType(r1, new StringBuilder(), preset1).toString();
|
---|
369 | String name21 = formatRelationNameAndType(r2, new StringBuilder(), preset2).toString();
|
---|
370 |
|
---|
371 | int comp1 = AlphanumComparator.getInstance().compare(name11, name21);
|
---|
372 | if (comp1 != 0)
|
---|
373 | return comp1;
|
---|
374 | } else {
|
---|
375 |
|
---|
376 | String type1 = getRelationTypeName(r1);
|
---|
377 | String type2 = getRelationTypeName(r2);
|
---|
378 |
|
---|
379 | int comp2 = AlphanumComparator.getInstance().compare(type1, type2);
|
---|
380 | if (comp2 != 0)
|
---|
381 | return comp2;
|
---|
382 |
|
---|
383 | String name12 = getRelationName(r1);
|
---|
384 | String name22 = getRelationName(r2);
|
---|
385 |
|
---|
386 | comp2 = AlphanumComparator.getInstance().compare(name12, name22);
|
---|
387 | if (comp2 != 0)
|
---|
388 | return comp2;
|
---|
389 | }
|
---|
390 |
|
---|
391 | int comp3 = Integer.compare(r1.getMembersCount(), r2.getMembersCount());
|
---|
392 | if (comp3 != 0)
|
---|
393 | return comp3;
|
---|
394 |
|
---|
395 |
|
---|
396 | comp3 = Boolean.compare(r1.hasIncompleteMembers(), r2.hasIncompleteMembers());
|
---|
397 | if (comp3 != 0)
|
---|
398 | return comp3;
|
---|
399 |
|
---|
400 | return Long.compare(r1.getUniqueId(), r2.getUniqueId());
|
---|
401 | };
|
---|
402 |
|
---|
403 | @Override
|
---|
404 | public Comparator<Relation> getRelationComparator() {
|
---|
405 | return relationComparator;
|
---|
406 | }
|
---|
407 |
|
---|
408 | private static String getRelationTypeName(IRelation relation) {
|
---|
409 | String name = trc("Relation type", relation.get("type"));
|
---|
410 | if (name == null) {
|
---|
411 | name = relation.hasKey("public_transport") ? tr("public transport") : null;
|
---|
412 | }
|
---|
413 | if (name == null) {
|
---|
414 | String building = relation.get("building");
|
---|
415 | if (OsmUtils.isTrue(building)) {
|
---|
416 | name = tr("building");
|
---|
417 | } else if (building != null) {
|
---|
418 | name = tr(building); // translate tag!
|
---|
419 | }
|
---|
420 | }
|
---|
421 | if (name == null) {
|
---|
422 | name = trc("Place type", relation.get("place"));
|
---|
423 | }
|
---|
424 | if (name == null) {
|
---|
425 | name = tr("relation");
|
---|
426 | }
|
---|
427 | String adminLevel = relation.get("admin_level");
|
---|
428 | if (adminLevel != null) {
|
---|
429 | name += '['+adminLevel+']';
|
---|
430 | }
|
---|
431 |
|
---|
432 | for (NameFormatterHook hook: FORMAT_HOOKS) {
|
---|
433 | String hookResult = hook.checkRelationTypeName(relation, name);
|
---|
434 | if (hookResult != null)
|
---|
435 | return hookResult;
|
---|
436 | }
|
---|
437 |
|
---|
438 | return name;
|
---|
439 | }
|
---|
440 |
|
---|
441 | private static String getNameTagValue(IRelation relation, String nameTag) {
|
---|
442 | if ("name".equals(nameTag)) {
|
---|
443 | if (Main.pref.getBoolean("osm-primitives.localize-name", true))
|
---|
444 | return relation.getLocalName();
|
---|
445 | else
|
---|
446 | return relation.getName();
|
---|
447 | } else if (":LocationCode".equals(nameTag)) {
|
---|
448 | for (String m : relation.keySet()) {
|
---|
449 | if (m.endsWith(nameTag))
|
---|
450 | return relation.get(m);
|
---|
451 | }
|
---|
452 | return null;
|
---|
453 | } else if (nameTag.startsWith("?") && OsmUtils.isTrue(relation.get(nameTag.substring(1)))) {
|
---|
454 | return tr(nameTag.substring(1));
|
---|
455 | } else if (nameTag.startsWith("?") && OsmUtils.isFalse(relation.get(nameTag.substring(1)))) {
|
---|
456 | return null;
|
---|
457 | } else if (nameTag.startsWith("?")) {
|
---|
458 | return trcLazy(nameTag, I18n.escape(relation.get(nameTag.substring(1))));
|
---|
459 | } else {
|
---|
460 | return trcLazy(nameTag, I18n.escape(relation.get(nameTag)));
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 | private static String getRelationName(IRelation relation) {
|
---|
465 | String nameTag;
|
---|
466 | for (String n : getNamingtagsForRelations()) {
|
---|
467 | nameTag = getNameTagValue(relation, n);
|
---|
468 | if (nameTag != null)
|
---|
469 | return nameTag;
|
---|
470 | }
|
---|
471 | return null;
|
---|
472 | }
|
---|
473 |
|
---|
474 | @Override
|
---|
475 | public String format(Changeset changeset) {
|
---|
476 | return tr("Changeset {0}", changeset.getId());
|
---|
477 | }
|
---|
478 |
|
---|
479 | /**
|
---|
480 | * Builds a default tooltip text for the primitive <code>primitive</code>.
|
---|
481 | *
|
---|
482 | * @param primitive the primitmive
|
---|
483 | * @return the tooltip text
|
---|
484 | */
|
---|
485 | public String buildDefaultToolTip(IPrimitive primitive) {
|
---|
486 | return buildDefaultToolTip(primitive.getId(), primitive.getKeys());
|
---|
487 | }
|
---|
488 |
|
---|
489 | private static String buildDefaultToolTip(long id, Map<String, String> tags) {
|
---|
490 | StringBuilder sb = new StringBuilder(128);
|
---|
491 | sb.append("<html><strong>id</strong>=")
|
---|
492 | .append(id)
|
---|
493 | .append("<br>");
|
---|
494 | List<String> keyList = new ArrayList<>(tags.keySet());
|
---|
495 | Collections.sort(keyList);
|
---|
496 | for (int i = 0; i < keyList.size(); i++) {
|
---|
497 | if (i > 0) {
|
---|
498 | sb.append("<br>");
|
---|
499 | }
|
---|
500 | String key = keyList.get(i);
|
---|
501 | sb.append("<strong>")
|
---|
502 | .append(key)
|
---|
503 | .append("</strong>=");
|
---|
504 | String value = tags.get(key);
|
---|
505 | while (!value.isEmpty()) {
|
---|
506 | sb.append(value.substring(0, Math.min(50, value.length())));
|
---|
507 | if (value.length() > 50) {
|
---|
508 | sb.append("<br>");
|
---|
509 | value = value.substring(50);
|
---|
510 | } else {
|
---|
511 | value = "";
|
---|
512 | }
|
---|
513 | }
|
---|
514 | }
|
---|
515 | sb.append("</html>");
|
---|
516 | return sb.toString();
|
---|
517 | }
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * Decorates the name of primitive with its id, if the preference
|
---|
521 | * <tt>osm-primitives.showid</tt> is set.
|
---|
522 | *
|
---|
523 | * The id is append to the {@link StringBuilder} passed in <code>name</code>.
|
---|
524 | *
|
---|
525 | * @param name the name without the id
|
---|
526 | * @param primitive the primitive
|
---|
527 | */
|
---|
528 | protected void decorateNameWithId(StringBuilder name, HistoryOsmPrimitive primitive) {
|
---|
529 | if (Main.pref.getBoolean("osm-primitives.showid")) {
|
---|
530 | name.append(tr(" [id: {0}]", primitive.getId()));
|
---|
531 | }
|
---|
532 | }
|
---|
533 |
|
---|
534 | @Override
|
---|
535 | public String format(HistoryNode node) {
|
---|
536 | StringBuilder sb = new StringBuilder();
|
---|
537 | String name;
|
---|
538 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
---|
539 | name = node.getLocalName();
|
---|
540 | } else {
|
---|
541 | name = node.getName();
|
---|
542 | }
|
---|
543 | if (name == null) {
|
---|
544 | sb.append(node.getId());
|
---|
545 | } else {
|
---|
546 | sb.append(name);
|
---|
547 | }
|
---|
548 | LatLon coord = node.getCoords();
|
---|
549 | if (coord != null) {
|
---|
550 | sb.append(" (")
|
---|
551 | .append(coord.latToString(CoordinateFormat.getDefaultFormat()))
|
---|
552 | .append(", ")
|
---|
553 | .append(coord.lonToString(CoordinateFormat.getDefaultFormat()))
|
---|
554 | .append(')');
|
---|
555 | }
|
---|
556 | decorateNameWithId(sb, node);
|
---|
557 | return sb.toString();
|
---|
558 | }
|
---|
559 |
|
---|
560 | @Override
|
---|
561 | public String format(HistoryWay way) {
|
---|
562 | StringBuilder sb = new StringBuilder();
|
---|
563 | String name;
|
---|
564 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
---|
565 | name = way.getLocalName();
|
---|
566 | } else {
|
---|
567 | name = way.getName();
|
---|
568 | }
|
---|
569 | if (name != null) {
|
---|
570 | sb.append(name);
|
---|
571 | }
|
---|
572 | if (sb.length() == 0 && way.get("ref") != null) {
|
---|
573 | sb.append(way.get("ref"));
|
---|
574 | }
|
---|
575 | if (sb.length() == 0) {
|
---|
576 | sb.append(
|
---|
577 | way.hasKey("highway") ? tr("highway") :
|
---|
578 | way.hasKey("railway") ? tr("railway") :
|
---|
579 | way.hasKey("waterway") ? tr("waterway") :
|
---|
580 | way.hasKey("landuse") ? tr("landuse") : ""
|
---|
581 | );
|
---|
582 | }
|
---|
583 |
|
---|
584 | int nodesNo = way.isClosed() ? (way.getNumNodes() -1) : way.getNumNodes();
|
---|
585 | String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
|
---|
586 | if (sb.length() == 0) {
|
---|
587 | sb.append(way.getId());
|
---|
588 | }
|
---|
589 | /* note: length == 0 should no longer happen, but leave the bracket code
|
---|
590 | nevertheless, who knows what future brings */
|
---|
591 | sb.append((sb.length() > 0) ? (" ("+nodes+')') : nodes);
|
---|
592 | decorateNameWithId(sb, way);
|
---|
593 | return sb.toString();
|
---|
594 | }
|
---|
595 |
|
---|
596 | @Override
|
---|
597 | public String format(HistoryRelation relation) {
|
---|
598 | StringBuilder sb = new StringBuilder();
|
---|
599 | String type = relation.get("type");
|
---|
600 | if (type != null) {
|
---|
601 | sb.append(type);
|
---|
602 | } else {
|
---|
603 | sb.append(tr("relation"));
|
---|
604 | }
|
---|
605 | sb.append(" (");
|
---|
606 | String nameTag = null;
|
---|
607 | Set<String> namingTags = new HashSet<>(getNamingtagsForRelations());
|
---|
608 | for (String n : relation.getTags().keySet()) {
|
---|
609 | // #3328: "note " and " note" are name tags too
|
---|
610 | if (namingTags.contains(n.trim())) {
|
---|
611 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
---|
612 | nameTag = relation.getLocalName();
|
---|
613 | } else {
|
---|
614 | nameTag = relation.getName();
|
---|
615 | }
|
---|
616 | if (nameTag == null) {
|
---|
617 | nameTag = relation.get(n);
|
---|
618 | }
|
---|
619 | }
|
---|
620 | if (nameTag != null) {
|
---|
621 | break;
|
---|
622 | }
|
---|
623 | }
|
---|
624 | if (nameTag == null) {
|
---|
625 | sb.append(Long.toString(relation.getId())).append(", ");
|
---|
626 | } else {
|
---|
627 | sb.append('\"').append(nameTag).append("\", ");
|
---|
628 | }
|
---|
629 |
|
---|
630 | int mbno = relation.getNumMembers();
|
---|
631 | sb.append(trn("{0} member", "{0} members", mbno, mbno)).append(')');
|
---|
632 |
|
---|
633 | decorateNameWithId(sb, relation);
|
---|
634 | return sb.toString();
|
---|
635 | }
|
---|
636 |
|
---|
637 | /**
|
---|
638 | * Builds a default tooltip text for an HistoryOsmPrimitive <code>primitive</code>.
|
---|
639 | *
|
---|
640 | * @param primitive the primitmive
|
---|
641 | * @return the tooltip text
|
---|
642 | */
|
---|
643 | public String buildDefaultToolTip(HistoryOsmPrimitive primitive) {
|
---|
644 | return buildDefaultToolTip(primitive.getId(), primitive.getTags());
|
---|
645 | }
|
---|
646 |
|
---|
647 | /**
|
---|
648 | * Formats the given collection of primitives as an HTML unordered list.
|
---|
649 | * @param primitives collection of primitives to format
|
---|
650 | * @param maxElements the maximum number of elements to display
|
---|
651 | * @return HTML unordered list
|
---|
652 | */
|
---|
653 | public String formatAsHtmlUnorderedList(Collection<? extends OsmPrimitive> primitives, int maxElements) {
|
---|
654 | Collection<String> displayNames = primitives.stream().map(x -> x.getDisplayName(this)).collect(Collectors.toList());
|
---|
655 | return Utils.joinAsHtmlUnorderedList(Utils.limit(displayNames, maxElements, "..."));
|
---|
656 | }
|
---|
657 |
|
---|
658 | /**
|
---|
659 | * Formats the given primitive as an HTML unordered list.
|
---|
660 | * @param primitive primitive to format
|
---|
661 | * @return HTML unordered list
|
---|
662 | */
|
---|
663 | public String formatAsHtmlUnorderedList(OsmPrimitive primitive) {
|
---|
664 | return formatAsHtmlUnorderedList(Collections.singletonList(primitive), 1);
|
---|
665 | }
|
---|
666 | }
|
---|