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

Last change on this file since 2973 was 2973, checked in by jttt, 14 years ago

Fix #4519 JOSM doesn't show nor find token (negative) IDs

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