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

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

fixed line endings of recent checkins

File size: 13.7 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", "public_transport", ":LocationCode", "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 = (relation.get("public_transport") != null) ? tr("public transport") : "";
164 }
165 if (name == null) {
166 name = tr("relation");
167 }
168
169 name += " (";
170 String nameTag = null;
171 for (String n : getNamingtagsForRelations()) {
172 if (n.equals("name")) {
173 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
174 nameTag = relation.getLocalName();
175 } else {
176 nameTag = relation.getName();
177 }
178 } else if (n.equals(":LocationCode")) {
179 for (String m : relation.keySet()) {
180 if (m.endsWith(n)) {
181 nameTag = relation.get(m);
182 break;
183 }
184 }
185 } else {
186 nameTag = relation.get(n);
187 }
188 if (nameTag != null) {
189 break;
190 }
191 }
192 if (nameTag == null) {
193 name += Long.toString(relation.getId()) + ", ";
194 } else {
195 name += "\"" + nameTag + "\", ";
196 }
197
198 int mbno = relation.getMembersCount();
199 name += trn("{0} member", "{0} members", mbno, mbno) + ")";
200 }
201 name = decorateNameWithId(name, relation);
202 return name;
203 }
204
205 /**
206 * Formats a name for a changeset
207 *
208 * @param changeset the changeset
209 * @return the name
210 */
211 public String format(Changeset changeset) {
212 return tr("Changeset {0}",changeset.getId());
213 }
214
215 /**
216 * Builds a default tooltip text for the primitive <code>primitive</code>.
217 *
218 * @param primitive the primitmive
219 * @return the tooltip text
220 */
221 public String buildDefaultToolTip(OsmPrimitive primitive) {
222 StringBuilder sb = new StringBuilder();
223 sb.append("<html>");
224 sb.append("<strong>id</strong>=")
225 .append(primitive.getId())
226 .append("<br>");
227 ArrayList<String> keyList = new ArrayList<String>(primitive.keySet());
228 Collections.sort(keyList);
229 for (int i = 0; i < keyList.size(); i++) {
230 if (i > 0) {
231 sb.append("<br>");
232 }
233 String key = keyList.get(i);
234 sb.append("<strong>")
235 .append(key)
236 .append("</strong>")
237 .append("=");
238 String value = primitive.get(key);
239 while(value.length() != 0) {
240 sb.append(value.substring(0,Math.min(50, value.length())));
241 if (value.length() > 50) {
242 sb.append("<br>");
243 value = value.substring(50);
244 } else {
245 value = "";
246 }
247 }
248 }
249 sb.append("</html>");
250 return sb.toString();
251 }
252
253 /**
254 * Decorates the name of primitive with its id, if the preference
255 * <tt>osm-primitives.showid</tt> is set.
256 *
257 * The id is append to the {@see StringBuilder} passed in in <code>name</code>.
258 *
259 * @param name the name without the id
260 * @param primitive the primitive
261 */
262 protected void decorateNameWithId(StringBuilder name, HistoryOsmPrimitive primitive) {
263 if (Main.pref.getBoolean("osm-primitives.showid")) {
264 name.append(tr(" [id: {0}]", primitive.getId()));
265 }
266 }
267
268 /**
269 * Formats a name for a history node
270 *
271 * @param node the node
272 * @return the name
273 */
274 public String format(HistoryNode node) {
275 StringBuilder sb = new StringBuilder();
276 String name;
277 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
278 name = node.getLocalName();
279 } else {
280 name = node.getName();
281 }
282 if (name == null) {
283 sb.append(node.getId());
284 } else {
285 sb.append(name);
286 }
287 sb.append(" (")
288 .append(node.getCoords().latToString(CoordinateFormat.getDefaultFormat()))
289 .append(", ")
290 .append(node.getCoords().lonToString(CoordinateFormat.getDefaultFormat()))
291 .append(")");
292 decorateNameWithId(sb, node);
293 return sb.toString();
294 }
295
296 /**
297 * Formats a name for a way
298 *
299 * @param way the way
300 * @return the name
301 */
302 public String format(HistoryWay way) {
303 StringBuilder sb = new StringBuilder();
304 String name;
305 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
306 name = way.getLocalName();
307 } else {
308 name = way.getName();
309 }
310 if (name != null) {
311 sb.append(name);
312 }
313 if (sb.length() == 0 && way.get("ref") != null) {
314 sb.append(way.get("ref"));
315 }
316 if (sb.length() == 0) {
317 sb.append(
318 (way.get("highway") != null) ? tr("highway") :
319 (way.get("railway") != null) ? tr("railway") :
320 (way.get("waterway") != null) ? tr("waterway") :
321 (way.get("landuse") != null) ? tr("landuse") : ""
322 );
323 }
324
325 int nodesNo = way.isClosed() ? way.getNumNodes() -1 : way.getNumNodes();
326 String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
327 sb.append((sb.length() > 0) ? " ("+nodes+")" : nodes);
328 decorateNameWithId(sb, way);
329 return sb.toString();
330 }
331
332 /**
333 * Formats a name for a {@see HistoryRelation})
334 *
335 * @param relation the relation
336 * @return the name
337 */
338 public String format(HistoryRelation relation) {
339 StringBuilder sb = new StringBuilder();
340 if (relation.get("type") != null) {
341 sb.append(relation.get("type"));
342 } else {
343 sb.append(tr("relation"));
344 }
345 sb.append(" (");
346 String nameTag = null;
347 Set<String> namingTags = new HashSet<String>(getNamingtagsForRelations());
348 for (String n : relation.getTags().keySet()) {
349 // #3328: "note " and " note" are name tags too
350 if (namingTags.contains(n.trim())) {
351 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
352 nameTag = relation.getLocalName();
353 } else {
354 nameTag = relation.getName();
355 }
356 if (nameTag == null) {
357 nameTag = relation.get(n);
358 }
359 }
360 if (nameTag != null) {
361 break;
362 }
363 }
364 if (nameTag == null) {
365 sb.append(Long.toString(relation.getId())).append(", ");
366 } else {
367 sb.append("\"").append(nameTag).append("\", ");
368 }
369
370 int mbno = relation.getNumMembers();
371 sb.append(trn("{0} member", "{0} members", mbno, mbno)).append(")");
372
373 decorateNameWithId(sb, relation);
374 return sb.toString();
375 }
376
377 /**
378 * Builds a default tooltip text for an HistoryOsmPrimitive <code>primitive</code>.
379 *
380 * @param primitive the primitmive
381 * @return the tooltip text
382 */
383 public String buildDefaultToolTip(HistoryOsmPrimitive primitive) {
384 StringBuilder sb = new StringBuilder();
385 sb.append("<html>");
386 sb.append("<strong>id</strong>=")
387 .append(primitive.getId())
388 .append("<br>");
389 ArrayList<String> keyList = new ArrayList<String>(primitive.getTags().keySet());
390 Collections.sort(keyList);
391 for (int i = 0; i < keyList.size(); i++) {
392 if (i > 0) {
393 sb.append("<br>");
394 }
395 String key = keyList.get(i);
396 sb.append("<strong>")
397 .append(key)
398 .append("</strong>")
399 .append("=");
400 String value = primitive.get(key);
401 while(value.length() != 0) {
402 sb.append(value.substring(0,Math.min(50, value.length())));
403 if (value.length() > 50) {
404 sb.append("<br>");
405 value = value.substring(50);
406 } else {
407 value = "";
408 }
409 }
410 }
411 sb.append("</html>");
412 return sb.toString();
413 }
414}
Note: See TracBrowser for help on using the repository browser.