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.DataSet;
|
---|
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"))
|
---|
78 | return name + tr(" [id: {0}]", primitive.getId());
|
---|
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) {
|
---|
100 | name = node.isNew() ? tr("node") : ""+ node.getId();
|
---|
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 |
|
---|
135 | int nodesNo = way.getNodesCount();
|
---|
136 | if (nodesNo > 1 && way.isClosed()) {
|
---|
137 | nodesNo--;
|
---|
138 | }
|
---|
139 | String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
|
---|
140 | name += (name.length() > 0) ? " ("+nodes+")" : nodes;
|
---|
141 | DataSet data = way.getDataSet();
|
---|
142 | if (data != null && data.getErrors(way) != null) {
|
---|
143 | name = "*"+name;
|
---|
144 | }
|
---|
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.incomplete) {
|
---|
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 | DataSet data = relation.getDataSet();
|
---|
194 | if(data != null && data.getErrors(relation) != null) {
|
---|
195 | name = "*"+name;
|
---|
196 | }
|
---|
197 | }
|
---|
198 | name = decorateNameWithId(name, relation);
|
---|
199 | return name;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Formats a name for a changeset
|
---|
204 | *
|
---|
205 | * @param changeset the changeset
|
---|
206 | * @return the name
|
---|
207 | */
|
---|
208 | public String format(Changeset changeset) {
|
---|
209 | return tr("Changeset {0}",changeset.getId());
|
---|
210 | }
|
---|
211 | }
|
---|