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

Last change on this file since 2462 was 2462, checked in by stoecker, 14 years ago

fixed 3941 - build issue

File size: 7.0 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 /**
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 if(way.getDataSet().getErrors(way) != null) {
141 name = "*"+name;
142 }
143 }
144 name = decorateNameWithId(name, way);
145 return name;
146 }
147
148 /**
149 * Formats a name for a relation
150 *
151 * @param relation the relation
152 * @return the name
153 */
154 public String format(Relation relation) {
155 String name;
156 if (relation.incomplete) {
157 name = tr("incomplete");
158 } else {
159 name = relation.get("type");
160 if (name == null) {
161 name = tr("relation");
162 }
163
164 name += " (";
165 String nameTag = null;
166 Set<String> namingTags = new HashSet<String>(getNamingtagsForRelations());
167 for (String n : relation.keySet()) {
168 // #3328: "note " and " note" are name tags too
169 if (namingTags.contains(n.trim())) {
170 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
171 nameTag = relation.getLocalName();
172 } else {
173 nameTag = relation.getName();
174 }
175 if (nameTag == null) {
176 nameTag = relation.get(n);
177 }
178 }
179 if (nameTag != null) {
180 break;
181 }
182 }
183 if (nameTag == null) {
184 name += Long.toString(relation.getId()) + ", ";
185 } else {
186 name += "\"" + nameTag + "\", ";
187 }
188
189 int mbno = relation.getMembersCount();
190 name += trn("{0} member", "{0} members", mbno, mbno) + ")";
191 if(relation.getDataSet().getErrors(relation) != null) {
192 name = "*"+name;
193 }
194 }
195 name = decorateNameWithId(name, relation);
196 return name;
197 }
198
199 /**
200 * Formats a name for a changeset
201 *
202 * @param changeset the changeset
203 * @return the name
204 */
205 public String format(Changeset changeset) {
206 return tr("Changeset {0}",changeset.getId());
207 }
208}
Note: See TracBrowser for help on using the repository browser.