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

Last change on this file since 2885 was 2885, checked in by bastiK, 15 years ago

Show in relation list if a relation has incomplete members.
Remove tooltips in relation list (fixes #3639, fixes #4227).

File size: 14.1 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.
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 return name + tr(" [id: {0}]", primitive.getId());
84 else
85 return name;
86 }
87
88 /**
89 * Formats a name for a node
90 *
91 * @param node the node
92 * @return the name
93 */
94 public String format(Node node) {
95 String name = "";
96 if (node.isIncomplete()) {
97 name = tr("incomplete");
98 } else {
99 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
100 name = node.getLocalName();
101 } else {
102 name = node.getName();
103 }
104 if (name == null) {
105 name = node.isNew() ? tr("node") : ""+ node.getId();
106 }
107 name += " (" + node.getCoor().latToString(CoordinateFormat.getDefaultFormat()) + ", " + node.getCoor().lonToString(CoordinateFormat.getDefaultFormat()) + ")";
108 }
109 name = decorateNameWithId(name, node);
110 return name;
111 }
112
113 /**
114 * Formats a name for a way
115 *
116 * @param way the way
117 * @return the name
118 */
119 public String format(Way way) {
120 String name = "";
121 if (way.isIncomplete()) {
122 name = tr("incomplete");
123 } else {
124 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
125 name = way.getLocalName();
126 } else {
127 name = way.getName();
128 }
129 if (name == null) {
130 name = way.get("ref");
131 }
132 if (name == null) {
133 name =
134 (way.get("highway") != null) ? tr("highway") :
135 (way.get("railway") != null) ? tr("railway") :
136 (way.get("waterway") != null) ? tr("waterway") :
137 (way.get("landuse") != null) ? tr("landuse") : "";
138 }
139
140 int nodesNo = way.getNodesCount();
141 if (nodesNo > 1 && way.isClosed()) {
142 nodesNo--;
143 }
144 String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
145 name += (name.length() > 0) ? " ("+nodes+")" : nodes;
146 }
147 name = decorateNameWithId(name, way);
148 return name;
149 }
150
151 /**
152 * Formats a name for a relation
153 *
154 * @param relation the relation
155 * @return the name
156 */
157 public String format(Relation relation) {
158 String name;
159 if (relation.isIncomplete()) {
160 name = tr("incomplete");
161 } else {
162 name = relation.get("type");
163 if (name == null) {
164 name = (relation.get("public_transport") != null) ? tr("public transport") : "";
165 }
166 if (name == null) {
167 name = tr("relation");
168 }
169
170 name += " (";
171 String nameTag = null;
172 for (String n : getNamingtagsForRelations()) {
173 if (n.equals("name")) {
174 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
175 nameTag = relation.getLocalName();
176 } else {
177 nameTag = relation.getName();
178 }
179 } else if (n.equals(":LocationCode")) {
180 for (String m : relation.keySet()) {
181 if (m.endsWith(n)) {
182 nameTag = relation.get(m);
183 break;
184 }
185 }
186 } else {
187 nameTag = relation.get(n);
188 }
189 if (nameTag != null) {
190 break;
191 }
192 }
193 if (nameTag == null) {
194 name += Long.toString(relation.getId()) + ", ";
195 } else {
196 name += "\"" + nameTag + "\", ";
197 }
198
199 int mbno = relation.getMembersCount();
200 name += trn("{0} member", "{0} members", mbno, mbno);
201
202 boolean incomplete = false;
203 for (RelationMember m : relation.getMembers()) {
204 if (m.getMember().isIncomplete()) {
205 incomplete = true;
206 break;
207 }
208 }
209 if (incomplete) {
210 name += ", "+tr("incomplete");
211 }
212
213 name += ")";
214 }
215 name = decorateNameWithId(name, relation);
216 return name;
217 }
218
219 /**
220 * Formats a name for a changeset
221 *
222 * @param changeset the changeset
223 * @return the name
224 */
225 public String format(Changeset changeset) {
226 return tr("Changeset {0}",changeset.getId());
227 }
228
229 /**
230 * Builds a default tooltip text for the primitive <code>primitive</code>.
231 *
232 * @param primitive the primitmive
233 * @return the tooltip text
234 */
235 public String buildDefaultToolTip(OsmPrimitive primitive) {
236 StringBuilder sb = new StringBuilder();
237 sb.append("<html>");
238 sb.append("<strong>id</strong>=")
239 .append(primitive.getId())
240 .append("<br>");
241 ArrayList<String> keyList = new ArrayList<String>(primitive.keySet());
242 Collections.sort(keyList);
243 for (int i = 0; i < keyList.size(); i++) {
244 if (i > 0) {
245 sb.append("<br>");
246 }
247 String key = keyList.get(i);
248 sb.append("<strong>")
249 .append(key)
250 .append("</strong>")
251 .append("=");
252 String value = primitive.get(key);
253 while(value.length() != 0) {
254 sb.append(value.substring(0,Math.min(50, value.length())));
255 if (value.length() > 50) {
256 sb.append("<br>");
257 value = value.substring(50);
258 } else {
259 value = "";
260 }
261 }
262 }
263 sb.append("</html>");
264 return sb.toString();
265 }
266
267 /**
268 * Decorates the name of primitive with its id, if the preference
269 * <tt>osm-primitives.showid</tt> is set.
270 *
271 * The id is append to the {@see StringBuilder} passed in in <code>name</code>.
272 *
273 * @param name the name without the id
274 * @param primitive the primitive
275 */
276 protected void decorateNameWithId(StringBuilder name, HistoryOsmPrimitive primitive) {
277 if (Main.pref.getBoolean("osm-primitives.showid")) {
278 name.append(tr(" [id: {0}]", primitive.getId()));
279 }
280 }
281
282 /**
283 * Formats a name for a history node
284 *
285 * @param node the node
286 * @return the name
287 */
288 public String format(HistoryNode node) {
289 StringBuilder sb = new StringBuilder();
290 String name;
291 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
292 name = node.getLocalName();
293 } else {
294 name = node.getName();
295 }
296 if (name == null) {
297 sb.append(node.getId());
298 } else {
299 sb.append(name);
300 }
301 sb.append(" (")
302 .append(node.getCoords().latToString(CoordinateFormat.getDefaultFormat()))
303 .append(", ")
304 .append(node.getCoords().lonToString(CoordinateFormat.getDefaultFormat()))
305 .append(")");
306 decorateNameWithId(sb, node);
307 return sb.toString();
308 }
309
310 /**
311 * Formats a name for a way
312 *
313 * @param way the way
314 * @return the name
315 */
316 public String format(HistoryWay way) {
317 StringBuilder sb = new StringBuilder();
318 String name;
319 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
320 name = way.getLocalName();
321 } else {
322 name = way.getName();
323 }
324 if (name != null) {
325 sb.append(name);
326 }
327 if (sb.length() == 0 && way.get("ref") != null) {
328 sb.append(way.get("ref"));
329 }
330 if (sb.length() == 0) {
331 sb.append(
332 (way.get("highway") != null) ? tr("highway") :
333 (way.get("railway") != null) ? tr("railway") :
334 (way.get("waterway") != null) ? tr("waterway") :
335 (way.get("landuse") != null) ? tr("landuse") : ""
336 );
337 }
338
339 int nodesNo = way.isClosed() ? way.getNumNodes() -1 : way.getNumNodes();
340 String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
341 sb.append((sb.length() > 0) ? " ("+nodes+")" : nodes);
342 decorateNameWithId(sb, way);
343 return sb.toString();
344 }
345
346 /**
347 * Formats a name for a {@see HistoryRelation})
348 *
349 * @param relation the relation
350 * @return the name
351 */
352 public String format(HistoryRelation relation) {
353 StringBuilder sb = new StringBuilder();
354 if (relation.get("type") != null) {
355 sb.append(relation.get("type"));
356 } else {
357 sb.append(tr("relation"));
358 }
359 sb.append(" (");
360 String nameTag = null;
361 Set<String> namingTags = new HashSet<String>(getNamingtagsForRelations());
362 for (String n : relation.getTags().keySet()) {
363 // #3328: "note " and " note" are name tags too
364 if (namingTags.contains(n.trim())) {
365 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
366 nameTag = relation.getLocalName();
367 } else {
368 nameTag = relation.getName();
369 }
370 if (nameTag == null) {
371 nameTag = relation.get(n);
372 }
373 }
374 if (nameTag != null) {
375 break;
376 }
377 }
378 if (nameTag == null) {
379 sb.append(Long.toString(relation.getId())).append(", ");
380 } else {
381 sb.append("\"").append(nameTag).append("\", ");
382 }
383
384 int mbno = relation.getNumMembers();
385 sb.append(trn("{0} member", "{0} members", mbno, mbno)).append(")");
386
387 decorateNameWithId(sb, relation);
388 return sb.toString();
389 }
390
391 /**
392 * Builds a default tooltip text for an HistoryOsmPrimitive <code>primitive</code>.
393 *
394 * @param primitive the primitmive
395 * @return the tooltip text
396 */
397 public String buildDefaultToolTip(HistoryOsmPrimitive primitive) {
398 StringBuilder sb = new StringBuilder();
399 sb.append("<html>");
400 sb.append("<strong>id</strong>=")
401 .append(primitive.getId())
402 .append("<br>");
403 ArrayList<String> keyList = new ArrayList<String>(primitive.getTags().keySet());
404 Collections.sort(keyList);
405 for (int i = 0; i < keyList.size(); i++) {
406 if (i > 0) {
407 sb.append("<br>");
408 }
409 String key = keyList.get(i);
410 sb.append("<strong>")
411 .append(key)
412 .append("</strong>")
413 .append("=");
414 String value = primitive.get(key);
415 while(value.length() != 0) {
416 sb.append(value.substring(0,Math.min(50, value.length())));
417 if (value.length() > 50) {
418 sb.append("<br>");
419 value = value.substring(50);
420 } else {
421 value = "";
422 }
423 }
424 }
425 sb.append("</html>");
426 return sb.toString();
427 }
428}
Note: See TracBrowser for help on using the repository browser.