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

Last change on this file since 2627 was 2578, checked in by jttt, 14 years ago

Encalupse OsmPrimitive.incomplete

File size: 7.8 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.Collections;
10import java.util.HashSet;
11import java.util.List;
12import java.util.Set;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.coor.CoordinateFormat;
16import org.openstreetmap.josm.data.osm.Changeset;
17import org.openstreetmap.josm.data.osm.NameFormatter;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.Relation;
21import 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 */
27public 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 * 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.isIncomplete()) {
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.isIncomplete()) {
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 }
141 name = decorateNameWithId(name, way);
142 return name;
143 }
144
145 /**
146 * Formats a name for a relation
147 *
148 * @param relation the relation
149 * @return the name
150 */
151 public String format(Relation relation) {
152 String name;
153 if (relation.isIncomplete()) {
154 name = tr("incomplete");
155 } else {
156 name = relation.get("type");
157 if (name == null) {
158 name = tr("relation");
159 }
160
161 name += " (";
162 String nameTag = null;
163 Set<String> namingTags = new HashSet<String>(getNamingtagsForRelations());
164 for (String n : relation.keySet()) {
165 // #3328: "note " and " note" are name tags too
166 if (namingTags.contains(n.trim())) {
167 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) {
168 nameTag = relation.getLocalName();
169 } else {
170 nameTag = relation.getName();
171 }
172 if (nameTag == null) {
173 nameTag = relation.get(n);
174 }
175 }
176 if (nameTag != null) {
177 break;
178 }
179 }
180 if (nameTag == null) {
181 name += Long.toString(relation.getId()) + ", ";
182 } else {
183 name += "\"" + nameTag + "\", ";
184 }
185
186 int mbno = relation.getMembersCount();
187 name += trn("{0} member", "{0} members", mbno, mbno) + ")";
188 }
189 name = decorateNameWithId(name, relation);
190 return name;
191 }
192
193 /**
194 * Formats a name for a changeset
195 *
196 * @param changeset the changeset
197 * @return the name
198 */
199 public String format(Changeset changeset) {
200 return tr("Changeset {0}",changeset.getId());
201 }
202
203 /**
204 * Builds a default tooltip text for the primitive <code>primitive</code>.
205 *
206 * @param primitive the primitmive
207 * @return the tooltip text
208 */
209 public String buildDefaultToolTip(OsmPrimitive primitive) {
210 StringBuilder sb = new StringBuilder();
211 sb.append("<html>");
212 sb.append("<strong>id</strong>=")
213 .append(primitive.getId())
214 .append("<br>");
215 ArrayList<String> keyList = new ArrayList<String>(primitive.keySet());
216 Collections.sort(keyList);
217 for (int i = 0; i < keyList.size(); i++) {
218 if (i > 0) {
219 sb.append("<br>");
220 }
221 String key = keyList.get(i);
222 sb.append("<strong>")
223 .append(key)
224 .append("</strong>")
225 .append("=");
226 String value = primitive.get(key);
227 while(value.length() != 0) {
228 sb.append(value.substring(0,Math.min(50, value.length())));
229 if (value.length() > 50) {
230 sb.append("<br>");
231 value = value.substring(50);
232 } else {
233 value = "";
234 }
235 }
236 }
237 sb.append("</html>");
238 return sb.toString();
239 }
240}
Note: See TracBrowser for help on using the repository browser.