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