source: josm/trunk/src/org/openstreetmap/josm/gui/DefaultNameFormatter.java@ 3887

Last change on this file since 3887 was 3887, checked in by stoecker, 14 years ago

fix #5932

  • Property svn:eol-style set to native
File size: 17.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trc;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Collections;
11import java.util.HashSet;
12import java.util.List;
13import java.util.Set;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.coor.CoordinateFormat;
17import org.openstreetmap.josm.data.osm.Changeset;
18import org.openstreetmap.josm.data.osm.NameFormatter;
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.OsmUtils;
22import org.openstreetmap.josm.data.osm.Relation;
23import org.openstreetmap.josm.data.osm.RelationMember;
24import org.openstreetmap.josm.data.osm.Way;
25import org.openstreetmap.josm.data.osm.history.HistoryNameFormatter;
26import org.openstreetmap.josm.data.osm.history.HistoryNode;
27import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
28import org.openstreetmap.josm.data.osm.history.HistoryRelation;
29import org.openstreetmap.josm.data.osm.history.HistoryWay;
30
31/**
32 * This is the default implementation of a {@see NameFormatter} for names of {@see OsmPrimitive}s.
33 *
34 */
35public class DefaultNameFormatter implements NameFormatter, HistoryNameFormatter {
36
37 static private DefaultNameFormatter instance;
38
39 /**
40 * Replies the unique instance of this formatter
41 *
42 * @return the unique instance of this formatter
43 */
44 static public DefaultNameFormatter getInstance() {
45 if (instance == null) {
46 instance = new DefaultNameFormatter();
47 }
48 return instance;
49 }
50
51 /** the default list of tags which are used as naming tags in relations */
52 static public final String[] DEFAULT_NAMING_TAGS_FOR_RELATIONS = {"name", "ref", "restriction", "landuse", "natural",
53 "public_transport", ":LocationCode", "note"};
54
55 /** the current list of tags used as naming tags in relations */
56 static private List<String> namingTagsForRelations = null;
57
58 /**
59 * Replies the list of naming tags used in relations. The list is given (in this order) by:
60 * <ul>
61 * <li>by the tag names in the preference <tt>relation.nameOrder</tt></li>
62 * <li>by the default tags in {@see #DEFAULT_NAMING_TAGS_FOR_RELATIONS}
63 * </ul>
64 *
65 * @return the list of naming tags used in relations
66 */
67 static public List<String> getNamingtagsForRelations() {
68 if (namingTagsForRelations == null) {
69 namingTagsForRelations = new ArrayList<String>(
70 Main.pref.getCollection("relation.nameOrder", Arrays.asList(DEFAULT_NAMING_TAGS_FOR_RELATIONS))
71 );
72 }
73 return namingTagsForRelations;
74 }
75
76 /**
77 * Decorates the name of primitive with its id, if the preference
78 * <tt>osm-primitives.showid</tt> is set. Shows unique id if osm-primitives.showid.new-primitives is set
79 *
80 * @param name the name without the id
81 * @param primitive the primitive
82 * @return the decorated name
83 */
84 protected String decorateNameWithId(String name, OsmPrimitive primitive) {
85 if (Main.pref.getBoolean("osm-primitives.showid"))
86 if (Main.pref.getBoolean("osm-primitives.showid.new-primitives"))
87 return name + tr(" [id: {0}]", primitive.getUniqueId());
88 else
89 return name + tr(" [id: {0}]", primitive.getId());
90 else
91 return name;
92 }
93
94 /**
95 * Formats a name for a node
96 *
97 * @param node the node
98 * @return the name
99 */
100 public String format(Node node) {
101 String name = "";
102 if (node.isIncomplete()) {
103 name = tr("incomplete");
104 } else {
105 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
106 name = node.getLocalName();
107 } else {
108 name = node.getName();
109 }
110 if(name == null)
111 {
112 String s;
113 if((s = node.get("addr:housename")) != null) {
114 /* I18n: name of house as parameter */
115 name = tr("House {0}", s);
116 }
117 if(name == null && (s = node.get("addr:housenumber")) != null) {
118 String t = node.get("addr:street");
119 if(t != null) {
120 /* I18n: house number, street as parameter, number should remain
121 before street for better visibility */
122 name = tr("House number {0} at {1}", s, t);
123 }
124 else {
125 /* I18n: house number as parameter */
126 name = tr("House number {0}", s);
127 }
128 }
129 }
130
131 if (name == null) {
132 name = node.isNew() ? tr("node") : ""+ node.getId();
133 }
134 name += " (" + node.getCoor().latToString(CoordinateFormat.getDefaultFormat()) + ", " + node.getCoor().lonToString(CoordinateFormat.getDefaultFormat()) + ")";
135 }
136 name = decorateNameWithId(name, node);
137 return name;
138 }
139
140 /**
141 * Formats a name for a way
142 *
143 * @param way the way
144 * @return the name
145 */
146 public String format(Way way) {
147 String name = "";
148 if (way.isIncomplete()) {
149 name = tr("incomplete");
150 } else {
151 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
152 name = way.getLocalName();
153 } else {
154 name = way.getName();
155 }
156 if (name == null) {
157 name = way.get("ref");
158 }
159 if (name == null) {
160 name =
161 (way.get("highway") != null) ? tr("highway") :
162 (way.get("railway") != null) ? tr("railway") :
163 (way.get("waterway") != null) ? tr("waterway") :
164 (way.get("landuse") != null) ? tr("landuse") : null;
165 }
166 if(name == null)
167 {
168 String s;
169 if((s = way.get("addr:housename")) != null) {
170 /* I18n: name of house as parameter */
171 name = tr("House {0}", s);
172 }
173 if(name == null && (s = way.get("addr:housenumber")) != null) {
174 String t = way.get("addr:street");
175 if(t != null) {
176 /* I18n: house number, street as parameter, number should remain
177 before street for better visibility */
178 name = tr("House number {0} at {1}", s, t);
179 }
180 else {
181 /* I18n: house number as parameter */
182 name = tr("House number {0}", s);
183 }
184 }
185 }
186
187 int nodesNo = way.getNodesCount();
188 if (nodesNo > 1 && way.isClosed()) {
189 nodesNo--;
190 }
191 if(name == null || name.length() == 0)
192 name = String.valueOf(way.getId());
193 /* note: length == 0 should no longer happen, but leave the bracket code
194 nevertheless, who knows what future brings */
195 /* I18n: count of nodes as parameter */
196 String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
197 name += (name.length() > 0) ? " ("+nodes+")" : nodes;
198 }
199 name = decorateNameWithId(name, way);
200 return name;
201 }
202
203 /**
204 * Formats a name for a relation
205 *
206 * @param relation the relation
207 * @return the name
208 */
209 public String format(Relation relation) {
210 String name;
211 if (relation.isIncomplete()) {
212 name = tr("incomplete");
213 } else {
214 name = trc("Relation type", relation.get("type"));
215 if (name == null) {
216 name = (relation.get("public_transport") != null) ? tr("public transport") : "";
217 }
218 if (name == null) {
219 String building = relation.get("building");
220 if(OsmUtils.isTrue(building))
221 name = tr("building");
222 else if(building != null)
223 name = tr(building); // translate tag!
224 }
225 if (name == null) {
226 name = tr("relation");
227 }
228 String admin_level = relation.get("admin_level");
229 if (admin_level != null) {
230 name += "["+admin_level+"]";
231 }
232
233 name += " (";
234 String nameTag = null;
235 for (String n : getNamingtagsForRelations()) {
236 if (n.equals("name")) {
237 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
238 nameTag = relation.getLocalName();
239 } else {
240 nameTag = relation.getName();
241 }
242 } else if (n.equals(":LocationCode")) {
243 for (String m : relation.keySet()) {
244 if (m.endsWith(n)) {
245 nameTag = relation.get(m);
246 break;
247 }
248 }
249 } else {
250 String str = relation.get(n);
251 if(str != null)
252 nameTag = tr(str);
253 }
254 if (nameTag != null) {
255 break;
256 }
257 }
258 if (nameTag == null) {
259 name += Long.toString(relation.getId()) + ", ";
260 } else {
261 name += "\"" + nameTag + "\", ";
262 }
263
264 int mbno = relation.getMembersCount();
265 name += trn("{0} member", "{0} members", mbno, mbno);
266
267 boolean incomplete = false;
268 for (RelationMember m : relation.getMembers()) {
269 if (m.getMember().isIncomplete()) {
270 incomplete = true;
271 break;
272 }
273 }
274 if (incomplete) {
275 name += ", "+tr("incomplete");
276 }
277
278 name += ")";
279 }
280 name = decorateNameWithId(name, relation);
281 return name;
282 }
283
284 /**
285 * Formats a name for a changeset
286 *
287 * @param changeset the changeset
288 * @return the name
289 */
290 public String format(Changeset changeset) {
291 return tr("Changeset {0}",changeset.getId());
292 }
293
294 /**
295 * Builds a default tooltip text for the primitive <code>primitive</code>.
296 *
297 * @param primitive the primitmive
298 * @return the tooltip text
299 */
300 public String buildDefaultToolTip(OsmPrimitive primitive) {
301 StringBuilder sb = new StringBuilder();
302 sb.append("<html>");
303 sb.append("<strong>id</strong>=")
304 .append(primitive.getId())
305 .append("<br>");
306 ArrayList<String> keyList = new ArrayList<String>(primitive.keySet());
307 Collections.sort(keyList);
308 for (int i = 0; i < keyList.size(); i++) {
309 if (i > 0) {
310 sb.append("<br>");
311 }
312 String key = keyList.get(i);
313 sb.append("<strong>")
314 .append(key)
315 .append("</strong>")
316 .append("=");
317 String value = primitive.get(key);
318 while(value.length() != 0) {
319 sb.append(value.substring(0,Math.min(50, value.length())));
320 if (value.length() > 50) {
321 sb.append("<br>");
322 value = value.substring(50);
323 } else {
324 value = "";
325 }
326 }
327 }
328 sb.append("</html>");
329 return sb.toString();
330 }
331
332 /**
333 * Decorates the name of primitive with its id, if the preference
334 * <tt>osm-primitives.showid</tt> is set.
335 *
336 * The id is append to the {@see StringBuilder} passed in in <code>name</code>.
337 *
338 * @param name the name without the id
339 * @param primitive the primitive
340 */
341 protected void decorateNameWithId(StringBuilder name, HistoryOsmPrimitive primitive) {
342 if (Main.pref.getBoolean("osm-primitives.showid")) {
343 name.append(tr(" [id: {0}]", primitive.getId()));
344 }
345 }
346
347 /**
348 * Formats a name for a history node
349 *
350 * @param node the node
351 * @return the name
352 */
353 public String format(HistoryNode node) {
354 StringBuilder sb = new StringBuilder();
355 String name;
356 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
357 name = node.getLocalName();
358 } else {
359 name = node.getName();
360 }
361 if (name == null) {
362 sb.append(node.getId());
363 } else {
364 sb.append(name);
365 }
366 sb.append(" (")
367 .append(node.getCoords().latToString(CoordinateFormat.getDefaultFormat()))
368 .append(", ")
369 .append(node.getCoords().lonToString(CoordinateFormat.getDefaultFormat()))
370 .append(")");
371 decorateNameWithId(sb, node);
372 return sb.toString();
373 }
374
375 /**
376 * Formats a name for a way
377 *
378 * @param way the way
379 * @return the name
380 */
381 public String format(HistoryWay way) {
382 StringBuilder sb = new StringBuilder();
383 String name;
384 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
385 name = way.getLocalName();
386 } else {
387 name = way.getName();
388 }
389 if (name != null) {
390 sb.append(name);
391 }
392 if (sb.length() == 0 && way.get("ref") != null) {
393 sb.append(way.get("ref"));
394 }
395 if (sb.length() == 0) {
396 sb.append(
397 (way.get("highway") != null) ? tr("highway") :
398 (way.get("railway") != null) ? tr("railway") :
399 (way.get("waterway") != null) ? tr("waterway") :
400 (way.get("landuse") != null) ? tr("landuse") : ""
401 );
402 }
403
404 int nodesNo = way.isClosed() ? way.getNumNodes() -1 : way.getNumNodes();
405 String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
406 if(sb.length() == 0 )
407 sb.append(way.getId());
408 /* note: length == 0 should no longer happen, but leave the bracket code
409 nevertheless, who knows what future brings */
410 sb.append((sb.length() > 0) ? " ("+nodes+")" : nodes);
411 decorateNameWithId(sb, way);
412 return sb.toString();
413 }
414
415 /**
416 * Formats a name for a {@see HistoryRelation})
417 *
418 * @param relation the relation
419 * @return the name
420 */
421 public String format(HistoryRelation relation) {
422 StringBuilder sb = new StringBuilder();
423 if (relation.get("type") != null) {
424 sb.append(relation.get("type"));
425 } else {
426 sb.append(tr("relation"));
427 }
428 sb.append(" (");
429 String nameTag = null;
430 Set<String> namingTags = new HashSet<String>(getNamingtagsForRelations());
431 for (String n : relation.getTags().keySet()) {
432 // #3328: "note " and " note" are name tags too
433 if (namingTags.contains(n.trim())) {
434 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
435 nameTag = relation.getLocalName();
436 } else {
437 nameTag = relation.getName();
438 }
439 if (nameTag == null) {
440 nameTag = relation.get(n);
441 }
442 }
443 if (nameTag != null) {
444 break;
445 }
446 }
447 if (nameTag == null) {
448 sb.append(Long.toString(relation.getId())).append(", ");
449 } else {
450 sb.append("\"").append(nameTag).append("\", ");
451 }
452
453 int mbno = relation.getNumMembers();
454 sb.append(trn("{0} member", "{0} members", mbno, mbno)).append(")");
455
456 decorateNameWithId(sb, relation);
457 return sb.toString();
458 }
459
460 /**
461 * Builds a default tooltip text for an HistoryOsmPrimitive <code>primitive</code>.
462 *
463 * @param primitive the primitmive
464 * @return the tooltip text
465 */
466 public String buildDefaultToolTip(HistoryOsmPrimitive primitive) {
467 StringBuilder sb = new StringBuilder();
468 sb.append("<html>");
469 sb.append("<strong>id</strong>=")
470 .append(primitive.getId())
471 .append("<br>");
472 ArrayList<String> keyList = new ArrayList<String>(primitive.getTags().keySet());
473 Collections.sort(keyList);
474 for (int i = 0; i < keyList.size(); i++) {
475 if (i > 0) {
476 sb.append("<br>");
477 }
478 String key = keyList.get(i);
479 sb.append("<strong>")
480 .append(key)
481 .append("</strong>")
482 .append("=");
483 String value = primitive.get(key);
484 while(value.length() != 0) {
485 sb.append(value.substring(0,Math.min(50, value.length())));
486 if (value.length() > 50) {
487 sb.append("<br>");
488 value = value.substring(50);
489 } else {
490 value = "";
491 }
492 }
493 }
494 sb.append("</html>");
495 return sb.toString();
496 }
497}
Note: See TracBrowser for help on using the repository browser.