| 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.HashSet;
|
|---|
| 10 | import java.util.List;
|
|---|
| 11 | import java.util.Set;
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.Main;
|
|---|
| 14 | import org.openstreetmap.josm.data.coor.CoordinateFormat;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.Changeset;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.NameFormatter;
|
|---|
| 17 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 19 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 20 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * This is the default implementation of a {@see NameFormatter} for names of {@see OsmPrimitive}s.
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 | public class DefaultNameFormatter implements NameFormatter {
|
|---|
| 27 |
|
|---|
| 28 | static private DefaultNameFormatter instance;
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Replies the unique instance of this formatter
|
|---|
| 32 | *
|
|---|
| 33 | * @return the unique instance of this formatter
|
|---|
| 34 | */
|
|---|
| 35 | static public DefaultNameFormatter getInstance() {
|
|---|
| 36 | if (instance == null) {
|
|---|
| 37 | instance = new DefaultNameFormatter();
|
|---|
| 38 | }
|
|---|
| 39 | return instance;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /** the default list of tags which are used as naming tags in relations */
|
|---|
| 43 | static public final String[] DEFAULT_NAMING_TAGS_FOR_RELATIONS = {"name", "ref", "restriction", "note"};
|
|---|
| 44 |
|
|---|
| 45 | /** the current list of tags used as naming tags in relations */
|
|---|
| 46 | static private List<String> namingTagsForRelations = null;
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Replies the list of naming tags used in relations. The list is given (in this order) by:
|
|---|
| 50 | * <ul>
|
|---|
| 51 | * <li>by the tag names in the preference <tt>relation.nameOrder</tt></li>
|
|---|
| 52 | * <li>by the default tags in {@see #DEFAULT_NAMING_TAGS_FOR_RELATIONS}
|
|---|
| 53 | * </ul>
|
|---|
| 54 | *
|
|---|
| 55 | * @return the list of naming tags used in relations
|
|---|
| 56 | */
|
|---|
| 57 | static public List<String> getNamingtagsForRelations() {
|
|---|
| 58 | if (namingTagsForRelations == null) {
|
|---|
| 59 | namingTagsForRelations = new ArrayList<String>(
|
|---|
| 60 | Main.pref.getCollection("relation.nameOrder", Arrays.asList(DEFAULT_NAMING_TAGS_FOR_RELATIONS))
|
|---|
| 61 | );
|
|---|
| 62 | }
|
|---|
| 63 | return namingTagsForRelations;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Decorates the name of primitive with its id, if the preference
|
|---|
| 69 | * <tt>osm-primitives.showid</tt> is set.
|
|---|
| 70 | *
|
|---|
| 71 | * @param name the name without the id
|
|---|
| 72 | * @param primitive the primitive
|
|---|
| 73 | * @return the decorated name
|
|---|
| 74 | */
|
|---|
| 75 | protected String decorateNameWithId(String name, OsmPrimitive primitive) {
|
|---|
| 76 | if (Main.pref.getBoolean("osm-primitives.showid"))
|
|---|
| 77 | return name + tr(" [id: {0}]", primitive.getId());
|
|---|
| 78 | else
|
|---|
| 79 | return name;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Formats a name for a node
|
|---|
| 84 | *
|
|---|
| 85 | * @param node the node
|
|---|
| 86 | * @return the name
|
|---|
| 87 | */
|
|---|
| 88 | public String format(Node node) {
|
|---|
| 89 | String name = "";
|
|---|
| 90 | if (node.incomplete) {
|
|---|
| 91 | name = tr("incomplete");
|
|---|
| 92 | } else {
|
|---|
| 93 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
|---|
| 94 | name = node.getLocalName();
|
|---|
| 95 | } else {
|
|---|
| 96 | name = node.getName();
|
|---|
| 97 | }
|
|---|
| 98 | if (name == null) {
|
|---|
| 99 | name = node.isNew() ? tr("node") : ""+ node.getId();
|
|---|
| 100 | }
|
|---|
| 101 | name += " (" + node.getCoor().latToString(CoordinateFormat.getDefaultFormat()) + ", " + node.getCoor().lonToString(CoordinateFormat.getDefaultFormat()) + ")";
|
|---|
| 102 | }
|
|---|
| 103 | name = decorateNameWithId(name, node);
|
|---|
| 104 | return name;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /**
|
|---|
| 108 | * Formats a name for a way
|
|---|
| 109 | *
|
|---|
| 110 | * @param way the way
|
|---|
| 111 | * @return the name
|
|---|
| 112 | */
|
|---|
| 113 | public String format(Way way) {
|
|---|
| 114 | String name = "";
|
|---|
| 115 | if (way.incomplete) {
|
|---|
| 116 | name = tr("incomplete");
|
|---|
| 117 | } else {
|
|---|
| 118 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
|---|
| 119 | name = way.getLocalName();
|
|---|
| 120 | } else {
|
|---|
| 121 | name = way.getName();
|
|---|
| 122 | }
|
|---|
| 123 | if (name == null) {
|
|---|
| 124 | name = way.get("ref");
|
|---|
| 125 | }
|
|---|
| 126 | if (name == null) {
|
|---|
| 127 | name =
|
|---|
| 128 | (way.get("highway") != null) ? tr("highway") :
|
|---|
| 129 | (way.get("railway") != null) ? tr("railway") :
|
|---|
| 130 | (way.get("waterway") != null) ? tr("waterway") :
|
|---|
| 131 | (way.get("landuse") != null) ? tr("landuse") : "";
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | int nodesNo = way.getNodesCount();
|
|---|
| 135 | if (nodesNo > 1 && way.isClosed()) {
|
|---|
| 136 | nodesNo--;
|
|---|
| 137 | }
|
|---|
| 138 | String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
|
|---|
| 139 | name += (name.length() > 0) ? " ("+nodes+")" : nodes;
|
|---|
| 140 | }
|
|---|
| 141 | name = decorateNameWithId(name, way);
|
|---|
| 142 | return name;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /**
|
|---|
| 146 | * Formats a name for a relation
|
|---|
| 147 | *
|
|---|
| 148 | * @param relation the relation
|
|---|
| 149 | * @return the name
|
|---|
| 150 | */
|
|---|
| 151 | public String format(Relation relation) {
|
|---|
| 152 | String name;
|
|---|
| 153 | if (relation.incomplete) {
|
|---|
| 154 | name = tr("incomplete");
|
|---|
| 155 | } else {
|
|---|
| 156 | name = relation.get("type");
|
|---|
| 157 | if (name == null) {
|
|---|
| 158 | name = tr("relation");
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | name += " (";
|
|---|
| 162 | String nameTag = null;
|
|---|
| 163 | Set<String> namingTags = new HashSet<String>(getNamingtagsForRelations());
|
|---|
| 164 | for (String n : relation.keySet()) {
|
|---|
| 165 | // #3328: "note " and " note" are name tags too
|
|---|
| 166 | if (namingTags.contains(n.trim())) {
|
|---|
| 167 | if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
|
|---|
| 168 | nameTag = relation.getLocalName();
|
|---|
| 169 | } else {
|
|---|
| 170 | nameTag = relation.getName();
|
|---|
| 171 | }
|
|---|
| 172 | if (nameTag == null) {
|
|---|
| 173 | nameTag = relation.get(n);
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 | if (nameTag != null) {
|
|---|
| 177 | break;
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|
| 180 | if (nameTag == null) {
|
|---|
| 181 | name += Long.toString(relation.getId()) + ", ";
|
|---|
| 182 | } else {
|
|---|
| 183 | name += "\"" + nameTag + "\", ";
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | int mbno = relation.getMembersCount();
|
|---|
| 187 | name += trn("{0} member", "{0} members", mbno, mbno) + ")";
|
|---|
| 188 | }
|
|---|
| 189 | name = decorateNameWithId(name, relation);
|
|---|
| 190 | return name;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /**
|
|---|
| 194 | * Formats a name for a changeset
|
|---|
| 195 | *
|
|---|
| 196 | * @param changeset the changeset
|
|---|
| 197 | * @return the name
|
|---|
| 198 | */
|
|---|
| 199 | public String format(Changeset changeset) {
|
|---|
| 200 | return tr("Changeset {0}",changeset.getId());
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|