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