source: josm/trunk/src/org/openstreetmap/josm/gui/DefaultNameFormatter.java@ 2512

Last change on this file since 2512 was 2512, checked in by stoecker, 15 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

File size: 6.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.HashSet;
10import java.util.List;
11import java.util.Set;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.coor.CoordinateFormat;
15import org.openstreetmap.josm.data.osm.Changeset;
16import org.openstreetmap.josm.data.osm.NameFormatter;
17import org.openstreetmap.josm.data.osm.Node;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.data.osm.Relation;
20import 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 */
26public 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 * 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.getId());
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.isNew() ? tr("node") : ""+ node.getId();
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 = way.getNodesCount();
134 if (nodesNo > 1 && way.isClosed()) {
135 nodesNo--;
136 }
137 String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
138 name += (name.length() > 0) ? " ("+nodes+")" : nodes;
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 Set<String> namingTags = new HashSet<String>(getNamingtagsForRelations());
163 for (String n : relation.keySet()) {
164 // #3328: "note " and " note" are name tags too
165 if (namingTags.contains(n.trim())) {
166 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
167 nameTag = relation.getLocalName();
168 } else {
169 nameTag = relation.getName();
170 }
171 if (nameTag == null) {
172 nameTag = relation.get(n);
173 }
174 }
175 if (nameTag != null) {
176 break;
177 }
178 }
179 if (nameTag == null) {
180 name += Long.toString(relation.getId()) + ", ";
181 } else {
182 name += "\"" + nameTag + "\", ";
183 }
184
185 int mbno = relation.getMembersCount();
186 name += trn("{0} member", "{0} members", mbno, mbno) + ")";
187 }
188 name = decorateNameWithId(name, relation);
189 return name;
190 }
191
192 /**
193 * Formats a name for a changeset
194 *
195 * @param changeset the changeset
196 * @return the name
197 */
198 public String format(Changeset changeset) {
199 return tr("Changeset {0}",changeset.getId());
200 }
201}
Note: See TracBrowser for help on using the repository browser.