[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[3389] | 5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
[4144] | 6 | import static org.openstreetmap.josm.tools.I18n.trc_lazy;
|
---|
[2512] | 7 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
| 8 |
|
---|
[7305] | 9 | import java.awt.ComponentOrientation;
|
---|
[2512] | 10 | import java.util.ArrayList;
|
---|
| 11 | import java.util.Arrays;
|
---|
[5059] | 12 | import java.util.Collection;
|
---|
[2563] | 13 | import java.util.Collections;
|
---|
[4070] | 14 | import java.util.Comparator;
|
---|
[2512] | 15 | import java.util.HashSet;
|
---|
[4209] | 16 | import java.util.LinkedList;
|
---|
[2512] | 17 | import java.util.List;
|
---|
[7305] | 18 | import java.util.Locale;
|
---|
[7018] | 19 | import java.util.Map;
|
---|
[2512] | 20 | import java.util.Set;
|
---|
| 21 |
|
---|
| 22 | import org.openstreetmap.josm.Main;
|
---|
| 23 | import org.openstreetmap.josm.data.coor.CoordinateFormat;
|
---|
[5346] | 24 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[2512] | 25 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
[4100] | 26 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
| 27 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
[2512] | 28 | import org.openstreetmap.josm.data.osm.NameFormatter;
|
---|
| 29 | import org.openstreetmap.josm.data.osm.Node;
|
---|
[5059] | 30 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[3268] | 31 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
[2512] | 32 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 33 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[2689] | 34 | import org.openstreetmap.josm.data.osm.history.HistoryNameFormatter;
|
---|
| 35 | import org.openstreetmap.josm.data.osm.history.HistoryNode;
|
---|
| 36 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
| 37 | import org.openstreetmap.josm.data.osm.history.HistoryRelation;
|
---|
| 38 | import org.openstreetmap.josm.data.osm.history.HistoryWay;
|
---|
[8863] | 39 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
| 40 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetNameTemplateList;
|
---|
[5083] | 41 | import org.openstreetmap.josm.tools.AlphanumComparator;
|
---|
[4477] | 42 | import org.openstreetmap.josm.tools.I18n;
|
---|
[5132] | 43 | import org.openstreetmap.josm.tools.Utils;
|
---|
| 44 | import org.openstreetmap.josm.tools.Utils.Function;
|
---|
[2512] | 45 |
|
---|
| 46 | /**
|
---|
[5266] | 47 | * This is the default implementation of a {@link NameFormatter} for names of {@link OsmPrimitive}s.
|
---|
[2512] | 48 | *
|
---|
| 49 | */
|
---|
[2689] | 50 | public class DefaultNameFormatter implements NameFormatter, HistoryNameFormatter {
|
---|
[2512] | 51 |
|
---|
[6889] | 52 | private static DefaultNameFormatter instance;
|
---|
[2512] | 53 |
|
---|
[7005] | 54 | private static final List<NameFormatterHook> formatHooks = new LinkedList<>();
|
---|
[4209] | 55 |
|
---|
[2512] | 56 | /**
|
---|
| 57 | * Replies the unique instance of this formatter
|
---|
| 58 | *
|
---|
| 59 | * @return the unique instance of this formatter
|
---|
| 60 | */
|
---|
[8126] | 61 | public static synchronized DefaultNameFormatter getInstance() {
|
---|
[2512] | 62 | if (instance == null) {
|
---|
| 63 | instance = new DefaultNameFormatter();
|
---|
| 64 | }
|
---|
| 65 | return instance;
|
---|
| 66 | }
|
---|
[4431] | 67 |
|
---|
[4209] | 68 | /**
|
---|
| 69 | * Registers a format hook. Adds the hook at the first position of the format hooks.
|
---|
| 70 | * (for plugins)
|
---|
| 71 | *
|
---|
| 72 | * @param hook the format hook. Ignored if null.
|
---|
| 73 | */
|
---|
| 74 | public static void registerFormatHook(NameFormatterHook hook) {
|
---|
| 75 | if (hook == null) return;
|
---|
| 76 | if (!formatHooks.contains(hook)) {
|
---|
[8510] | 77 | formatHooks.add(0, hook);
|
---|
[4209] | 78 | }
|
---|
| 79 | }
|
---|
[2512] | 80 |
|
---|
[4209] | 81 | /**
|
---|
| 82 | * Unregisters a format hook. Removes the hook from the list of format hooks.
|
---|
| 83 | *
|
---|
| 84 | * @param hook the format hook. Ignored if null.
|
---|
| 85 | */
|
---|
| 86 | public static void unregisterFormatHook(NameFormatterHook hook) {
|
---|
| 87 | if (hook == null) return;
|
---|
| 88 | if (formatHooks.contains(hook)) {
|
---|
| 89 | formatHooks.remove(hook);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[4882] | 93 | /** The default list of tags which are used as naming tags in relations.
|
---|
| 94 | * A ? prefix indicates a boolean value, for which the key (instead of the value) is used.
|
---|
| 95 | */
|
---|
[6889] | 96 | public static final String[] DEFAULT_NAMING_TAGS_FOR_RELATIONS = {"name", "ref", "restriction", "landuse", "natural",
|
---|
[4882] | 97 | "public_transport", ":LocationCode", "note", "?building"};
|
---|
[2512] | 98 |
|
---|
| 99 | /** the current list of tags used as naming tags in relations */
|
---|
[8840] | 100 | private static List<String> namingTagsForRelations;
|
---|
[2512] | 101 |
|
---|
| 102 | /**
|
---|
| 103 | * Replies the list of naming tags used in relations. The list is given (in this order) by:
|
---|
| 104 | * <ul>
|
---|
| 105 | * <li>by the tag names in the preference <tt>relation.nameOrder</tt></li>
|
---|
[5266] | 106 | * <li>by the default tags in {@link #DEFAULT_NAMING_TAGS_FOR_RELATIONS}
|
---|
[2512] | 107 | * </ul>
|
---|
| 108 | *
|
---|
| 109 | * @return the list of naming tags used in relations
|
---|
| 110 | */
|
---|
[8126] | 111 | public static synchronized List<String> getNamingtagsForRelations() {
|
---|
[2512] | 112 | if (namingTagsForRelations == null) {
|
---|
[7005] | 113 | namingTagsForRelations = new ArrayList<>(
|
---|
[2512] | 114 | Main.pref.getCollection("relation.nameOrder", Arrays.asList(DEFAULT_NAMING_TAGS_FOR_RELATIONS))
|
---|
[4431] | 115 | );
|
---|
[2512] | 116 | }
|
---|
| 117 | return namingTagsForRelations;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
| 121 | * Decorates the name of primitive with its id, if the preference
|
---|
[2973] | 122 | * <tt>osm-primitives.showid</tt> is set. Shows unique id if osm-primitives.showid.new-primitives is set
|
---|
[2512] | 123 | *
|
---|
| 124 | * @param name the name without the id
|
---|
| 125 | * @param primitive the primitive
|
---|
| 126 | */
|
---|
[4431] | 127 | protected void decorateNameWithId(StringBuilder name, IPrimitive primitive) {
|
---|
| 128 | if (Main.pref.getBoolean("osm-primitives.showid")) {
|
---|
| 129 | if (Main.pref.getBoolean("osm-primitives.showid.new-primitives")) {
|
---|
| 130 | name.append(tr(" [id: {0}]", primitive.getUniqueId()));
|
---|
| 131 | } else {
|
---|
| 132 | name.append(tr(" [id: {0}]", primitive.getId()));
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
[2512] | 135 | }
|
---|
| 136 |
|
---|
| 137 | /**
|
---|
| 138 | * Formats a name for a node
|
---|
| 139 | *
|
---|
| 140 | * @param node the node
|
---|
| 141 | * @return the name
|
---|
| 142 | */
|
---|
[6084] | 143 | @Override
|
---|
[4431] | 144 | public String format(Node node) {
|
---|
| 145 | StringBuilder name = new StringBuilder();
|
---|
[2578] | 146 | if (node.isIncomplete()) {
|
---|
[4431] | 147 | name.append(tr("incomplete"));
|
---|
[2512] | 148 | } else {
|
---|
[4431] | 149 | TaggingPreset preset = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(node);
|
---|
| 150 | if (preset == null) {
|
---|
| 151 | String n;
|
---|
| 152 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
---|
| 153 | n = node.getLocalName();
|
---|
| 154 | } else {
|
---|
| 155 | n = node.getName();
|
---|
[3887] | 156 | }
|
---|
[8395] | 157 | if (n == null) {
|
---|
[4431] | 158 | String s;
|
---|
[8510] | 159 | if ((s = node.get("addr:housename")) != null) {
|
---|
[4431] | 160 | /* I18n: name of house as parameter */
|
---|
| 161 | n = tr("House {0}", s);
|
---|
| 162 | }
|
---|
[8510] | 163 | if (n == null && (s = node.get("addr:housenumber")) != null) {
|
---|
[4431] | 164 | String t = node.get("addr:street");
|
---|
[8510] | 165 | if (t != null) {
|
---|
[4431] | 166 | /* I18n: house number, street as parameter, number should remain
|
---|
[3887] | 167 | before street for better visibility */
|
---|
[4431] | 168 | n = tr("House number {0} at {1}", s, t);
|
---|
[8342] | 169 | } else {
|
---|
[4431] | 170 | /* I18n: house number as parameter */
|
---|
| 171 | n = tr("House number {0}", s);
|
---|
| 172 | }
|
---|
[3887] | 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[4431] | 176 | if (n == null) {
|
---|
[6986] | 177 | n = node.isNew() ? tr("node") : Long.toString(node.getId());
|
---|
[4431] | 178 | }
|
---|
| 179 | name.append(n);
|
---|
| 180 | } else {
|
---|
| 181 | preset.nameTemplate.appendText(name, node);
|
---|
[2512] | 182 | }
|
---|
[5328] | 183 | if (node.getCoor() != null) {
|
---|
[8390] | 184 | name.append(" \u200E(").append(node.getCoor().latToString(CoordinateFormat.getDefaultFormat())).append(", ")
|
---|
| 185 | .append(node.getCoor().lonToString(CoordinateFormat.getDefaultFormat())).append(')');
|
---|
[5328] | 186 | }
|
---|
[2512] | 187 | }
|
---|
[4431] | 188 | decorateNameWithId(name, node);
|
---|
[4209] | 189 |
|
---|
[4431] | 190 |
|
---|
| 191 | String result = name.toString();
|
---|
[4209] | 192 | for (NameFormatterHook hook: formatHooks) {
|
---|
[4431] | 193 | String hookResult = hook.checkFormat(node, result);
|
---|
| 194 | if (hookResult != null)
|
---|
[4209] | 195 | return hookResult;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[4431] | 198 | return result;
|
---|
[2512] | 199 | }
|
---|
| 200 |
|
---|
[4070] | 201 | private final Comparator<Node> nodeComparator = new Comparator<Node>() {
|
---|
| 202 | @Override
|
---|
| 203 | public int compare(Node n1, Node n2) {
|
---|
| 204 | return format(n1).compareTo(format(n2));
|
---|
| 205 | }
|
---|
| 206 | };
|
---|
| 207 |
|
---|
[6084] | 208 | @Override
|
---|
[4070] | 209 | public Comparator<Node> getNodeComparator() {
|
---|
| 210 | return nodeComparator;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 |
|
---|
[2512] | 214 | /**
|
---|
| 215 | * Formats a name for a way
|
---|
| 216 | *
|
---|
| 217 | * @param way the way
|
---|
| 218 | * @return the name
|
---|
| 219 | */
|
---|
[6084] | 220 | @Override
|
---|
[4431] | 221 | public String format(Way way) {
|
---|
| 222 | StringBuilder name = new StringBuilder();
|
---|
[7305] | 223 |
|
---|
| 224 | char mark = 0;
|
---|
| 225 | // If current language is left-to-right (almost all languages)
|
---|
| 226 | if (ComponentOrientation.getOrientation(Locale.getDefault()).isLeftToRight()) {
|
---|
| 227 | // will insert Left-To-Right Mark to ensure proper display of text in the case when object name is right-to-left
|
---|
| 228 | mark = '\u200E';
|
---|
| 229 | } else {
|
---|
| 230 | // otherwise will insert Right-To-Left Mark to ensure proper display in the opposite case
|
---|
| 231 | mark = '\u200F';
|
---|
| 232 | }
|
---|
| 233 | // Initialize base direction of the string
|
---|
| 234 | name.append(mark);
|
---|
| 235 |
|
---|
[2578] | 236 | if (way.isIncomplete()) {
|
---|
[4431] | 237 | name.append(tr("incomplete"));
|
---|
[2512] | 238 | } else {
|
---|
[4431] | 239 | TaggingPreset preset = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(way);
|
---|
| 240 | if (preset == null) {
|
---|
| 241 | String n;
|
---|
| 242 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
---|
| 243 | n = way.getLocalName();
|
---|
| 244 | } else {
|
---|
| 245 | n = way.getName();
|
---|
[3887] | 246 | }
|
---|
[4431] | 247 | if (n == null) {
|
---|
| 248 | n = way.get("ref");
|
---|
| 249 | }
|
---|
| 250 | if (n == null) {
|
---|
[7305] | 251 | n = (way.get("highway") != null) ? tr("highway") :
|
---|
[4431] | 252 | (way.get("railway") != null) ? tr("railway") :
|
---|
| 253 | (way.get("waterway") != null) ? tr("waterway") :
|
---|
[4744] | 254 | (way.get("landuse") != null) ? tr("landuse") : null;
|
---|
[4431] | 255 | }
|
---|
[7305] | 256 | if (n == null) {
|
---|
[4431] | 257 | String s;
|
---|
[8510] | 258 | if ((s = way.get("addr:housename")) != null) {
|
---|
[4431] | 259 | /* I18n: name of house as parameter */
|
---|
| 260 | n = tr("House {0}", s);
|
---|
| 261 | }
|
---|
[8510] | 262 | if (n == null && (s = way.get("addr:housenumber")) != null) {
|
---|
[4431] | 263 | String t = way.get("addr:street");
|
---|
[8510] | 264 | if (t != null) {
|
---|
[4431] | 265 | /* I18n: house number, street as parameter, number should remain
|
---|
[3887] | 266 | before street for better visibility */
|
---|
[4431] | 267 | n = tr("House number {0} at {1}", s, t);
|
---|
[8342] | 268 | } else {
|
---|
[4431] | 269 | /* I18n: house number as parameter */
|
---|
| 270 | n = tr("House number {0}", s);
|
---|
| 271 | }
|
---|
[3887] | 272 | }
|
---|
| 273 | }
|
---|
[8510] | 274 | if (n == null && way.get("building") != null) n = tr("building");
|
---|
| 275 | if (n == null || n.isEmpty()) {
|
---|
[4431] | 276 | n = String.valueOf(way.getId());
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | name.append(n);
|
---|
| 280 | } else {
|
---|
| 281 | preset.nameTemplate.appendText(name, way);
|
---|
[3887] | 282 | }
|
---|
[2512] | 283 |
|
---|
[5847] | 284 | int nodesNo = way.getRealNodesCount();
|
---|
[3286] | 285 | /* note: length == 0 should no longer happen, but leave the bracket code
|
---|
| 286 | nevertheless, who knows what future brings */
|
---|
[3887] | 287 | /* I18n: count of nodes as parameter */
|
---|
[2512] | 288 | String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
|
---|
[8390] | 289 | name.append(mark).append(" (").append(nodes).append(')');
|
---|
[2512] | 290 | }
|
---|
[4431] | 291 | decorateNameWithId(name, way);
|
---|
| 292 |
|
---|
| 293 | String result = name.toString();
|
---|
[4209] | 294 | for (NameFormatterHook hook: formatHooks) {
|
---|
[4431] | 295 | String hookResult = hook.checkFormat(way, result);
|
---|
| 296 | if (hookResult != null)
|
---|
[4209] | 297 | return hookResult;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[4431] | 300 | return result;
|
---|
[2512] | 301 | }
|
---|
| 302 |
|
---|
[4070] | 303 | private final Comparator<Way> wayComparator = new Comparator<Way>() {
|
---|
| 304 | @Override
|
---|
| 305 | public int compare(Way w1, Way w2) {
|
---|
| 306 | return format(w1).compareTo(format(w2));
|
---|
| 307 | }
|
---|
| 308 | };
|
---|
| 309 |
|
---|
[6084] | 310 | @Override
|
---|
[4070] | 311 | public Comparator<Way> getWayComparator() {
|
---|
| 312 | return wayComparator;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 |
|
---|
[2512] | 316 | /**
|
---|
| 317 | * Formats a name for a relation
|
---|
| 318 | *
|
---|
| 319 | * @param relation the relation
|
---|
| 320 | * @return the name
|
---|
| 321 | */
|
---|
[6084] | 322 | @Override
|
---|
[4431] | 323 | public String format(Relation relation) {
|
---|
| 324 | StringBuilder name = new StringBuilder();
|
---|
[2578] | 325 | if (relation.isIncomplete()) {
|
---|
[4431] | 326 | name.append(tr("incomplete"));
|
---|
[2512] | 327 | } else {
|
---|
[4431] | 328 | TaggingPreset preset = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(relation);
|
---|
[2512] | 329 |
|
---|
[4431] | 330 | formatRelationNameAndType(relation, name, preset);
|
---|
| 331 |
|
---|
[2512] | 332 | int mbno = relation.getMembersCount();
|
---|
[4431] | 333 | name.append(trn("{0} member", "{0} members", mbno, mbno));
|
---|
[2885] | 334 |
|
---|
[4431] | 335 | if (relation.hasIncompleteMembers()) {
|
---|
| 336 | name.append(", ").append(tr("incomplete"));
|
---|
[2885] | 337 | }
|
---|
| 338 |
|
---|
[8390] | 339 | name.append(')');
|
---|
[2512] | 340 | }
|
---|
[4431] | 341 | decorateNameWithId(name, relation);
|
---|
[4209] | 342 |
|
---|
[4431] | 343 | String result = name.toString();
|
---|
[4209] | 344 | for (NameFormatterHook hook: formatHooks) {
|
---|
[4431] | 345 | String hookResult = hook.checkFormat(relation, result);
|
---|
| 346 | if (hookResult != null)
|
---|
[4209] | 347 | return hookResult;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
[4431] | 350 | return result;
|
---|
[2512] | 351 | }
|
---|
| 352 |
|
---|
[4431] | 353 | private void formatRelationNameAndType(Relation relation, StringBuilder result, TaggingPreset preset) {
|
---|
| 354 | if (preset == null) {
|
---|
| 355 | result.append(getRelationTypeName(relation));
|
---|
| 356 | String relationName = getRelationName(relation);
|
---|
| 357 | if (relationName == null) {
|
---|
| 358 | relationName = Long.toString(relation.getId());
|
---|
| 359 | } else {
|
---|
[8846] | 360 | relationName = '\"' + relationName + '\"';
|
---|
[4431] | 361 | }
|
---|
| 362 | result.append(" (").append(relationName).append(", ");
|
---|
| 363 | } else {
|
---|
| 364 | preset.nameTemplate.appendText(result, relation);
|
---|
[8390] | 365 | result.append('(');
|
---|
[4431] | 366 | }
|
---|
| 367 | }
|
---|
| 368 |
|
---|
[4070] | 369 | private final Comparator<Relation> relationComparator = new Comparator<Relation>() {
|
---|
| 370 | @Override
|
---|
| 371 | public int compare(Relation r1, Relation r2) {
|
---|
[4431] | 372 | //TODO This doesn't work correctly with formatHooks
|
---|
[4070] | 373 |
|
---|
[4431] | 374 | TaggingPreset preset1 = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(r1);
|
---|
| 375 | TaggingPreset preset2 = TaggingPresetNameTemplateList.getInstance().findPresetTemplate(r2);
|
---|
[4070] | 376 |
|
---|
[4431] | 377 | if (preset1 != null || preset2 != null) {
|
---|
| 378 | StringBuilder name1 = new StringBuilder();
|
---|
| 379 | formatRelationNameAndType(r1, name1, preset1);
|
---|
| 380 | StringBuilder name2 = new StringBuilder();
|
---|
| 381 | formatRelationNameAndType(r2, name2, preset2);
|
---|
[4070] | 382 |
|
---|
[7098] | 383 | int comp = AlphanumComparator.getInstance().compare(name1.toString(), name2.toString());
|
---|
[4070] | 384 | if (comp != 0)
|
---|
| 385 | return comp;
|
---|
[4431] | 386 | } else {
|
---|
[4070] | 387 |
|
---|
[4431] | 388 | String type1 = getRelationTypeName(r1);
|
---|
| 389 | String type2 = getRelationTypeName(r2);
|
---|
| 390 |
|
---|
[6804] | 391 | int comp = AlphanumComparator.getInstance().compare(type1, type2);
|
---|
[4070] | 392 | if (comp != 0)
|
---|
| 393 | return comp;
|
---|
| 394 |
|
---|
[4431] | 395 | String name1 = getRelationName(r1);
|
---|
| 396 | String name2 = getRelationName(r2);
|
---|
| 397 |
|
---|
[6804] | 398 | comp = AlphanumComparator.getInstance().compare(name1, name2);
|
---|
[5710] | 399 | if (comp != 0)
|
---|
| 400 | return comp;
|
---|
[4070] | 401 | }
|
---|
| 402 |
|
---|
| 403 | if (r1.getMembersCount() != r2.getMembersCount())
|
---|
[8510] | 404 | return (r1.getMembersCount() > r2.getMembersCount()) ? 1 : -1;
|
---|
[4070] | 405 |
|
---|
[4431] | 406 | int comp = Boolean.valueOf(r1.hasIncompleteMembers()).compareTo(Boolean.valueOf(r2.hasIncompleteMembers()));
|
---|
[4070] | 407 | if (comp != 0)
|
---|
| 408 | return comp;
|
---|
| 409 |
|
---|
[6070] | 410 | if (r1.getUniqueId() > r2.getUniqueId())
|
---|
[5750] | 411 | return 1;
|
---|
| 412 | else if (r1.getUniqueId() < r2.getUniqueId())
|
---|
| 413 | return -1;
|
---|
| 414 | else
|
---|
| 415 | return 0;
|
---|
[4070] | 416 | }
|
---|
| 417 | };
|
---|
| 418 |
|
---|
[6084] | 419 | @Override
|
---|
[4070] | 420 | public Comparator<Relation> getRelationComparator() {
|
---|
| 421 | return relationComparator;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
[4100] | 424 | private String getRelationTypeName(IRelation relation) {
|
---|
[4070] | 425 | String name = trc("Relation type", relation.get("type"));
|
---|
| 426 | if (name == null) {
|
---|
| 427 | name = (relation.get("public_transport") != null) ? tr("public transport") : null;
|
---|
| 428 | }
|
---|
| 429 | if (name == null) {
|
---|
| 430 | String building = relation.get("building");
|
---|
[4744] | 431 | if (OsmUtils.isTrue(building)) {
|
---|
[4070] | 432 | name = tr("building");
|
---|
[8395] | 433 | } else if (building != null) {
|
---|
[4070] | 434 | name = tr(building); // translate tag!
|
---|
| 435 | }
|
---|
| 436 | }
|
---|
| 437 | if (name == null) {
|
---|
| 438 | name = trc("Place type", relation.get("place"));
|
---|
| 439 | }
|
---|
| 440 | if (name == null) {
|
---|
| 441 | name = tr("relation");
|
---|
| 442 | }
|
---|
| 443 | String admin_level = relation.get("admin_level");
|
---|
| 444 | if (admin_level != null) {
|
---|
[8846] | 445 | name += '['+admin_level+']';
|
---|
[4070] | 446 | }
|
---|
[4431] | 447 |
|
---|
[4209] | 448 | for (NameFormatterHook hook: formatHooks) {
|
---|
| 449 | String hookResult = hook.checkRelationTypeName(relation, name);
|
---|
[4431] | 450 | if (hookResult != null)
|
---|
[4209] | 451 | return hookResult;
|
---|
| 452 | }
|
---|
[4070] | 453 |
|
---|
| 454 | return name;
|
---|
| 455 | }
|
---|
| 456 |
|
---|
[4100] | 457 | private String getNameTagValue(IRelation relation, String nameTag) {
|
---|
[6990] | 458 | if ("name".equals(nameTag)) {
|
---|
[4070] | 459 | if (Main.pref.getBoolean("osm-primitives.localize-name", true))
|
---|
| 460 | return relation.getLocalName();
|
---|
| 461 | else
|
---|
| 462 | return relation.getName();
|
---|
[6990] | 463 | } else if (":LocationCode".equals(nameTag)) {
|
---|
[4070] | 464 | for (String m : relation.keySet()) {
|
---|
| 465 | if (m.endsWith(nameTag))
|
---|
| 466 | return relation.get(m);
|
---|
| 467 | }
|
---|
| 468 | return null;
|
---|
[4882] | 469 | } else if (nameTag.startsWith("?") && OsmUtils.isTrue(relation.get(nameTag.substring(1)))) {
|
---|
| 470 | return tr(nameTag.substring(1));
|
---|
| 471 | } else if (nameTag.startsWith("?") && OsmUtils.isFalse(relation.get(nameTag.substring(1)))) {
|
---|
[4744] | 472 | return null;
|
---|
[7112] | 473 | } else if (nameTag.startsWith("?")) {
|
---|
| 474 | return trc_lazy(nameTag, I18n.escape(relation.get(nameTag.substring(1))));
|
---|
[4744] | 475 | } else {
|
---|
[4477] | 476 | return trc_lazy(nameTag, I18n.escape(relation.get(nameTag)));
|
---|
[4744] | 477 | }
|
---|
[4070] | 478 | }
|
---|
| 479 |
|
---|
[4100] | 480 | private String getRelationName(IRelation relation) {
|
---|
[4070] | 481 | String nameTag = null;
|
---|
| 482 | for (String n : getNamingtagsForRelations()) {
|
---|
| 483 | nameTag = getNameTagValue(relation, n);
|
---|
| 484 | if (nameTag != null)
|
---|
| 485 | return nameTag;
|
---|
| 486 | }
|
---|
| 487 | return null;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
[2512] | 490 | /**
|
---|
| 491 | * Formats a name for a changeset
|
---|
| 492 | *
|
---|
| 493 | * @param changeset the changeset
|
---|
| 494 | * @return the name
|
---|
| 495 | */
|
---|
[6084] | 496 | @Override
|
---|
[2512] | 497 | public String format(Changeset changeset) {
|
---|
[8510] | 498 | return tr("Changeset {0}", changeset.getId());
|
---|
[2512] | 499 | }
|
---|
[2563] | 500 |
|
---|
| 501 | /**
|
---|
| 502 | * Builds a default tooltip text for the primitive <code>primitive</code>.
|
---|
[2711] | 503 | *
|
---|
[2563] | 504 | * @param primitive the primitmive
|
---|
| 505 | * @return the tooltip text
|
---|
| 506 | */
|
---|
[4100] | 507 | public String buildDefaultToolTip(IPrimitive primitive) {
|
---|
[7018] | 508 | return buildDefaultToolTip(primitive.getId(), primitive.getKeys());
|
---|
| 509 | }
|
---|
[7098] | 510 |
|
---|
[7018] | 511 | private String buildDefaultToolTip(long id, Map<String, String> tags) {
|
---|
[2563] | 512 | StringBuilder sb = new StringBuilder();
|
---|
[8390] | 513 | sb.append("<html><strong>id</strong>=")
|
---|
| 514 | .append(id)
|
---|
| 515 | .append("<br>");
|
---|
[7018] | 516 | List<String> keyList = new ArrayList<>(tags.keySet());
|
---|
[2563] | 517 | Collections.sort(keyList);
|
---|
| 518 | for (int i = 0; i < keyList.size(); i++) {
|
---|
| 519 | if (i > 0) {
|
---|
| 520 | sb.append("<br>");
|
---|
| 521 | }
|
---|
| 522 | String key = keyList.get(i);
|
---|
| 523 | sb.append("<strong>")
|
---|
[8390] | 524 | .append(key)
|
---|
| 525 | .append("</strong>=");
|
---|
[7018] | 526 | String value = tags.get(key);
|
---|
[8461] | 527 | while (!value.isEmpty()) {
|
---|
[8510] | 528 | sb.append(value.substring(0, Math.min(50, value.length())));
|
---|
[2563] | 529 | if (value.length() > 50) {
|
---|
| 530 | sb.append("<br>");
|
---|
| 531 | value = value.substring(50);
|
---|
| 532 | } else {
|
---|
| 533 | value = "";
|
---|
| 534 | }
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
| 537 | sb.append("</html>");
|
---|
| 538 | return sb.toString();
|
---|
| 539 | }
|
---|
[2689] | 540 |
|
---|
| 541 | /**
|
---|
| 542 | * Decorates the name of primitive with its id, if the preference
|
---|
| 543 | * <tt>osm-primitives.showid</tt> is set.
|
---|
[2711] | 544 | *
|
---|
[7187] | 545 | * The id is append to the {@link StringBuilder} passed in <code>name</code>.
|
---|
[2689] | 546 | *
|
---|
| 547 | * @param name the name without the id
|
---|
| 548 | * @param primitive the primitive
|
---|
| 549 | */
|
---|
| 550 | protected void decorateNameWithId(StringBuilder name, HistoryOsmPrimitive primitive) {
|
---|
| 551 | if (Main.pref.getBoolean("osm-primitives.showid")) {
|
---|
| 552 | name.append(tr(" [id: {0}]", primitive.getId()));
|
---|
| 553 | }
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | /**
|
---|
| 557 | * Formats a name for a history node
|
---|
| 558 | *
|
---|
| 559 | * @param node the node
|
---|
| 560 | * @return the name
|
---|
| 561 | */
|
---|
[6084] | 562 | @Override
|
---|
[2689] | 563 | public String format(HistoryNode node) {
|
---|
| 564 | StringBuilder sb = new StringBuilder();
|
---|
| 565 | String name;
|
---|
| 566 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
---|
| 567 | name = node.getLocalName();
|
---|
| 568 | } else {
|
---|
| 569 | name = node.getName();
|
---|
| 570 | }
|
---|
| 571 | if (name == null) {
|
---|
| 572 | sb.append(node.getId());
|
---|
| 573 | } else {
|
---|
| 574 | sb.append(name);
|
---|
| 575 | }
|
---|
[5346] | 576 | LatLon coord = node.getCoords();
|
---|
| 577 | if (coord != null) {
|
---|
| 578 | sb.append(" (")
|
---|
| 579 | .append(coord.latToString(CoordinateFormat.getDefaultFormat()))
|
---|
| 580 | .append(", ")
|
---|
| 581 | .append(coord.lonToString(CoordinateFormat.getDefaultFormat()))
|
---|
[8390] | 582 | .append(')');
|
---|
[5346] | 583 | }
|
---|
[2689] | 584 | decorateNameWithId(sb, node);
|
---|
| 585 | return sb.toString();
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | /**
|
---|
| 589 | * Formats a name for a way
|
---|
| 590 | *
|
---|
| 591 | * @param way the way
|
---|
| 592 | * @return the name
|
---|
| 593 | */
|
---|
[6084] | 594 | @Override
|
---|
[2689] | 595 | public String format(HistoryWay way) {
|
---|
| 596 | StringBuilder sb = new StringBuilder();
|
---|
| 597 | String name;
|
---|
| 598 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
---|
| 599 | name = way.getLocalName();
|
---|
| 600 | } else {
|
---|
| 601 | name = way.getName();
|
---|
| 602 | }
|
---|
| 603 | if (name != null) {
|
---|
| 604 | sb.append(name);
|
---|
| 605 | }
|
---|
| 606 | if (sb.length() == 0 && way.get("ref") != null) {
|
---|
| 607 | sb.append(way.get("ref"));
|
---|
| 608 | }
|
---|
| 609 | if (sb.length() == 0) {
|
---|
| 610 | sb.append(
|
---|
| 611 | (way.get("highway") != null) ? tr("highway") :
|
---|
| 612 | (way.get("railway") != null) ? tr("railway") :
|
---|
| 613 | (way.get("waterway") != null) ? tr("waterway") :
|
---|
| 614 | (way.get("landuse") != null) ? tr("landuse") : ""
|
---|
[4431] | 615 | );
|
---|
[2689] | 616 | }
|
---|
| 617 |
|
---|
| 618 | int nodesNo = way.isClosed() ? way.getNumNodes() -1 : way.getNumNodes();
|
---|
| 619 | String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
|
---|
[8443] | 620 | if (sb.length() == 0) {
|
---|
[3286] | 621 | sb.append(way.getId());
|
---|
[4070] | 622 | }
|
---|
[3286] | 623 | /* note: length == 0 should no longer happen, but leave the bracket code
|
---|
| 624 | nevertheless, who knows what future brings */
|
---|
[8846] | 625 | sb.append((sb.length() > 0) ? " ("+nodes+')' : nodes);
|
---|
[2689] | 626 | decorateNameWithId(sb, way);
|
---|
| 627 | return sb.toString();
|
---|
| 628 | }
|
---|
| 629 |
|
---|
| 630 | /**
|
---|
[5266] | 631 | * Formats a name for a {@link HistoryRelation})
|
---|
[2689] | 632 | *
|
---|
| 633 | * @param relation the relation
|
---|
| 634 | * @return the name
|
---|
| 635 | */
|
---|
[6084] | 636 | @Override
|
---|
[2689] | 637 | public String format(HistoryRelation relation) {
|
---|
| 638 | StringBuilder sb = new StringBuilder();
|
---|
| 639 | if (relation.get("type") != null) {
|
---|
| 640 | sb.append(relation.get("type"));
|
---|
| 641 | } else {
|
---|
| 642 | sb.append(tr("relation"));
|
---|
| 643 | }
|
---|
| 644 | sb.append(" (");
|
---|
| 645 | String nameTag = null;
|
---|
[7005] | 646 | Set<String> namingTags = new HashSet<>(getNamingtagsForRelations());
|
---|
[2689] | 647 | for (String n : relation.getTags().keySet()) {
|
---|
| 648 | // #3328: "note " and " note" are name tags too
|
---|
| 649 | if (namingTags.contains(n.trim())) {
|
---|
| 650 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
---|
| 651 | nameTag = relation.getLocalName();
|
---|
| 652 | } else {
|
---|
| 653 | nameTag = relation.getName();
|
---|
| 654 | }
|
---|
| 655 | if (nameTag == null) {
|
---|
| 656 | nameTag = relation.get(n);
|
---|
| 657 | }
|
---|
| 658 | }
|
---|
| 659 | if (nameTag != null) {
|
---|
| 660 | break;
|
---|
| 661 | }
|
---|
| 662 | }
|
---|
| 663 | if (nameTag == null) {
|
---|
| 664 | sb.append(Long.toString(relation.getId())).append(", ");
|
---|
| 665 | } else {
|
---|
[8846] | 666 | sb.append('\"').append(nameTag).append("\", ");
|
---|
[2689] | 667 | }
|
---|
| 668 |
|
---|
| 669 | int mbno = relation.getNumMembers();
|
---|
[8390] | 670 | sb.append(trn("{0} member", "{0} members", mbno, mbno)).append(')');
|
---|
[2689] | 671 |
|
---|
| 672 | decorateNameWithId(sb, relation);
|
---|
| 673 | return sb.toString();
|
---|
| 674 | }
|
---|
| 675 |
|
---|
| 676 | /**
|
---|
| 677 | * Builds a default tooltip text for an HistoryOsmPrimitive <code>primitive</code>.
|
---|
[2711] | 678 | *
|
---|
[2689] | 679 | * @param primitive the primitmive
|
---|
| 680 | * @return the tooltip text
|
---|
| 681 | */
|
---|
| 682 | public String buildDefaultToolTip(HistoryOsmPrimitive primitive) {
|
---|
[7018] | 683 | return buildDefaultToolTip(primitive.getId(), primitive.getTags());
|
---|
[2689] | 684 | }
|
---|
[5059] | 685 |
|
---|
| 686 | public String formatAsHtmlUnorderedList(Collection<? extends OsmPrimitive> primitives) {
|
---|
[5132] | 687 | return Utils.joinAsHtmlUnorderedList(Utils.transform(primitives, new Function<OsmPrimitive, String>() {
|
---|
| 688 |
|
---|
| 689 | @Override
|
---|
| 690 | public String apply(OsmPrimitive x) {
|
---|
| 691 | return x.getDisplayName(DefaultNameFormatter.this);
|
---|
| 692 | }
|
---|
| 693 | }));
|
---|
[5059] | 694 | }
|
---|
| 695 |
|
---|
| 696 | public String formatAsHtmlUnorderedList(OsmPrimitive... primitives) {
|
---|
| 697 | return formatAsHtmlUnorderedList(Arrays.asList(primitives));
|
---|
| 698 | }
|
---|
[2512] | 699 | }
|
---|