| 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.Way;
|
|---|
| 22 | import org.openstreetmap.josm.data.osm.history.HistoryNameFormatter;
|
|---|
| 23 | import org.openstreetmap.josm.data.osm.history.HistoryNode;
|
|---|
| 24 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
|---|
| 25 | import org.openstreetmap.josm.data.osm.history.HistoryRelation;
|
|---|
| 26 | import 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 | */
|
|---|
| 32 | public 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 | /**
|
|---|
| 248 | * Decorates the name of primitive with its id, if the preference
|
|---|
| 249 | * <tt>osm-primitives.showid</tt> is set.
|
|---|
| 250 | *
|
|---|
| 251 | * The id is append to the {@see StringBuilder} passed in in <code>name</code>.
|
|---|
| 252 | *
|
|---|
| 253 | * @param name the name without the id
|
|---|
| 254 | * @param primitive the primitive
|
|---|
| 255 | */
|
|---|
| 256 | protected void decorateNameWithId(StringBuilder name, HistoryOsmPrimitive primitive) {
|
|---|
| 257 | if (Main.pref.getBoolean("osm-primitives.showid")) {
|
|---|
| 258 | name.append(tr(" [id: {0}]", primitive.getId()));
|
|---|
| 259 | }
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 | /**
|
|---|
| 264 | * Formats a name for a history node
|
|---|
| 265 | *
|
|---|
| 266 | * @param node the node
|
|---|
| 267 | * @return the name
|
|---|
| 268 | */
|
|---|
| 269 | public String format(HistoryNode node) {
|
|---|
| 270 | StringBuilder sb = new StringBuilder();
|
|---|
| 271 | String name;
|
|---|
| 272 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
|---|
| 273 | name = node.getLocalName();
|
|---|
| 274 | } else {
|
|---|
| 275 | name = node.getName();
|
|---|
| 276 | }
|
|---|
| 277 | if (name == null) {
|
|---|
| 278 | sb.append(node.getId());
|
|---|
| 279 | } else {
|
|---|
| 280 | sb.append(name);
|
|---|
| 281 | }
|
|---|
| 282 | sb.append(" (")
|
|---|
| 283 | .append(node.getCoords().latToString(CoordinateFormat.getDefaultFormat()))
|
|---|
| 284 | .append(", ")
|
|---|
| 285 | .append(node.getCoords().lonToString(CoordinateFormat.getDefaultFormat()))
|
|---|
| 286 | .append(")");
|
|---|
| 287 | decorateNameWithId(sb, node);
|
|---|
| 288 | return sb.toString();
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | /**
|
|---|
| 292 | * Formats a name for a way
|
|---|
| 293 | *
|
|---|
| 294 | * @param way the way
|
|---|
| 295 | * @return the name
|
|---|
| 296 | */
|
|---|
| 297 | public String format(HistoryWay way) {
|
|---|
| 298 | StringBuilder sb = new StringBuilder();
|
|---|
| 299 | String name;
|
|---|
| 300 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
|---|
| 301 | name = way.getLocalName();
|
|---|
| 302 | } else {
|
|---|
| 303 | name = way.getName();
|
|---|
| 304 | }
|
|---|
| 305 | if (name != null) {
|
|---|
| 306 | sb.append(name);
|
|---|
| 307 | }
|
|---|
| 308 | if (sb.length() == 0 && way.get("ref") != null) {
|
|---|
| 309 | sb.append(way.get("ref"));
|
|---|
| 310 | }
|
|---|
| 311 | if (sb.length() == 0) {
|
|---|
| 312 | sb.append(
|
|---|
| 313 | (way.get("highway") != null) ? tr("highway") :
|
|---|
| 314 | (way.get("railway") != null) ? tr("railway") :
|
|---|
| 315 | (way.get("waterway") != null) ? tr("waterway") :
|
|---|
| 316 | (way.get("landuse") != null) ? tr("landuse") : ""
|
|---|
| 317 | );
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | int nodesNo = way.isClosed() ? way.getNumNodes() -1 : way.getNumNodes();
|
|---|
| 321 | String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
|
|---|
| 322 | sb.append((sb.length() > 0) ? " ("+nodes+")" : nodes);
|
|---|
| 323 | decorateNameWithId(sb, way);
|
|---|
| 324 | return sb.toString();
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 | /**
|
|---|
| 329 | * Formats a name for a {@see HistoryRelation})
|
|---|
| 330 | *
|
|---|
| 331 | * @param relation the relation
|
|---|
| 332 | * @return the name
|
|---|
| 333 | */
|
|---|
| 334 | public String format(HistoryRelation relation) {
|
|---|
| 335 | StringBuilder sb = new StringBuilder();
|
|---|
| 336 | if (relation.get("type") != null) {
|
|---|
| 337 | sb.append(relation.get("type"));
|
|---|
| 338 | } else {
|
|---|
| 339 | sb.append(tr("relation"));
|
|---|
| 340 | }
|
|---|
| 341 | sb.append(" (");
|
|---|
| 342 | String nameTag = null;
|
|---|
| 343 | Set<String> namingTags = new HashSet<String>(getNamingtagsForRelations());
|
|---|
| 344 | for (String n : relation.getTags().keySet()) {
|
|---|
| 345 | // #3328: "note " and " note" are name tags too
|
|---|
| 346 | if (namingTags.contains(n.trim())) {
|
|---|
| 347 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
|---|
| 348 | nameTag = relation.getLocalName();
|
|---|
| 349 | } else {
|
|---|
| 350 | nameTag = relation.getName();
|
|---|
| 351 | }
|
|---|
| 352 | if (nameTag == null) {
|
|---|
| 353 | nameTag = relation.get(n);
|
|---|
| 354 | }
|
|---|
| 355 | }
|
|---|
| 356 | if (nameTag != null) {
|
|---|
| 357 | break;
|
|---|
| 358 | }
|
|---|
| 359 | }
|
|---|
| 360 | if (nameTag == null) {
|
|---|
| 361 | sb.append(Long.toString(relation.getId())).append(", ");
|
|---|
| 362 | } else {
|
|---|
| 363 | sb.append("\"").append(nameTag).append("\", ");
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | int mbno = relation.getNumMembers();
|
|---|
| 367 | sb.append(trn("{0} member", "{0} members", mbno, mbno)).append(")");
|
|---|
| 368 |
|
|---|
| 369 | decorateNameWithId(sb, relation);
|
|---|
| 370 | return sb.toString();
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | /**
|
|---|
| 374 | * Builds a default tooltip text for an HistoryOsmPrimitive <code>primitive</code>.
|
|---|
| 375 | *
|
|---|
| 376 | * @param primitive the primitmive
|
|---|
| 377 | * @return the tooltip text
|
|---|
| 378 | */
|
|---|
| 379 | public String buildDefaultToolTip(HistoryOsmPrimitive primitive) {
|
|---|
| 380 | StringBuilder sb = new StringBuilder();
|
|---|
| 381 | sb.append("<html>");
|
|---|
| 382 | sb.append("<strong>id</strong>=")
|
|---|
| 383 | .append(primitive.getId())
|
|---|
| 384 | .append("<br>");
|
|---|
| 385 | ArrayList<String> keyList = new ArrayList<String>(primitive.getTags().keySet());
|
|---|
| 386 | Collections.sort(keyList);
|
|---|
| 387 | for (int i = 0; i < keyList.size(); i++) {
|
|---|
| 388 | if (i > 0) {
|
|---|
| 389 | sb.append("<br>");
|
|---|
| 390 | }
|
|---|
| 391 | String key = keyList.get(i);
|
|---|
| 392 | sb.append("<strong>")
|
|---|
| 393 | .append(key)
|
|---|
| 394 | .append("</strong>")
|
|---|
| 395 | .append("=");
|
|---|
| 396 | String value = primitive.get(key);
|
|---|
| 397 | while(value.length() != 0) {
|
|---|
| 398 | sb.append(value.substring(0,Math.min(50, value.length())));
|
|---|
| 399 | if (value.length() > 50) {
|
|---|
| 400 | sb.append("<br>");
|
|---|
| 401 | value = value.substring(50);
|
|---|
| 402 | } else {
|
|---|
| 403 | value = "";
|
|---|
| 404 | }
|
|---|
| 405 | }
|
|---|
| 406 | }
|
|---|
| 407 | sb.append("</html>");
|
|---|
| 408 | return sb.toString();
|
|---|
| 409 | }
|
|---|
| 410 | }
|
|---|