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

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

fix bad line endings

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