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