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