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

Last change on this file since 3271 was 3271, checked in by stoecker, 15 years ago

translate polygon names

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