Changeset 4070 in josm
- Timestamp:
- 2011-05-03T08:02:54+02:00 (14 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/NameFormatter.java
r3083 r4070 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.data.osm; 3 4 import java.util.Comparator; 3 5 4 6 public interface NameFormatter { … … 7 9 String format(Relation relation); 8 10 String format(Changeset changeset); 11 12 Comparator<Node> getNodeComparator(); 13 Comparator<Way> getWayComparator(); 14 Comparator<Relation> getRelationComparator(); 9 15 } -
trunk/src/org/openstreetmap/josm/gui/DefaultNameFormatter.java
r3918 r4070 9 9 import java.util.Arrays; 10 10 import java.util.Collections; 11 import java.util.Comparator; 11 12 import java.util.HashSet; 12 13 import java.util.List; … … 51 52 /** the default list of tags which are used as naming tags in relations */ 52 53 static public final String[] DEFAULT_NAMING_TAGS_FOR_RELATIONS = {"name", "ref", "restriction", "landuse", "natural", 53 "public_transport", ":LocationCode", "note"};54 "public_transport", ":LocationCode", "note"}; 54 55 55 56 /** the current list of tags used as naming tags in relations */ … … 138 139 } 139 140 141 private final Comparator<Node> nodeComparator = new Comparator<Node>() { 142 @Override 143 public int compare(Node n1, Node n2) { 144 return format(n1).compareTo(format(n2)); 145 } 146 }; 147 148 public Comparator<Node> getNodeComparator() { 149 return nodeComparator; 150 } 151 152 140 153 /** 141 154 * Formats a name for a way … … 189 202 nodesNo--; 190 203 } 191 if(name == null || name.length() == 0) 204 if(name == null || name.length() == 0) { 192 205 name = String.valueOf(way.getId()); 206 } 193 207 /* note: length == 0 should no longer happen, but leave the bracket code 194 208 nevertheless, who knows what future brings */ … … 201 215 } 202 216 217 private final Comparator<Way> wayComparator = new Comparator<Way>() { 218 @Override 219 public int compare(Way w1, Way w2) { 220 return format(w1).compareTo(format(w2)); 221 } 222 }; 223 224 public Comparator<Way> getWayComparator() { 225 return wayComparator; 226 } 227 228 203 229 /** 204 230 * Formats a name for a relation … … 212 238 name = tr("incomplete"); 213 239 } else { 214 name = trc("Relation type", relation.get("type")); 215 if (name == null) { 216 name = (relation.get("public_transport") != null) ? tr("public transport") : ""; 217 } 218 if (name == null) { 219 String building = relation.get("building"); 220 if(OsmUtils.isTrue(building)) 221 name = tr("building"); 222 else if(building != null) 223 name = tr(building); // translate tag! 224 } 225 if (name == null) { 226 name = trc("Place type", relation.get("place")); 227 } 228 if (name == null) { 229 name = tr("relation"); 230 } 231 String admin_level = relation.get("admin_level"); 232 if (admin_level != null) { 233 name += "["+admin_level+"]"; 234 } 235 236 name += " ("; 237 String nameTag = null; 238 for (String n : getNamingtagsForRelations()) { 239 if (n.equals("name")) { 240 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) { 241 nameTag = relation.getLocalName(); 242 } else { 243 nameTag = relation.getName(); 244 } 245 } else if (n.equals(":LocationCode")) { 246 for (String m : relation.keySet()) { 247 if (m.endsWith(n)) { 248 nameTag = relation.get(m); 249 break; 250 } 251 } 252 } else { 253 String str = relation.get(n); 254 if(str != null) 255 nameTag = tr(str); 256 } 257 if (nameTag != null) { 258 break; 259 } 260 } 261 if (nameTag == null) { 262 name += Long.toString(relation.getId()) + ", "; 240 name = getRelationTypeName(relation); 241 String relationName = getRelationName(relation); 242 if (relationName == null) { 243 relationName = Long.toString(relation.getId()); 263 244 } else { 264 name += "\"" + nameTag + "\", "; 265 } 245 relationName = "\"" + relationName + "\""; 246 } 247 name += " (" + relationName + ", "; 266 248 267 249 int mbno = relation.getMembersCount(); 268 250 name += trn("{0} member", "{0} members", mbno, mbno); 269 251 270 boolean incomplete = false; 271 for (RelationMember m : relation.getMembers()) { 272 if (m.getMember().isIncomplete()) { 273 incomplete = true; 274 break; 275 } 276 } 277 if (incomplete) { 252 if (relationHasIncompleteMember(relation)) { 278 253 name += ", "+tr("incomplete"); 279 254 } … … 283 258 name = decorateNameWithId(name, relation); 284 259 return name; 260 } 261 262 private final Comparator<Relation> relationComparator = new Comparator<Relation>() { 263 @Override 264 public int compare(Relation r1, Relation r2) { 265 String type1 = getRelationTypeName(r1); 266 String type2 = getRelationTypeName(r2); 267 268 int comp = type1.compareTo(type2); 269 if (comp != 0) 270 return comp; 271 272 String name1 = getRelationName(r1); 273 String name2 = getRelationName(r2); 274 275 if (name1 == null && name2 == null) 276 return (r1.getUniqueId() > r2.getUniqueId())?1:-1; 277 else if (name1 == null) 278 return -1; 279 else if (name2 == null) 280 return 1; 281 else if (!name1.isEmpty() && !name2.isEmpty() && Character.isDigit(name1.charAt(0)) && Character.isDigit(name2.charAt(0))) { 282 //Compare numerically 283 String ln1 = getLeadingNumber(name1); 284 String ln2 = getLeadingNumber(name2); 285 286 comp = Long.valueOf(ln1).compareTo(Long.valueOf(ln2)); 287 if (comp != 0) 288 return comp; 289 290 // put 1 before 0001 291 comp = ln1.compareTo(ln2); 292 if (comp != 0) 293 return comp; 294 295 comp = name1.substring(ln1.length()).compareTo(name2.substring(ln2.length())); 296 if (comp != 0) 297 return comp; 298 } else { 299 comp = name1.compareToIgnoreCase(name2); 300 if (comp != 0) 301 return comp; 302 } 303 304 if (r1.getMembersCount() != r2.getMembersCount()) 305 return (r1.getMembersCount() > r2.getMembersCount())?1:-1; 306 307 comp = Boolean.valueOf(relationHasIncompleteMember(r1)).compareTo(Boolean.valueOf(relationHasIncompleteMember(r2))); 308 if (comp != 0) 309 return comp; 310 311 return r1.getUniqueId() > r2.getUniqueId()?1:-1; 312 } 313 }; 314 315 public Comparator<Relation> getRelationComparator() { 316 return relationComparator; 317 } 318 319 private String getLeadingNumber(String s) { 320 int i = 0; 321 while (i < s.length() && Character.isDigit(s.charAt(i))) { 322 i++; 323 } 324 return s.substring(0, i); 325 } 326 327 private String getRelationTypeName(Relation relation) { 328 String name = trc("Relation type", relation.get("type")); 329 if (name == null) { 330 name = (relation.get("public_transport") != null) ? tr("public transport") : null; 331 } 332 if (name == null) { 333 String building = relation.get("building"); 334 if(OsmUtils.isTrue(building)) { 335 name = tr("building"); 336 } else if(building != null) 337 { 338 name = tr(building); // translate tag! 339 } 340 } 341 if (name == null) { 342 name = trc("Place type", relation.get("place")); 343 } 344 if (name == null) { 345 name = tr("relation"); 346 } 347 String admin_level = relation.get("admin_level"); 348 if (admin_level != null) { 349 name += "["+admin_level+"]"; 350 } 351 352 return name; 353 } 354 355 private String getNameTagValue(Relation relation, String nameTag) { 356 if (nameTag.equals("name")) { 357 if (Main.pref.getBoolean("osm-primitives.localize-name", true)) 358 return relation.getLocalName(); 359 else 360 return relation.getName(); 361 } else if (nameTag.equals(":LocationCode")) { 362 for (String m : relation.keySet()) { 363 if (m.endsWith(nameTag)) 364 return relation.get(m); 365 } 366 return null; 367 } else 368 return relation.get(nameTag); 369 } 370 371 private String getRelationName(Relation relation) { 372 String nameTag = null; 373 for (String n : getNamingtagsForRelations()) { 374 nameTag = getNameTagValue(relation, n); 375 if (nameTag != null) 376 return nameTag; 377 } 378 return null; 379 } 380 381 private boolean relationHasIncompleteMember(Relation relation) { 382 for (RelationMember m : relation.getMembers()) { 383 if (m.getMember().isIncomplete()) 384 return true; 385 } 386 return false; 285 387 } 286 388 … … 407 509 int nodesNo = way.isClosed() ? way.getNumNodes() -1 : way.getNumNodes(); 408 510 String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo); 409 if(sb.length() == 0 ) 511 if(sb.length() == 0 ) { 410 512 sb.append(way.getId()); 513 } 411 514 /* note: length == 0 should no longer happen, but leave the bracket code 412 515 nevertheless, who knows what future brings */ -
trunk/src/org/openstreetmap/josm/gui/dialogs/RelationListDialog.java
r3719 r4070 34 34 35 35 import org.openstreetmap.josm.Main; 36 import org.openstreetmap.josm.data.osm.NameFormatter;37 36 import org.openstreetmap.josm.data.osm.OsmPrimitive; 38 37 import org.openstreetmap.josm.data.osm.Relation; … … 42 41 import org.openstreetmap.josm.data.osm.event.DataSetListener; 43 42 import org.openstreetmap.josm.data.osm.event.DatasetEventManager; 43 import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode; 44 44 import org.openstreetmap.josm.data.osm.event.NodeMovedEvent; 45 45 import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent; … … 48 48 import org.openstreetmap.josm.data.osm.event.TagsChangedEvent; 49 49 import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent; 50 import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;51 50 import org.openstreetmap.josm.gui.DefaultNameFormatter; 52 51 import org.openstreetmap.josm.gui.MapView; 52 import org.openstreetmap.josm.gui.MapView.LayerChangeListener; 53 53 import org.openstreetmap.josm.gui.OsmPrimitivRenderer; 54 54 import org.openstreetmap.josm.gui.SideButton; 55 import org.openstreetmap.josm.gui.MapView.LayerChangeListener;56 55 import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask; 57 56 import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask; … … 91 90 super(tr("Relations"), "relationlist", tr("Open a list of all relations."), 92 91 Shortcut.registerShortcut("subwindow:relations", tr("Toggle: {0}", tr("Relations")), KeyEvent.VK_R, 93 Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT), 150);92 Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT), 150); 94 93 95 94 // create the list of relations … … 578 577 Collections.sort( 579 578 relations, 580 new Comparator<Relation>() { 581 NameFormatter formatter = DefaultNameFormatter.getInstance(); 582 583 public int compare(Relation r1, Relation r2) { 584 return r1.getDisplayName(formatter).compareTo(r2.getDisplayName(formatter)); 585 } 586 } 579 DefaultNameFormatter.getInstance().getRelationComparator() 587 580 ); 588 581 }
Note:
See TracChangeset
for help on using the changeset viewer.