Changeset 7186 in josm
- Timestamp:
- 2014-05-29T08:49:43+02:00 (10 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/imagery/ImageryInfo.java
r7005 r7186 10 10 import java.util.Collections; 11 11 import java.util.List; 12 import java.util.Objects; 12 13 import java.util.regex.Matcher; 13 14 import java.util.regex.Pattern; … … 146 147 } 147 148 } 148 149 149 150 /** name of the imagery entry (gets translated by josm usually) */ 150 151 private String name; 151 152 /** original name of the imagery entry in case of translation call */ 152 153 private String origName; 154 /** id for this imagery entry, optional at the moment */ 155 private String id; 153 156 private String url = null; 154 157 private boolean defaultEntry = false; … … 176 179 public static class ImageryPreferenceEntry { 177 180 @pref String name; 181 @pref String id; 178 182 @pref String type; 179 183 @pref String url; … … 207 211 public ImageryPreferenceEntry(ImageryInfo i) { 208 212 name = i.name; 213 id = i.id; 209 214 type = i.imageryType.getTypeString(); 210 215 url = i.url; … … 249 254 @Override 250 255 public String toString() { 251 return "ImageryPreferenceEntry [name=" + name + "]"; 256 String s = "ImageryPreferenceEntry [name=" + name; 257 if (id != null) { 258 s += " id=" + id; 259 } 260 s += "]"; 261 return s; 252 262 } 253 263 } … … 308 318 CheckParameterUtil.ensureParameterNotNull(e.url, "url"); 309 319 name = e.name; 320 id = e.id; 310 321 url = e.url; 311 322 cookies = e.cookies; … … 347 358 public ImageryInfo(ImageryInfo i) { 348 359 this.name = i.name; 360 this.id = i.id; 349 361 this.url = i.url; 350 362 this.defaultEntry = i.defaultEntry; … … 380 392 return true; 381 393 } 382 394 395 /** 396 * Check if this object equals another ImageryInfo with respect to the properties 397 * that get written to the preference file. 398 * 399 * The field {@link #pixelPerDegree} is ignored. 400 * 401 * @param other the ImageryInfo object to compare to 402 * @return true if they are equal 403 */ 404 public boolean equalsPref(ImageryInfo other) { 405 if (other == null) { 406 return false; 407 } 408 if (!Objects.equals(this.name, other.name)) { 409 return false; 410 } 411 if (!Objects.equals(this.id, other.id)) { 412 return false; 413 } 414 if (!Objects.equals(this.url, other.url)) { 415 return false; 416 } 417 if (!Objects.equals(this.cookies, other.cookies)) { 418 return false; 419 } 420 if (!Objects.equals(this.eulaAcceptanceRequired, other.eulaAcceptanceRequired)) { 421 return false; 422 } 423 if (this.imageryType != other.imageryType) { 424 return false; 425 } 426 if (this.defaultMaxZoom != other.defaultMaxZoom) { 427 return false; 428 } 429 if (this.defaultMinZoom != other.defaultMinZoom) { 430 return false; 431 } 432 if (!Objects.equals(this.bounds, other.bounds)) { 433 return false; 434 } 435 if (!Objects.equals(this.serverProjections, other.serverProjections)) { 436 return false; 437 } 438 if (!Objects.equals(this.attributionText, other.attributionText)) { 439 return false; 440 } 441 if (!Objects.equals(this.attributionLinkURL, other.attributionLinkURL)) { 442 return false; 443 } 444 if (!Objects.equals(this.attributionImage, other.attributionImage)) { 445 return false; 446 } 447 if (!Objects.equals(this.attributionImageURL, other.attributionImageURL)) { 448 return false; 449 } 450 if (!Objects.equals(this.termsOfUseText, other.termsOfUseText)) { 451 return false; 452 } 453 if (!Objects.equals(this.termsOfUseURL, other.termsOfUseURL)) { 454 return false; 455 } 456 if (!Objects.equals(this.countryCode, other.countryCode)) { 457 return false; 458 } 459 if (!Objects.equals(this.icon, other.icon)) { 460 return false; 461 } 462 return true; 463 } 464 465 383 466 @Override 384 467 public int hashCode() { … … 594 677 595 678 /** 679 * Gets the entry id. 680 * 681 * Id can be null. This gets the configured id as is. Due to a user error, 682 * this may not be unique. Use {@link ImageryLayerInfo#getUniqueId} to ensure 683 * a unique value. 684 * @return the id 685 */ 686 public String getId() { 687 return this.id; 688 } 689 690 /** 691 * Sets the entry id. 692 * @param id the entry id 693 */ 694 public void setId(String id) { 695 this.id = id; 696 } 697 698 /** 596 699 * Returns the entry URL. 597 700 * @return The entry URL -
trunk/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java
r7083 r7186 7 7 import java.util.Collection; 8 8 import java.util.Collections; 9 import java.util.HashMap; 10 import java.util.HashSet; 9 11 import java.util.List; 12 import java.util.Map; 10 13 import java.util.Objects; 11 14 import java.util.Set; 15 import java.util.TreeSet; 12 16 import org.openstreetmap.josm.Main; 13 17 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry; … … 22 26 23 27 public static final ImageryLayerInfo instance = new ImageryLayerInfo(); 24 List<ImageryInfo> layers = new ArrayList<>(); 25 static List<ImageryInfo> defaultLayers = new ArrayList<>(); 28 private final List<ImageryInfo> layers = new ArrayList<>(); 29 private final Map<String, ImageryInfo> layerIds = new HashMap<>(); 30 private final static List<ImageryInfo> defaultLayers = new ArrayList<>(); 31 private final static Map<String, ImageryInfo> defaultLayerIds = new HashMap<>(); 26 32 27 33 private static final String[] DEFAULT_LAYER_SITES = { … … 38 44 public void clear() { 39 45 layers.clear(); 46 layerIds.clear(); 40 47 } 41 48 42 49 public void load() { 43 boolean addedDefault = !layers.isEmpty();50 clear(); 44 51 List<ImageryPreferenceEntry> entries = Main.pref.getListOfStructs("imagery.entries", null, ImageryPreferenceEntry.class); 45 52 if (entries != null) { … … 54 61 Collections.sort(layers); 55 62 } 56 if (addedDefault) { 57 save(); 58 } 63 loadDefaults(false); 59 64 } 60 65 … … 70 75 public void loadDefaults(boolean clearCache) { 71 76 defaultLayers.clear(); 77 defaultLayerIds.clear(); 72 78 for (String source : Main.pref.getCollection("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES))) { 73 79 if (clearCache) { … … 80 86 } catch (IOException ex) { 81 87 Main.error(ex, false); 82 continue;83 88 } catch (SAXException ex) { 84 89 Main.error(ex); 85 continue;86 90 } 87 91 } 88 92 while (defaultLayers.remove(null)); 89 90 Collection<String> defaults = Main.pref.getCollection("imagery.layers.default"); 91 List<String> defaultsSave = new ArrayList<>(); 93 Collections.sort(defaultLayers); 94 buildIdMap(defaultLayers, defaultLayerIds); 95 updateEntriesFromDefaults(); 96 buildIdMap(layers, layerIds); 97 } 98 99 /** 100 * Build the mapping of unique ids to {@link ImageryInfo}s. 101 * @param lst input list 102 * @param idMap output map 103 */ 104 private static void buildIdMap(List<ImageryInfo> lst, Map<String, ImageryInfo> idMap) { 105 idMap.clear(); 106 Set<String> notUnique = new HashSet<>(); 107 for (ImageryInfo i : lst) { 108 if (i.getId() != null) { 109 if (idMap.containsKey(i.getId())) { 110 notUnique.add(i.getId()); 111 Main.error("Id ''{0}'' is not unique - used by ''{1}'' and ''{2}''!", 112 i.getId(), i.getName(), idMap.get(i.getId()).getName()); 113 continue; 114 } 115 idMap.put(i.getId(), i); 116 } 117 } 118 for (String i : notUnique) { 119 idMap.remove(i); 120 } 121 } 122 123 /** 124 * Update user entries according to the list of default entries. 125 */ 126 public void updateEntriesFromDefaults() { 127 // add new default entries to the user selection 128 boolean changed = false; 129 Collection<String> knownDefaults = Main.pref.getCollection("imagery.layers.default"); 130 Collection<String> newKnownDefaults = new TreeSet<>(knownDefaults); 92 131 for (ImageryInfo def : defaultLayers) { 93 132 if (def.isDefaultEntry()) { 94 defaultsSave.add(def.getUrl());95 96 133 boolean isKnownDefault = false; 97 for (String url : defaults) {134 for (String url : knownDefaults) { 98 135 if (isSimilar(url, def.getUrl())) { 99 136 isKnownDefault = true; … … 103 140 boolean isInUserList = false; 104 141 if (!isKnownDefault) { 142 newKnownDefaults.add(def.getUrl()); 105 143 for (ImageryInfo i : layers) { 106 if (isSimilar(def .getUrl(), i.getUrl())) {144 if (isSimilar(def, i)) { 107 145 isInUserList = true; 108 146 break; … … 112 150 if (!isKnownDefault && !isInUserList) { 113 151 add(new ImageryInfo(def)); 114 } 115 } 116 } 117 118 Collections.sort(defaultLayers); 119 Main.pref.putCollection("imagery.layers.default", defaultsSave.isEmpty() ? defaults : defaultsSave); 120 } 121 152 changed = true; 153 } 154 } 155 } 156 Main.pref.putCollection("imagery.layers.default", newKnownDefaults); 157 158 // Add ids to user entries without id. 159 // Only do this the first time for each id, so the user can have 160 // custom entries that don't get updated automatically 161 Collection<String> addedIds = Main.pref.getCollection("imagery.layers.addedIds"); 162 Collection<String> newAddedIds = new TreeSet<>(addedIds); 163 for (ImageryInfo info : layers) { 164 for (ImageryInfo def : defaultLayers) { 165 if (isSimilar(def, info)) { 166 if (def.getId() != null && !addedIds.contains(def.getId())) { 167 if (!defaultLayerIds.containsKey(def.getId())) { 168 // ignore ids used more than once (have been purged from the map) 169 continue; 170 } 171 newAddedIds.add(def.getId()); 172 if (info.getId() == null) { 173 info.setId(def.getId()); 174 changed = true; 175 } 176 } 177 } 178 } 179 } 180 Main.pref.putCollection("imagery.layers.migration.addedIds", newAddedIds); 181 182 // automatically update user entries with same id as a default entry 183 for (int i=0; i<layers.size(); i++) { 184 ImageryInfo info = layers.get(i); 185 if (info.getId() == null) { 186 continue; 187 } 188 ImageryInfo matchingDefault = defaultLayerIds.get(info.getId()); 189 if (matchingDefault != null && !matchingDefault.equalsPref(info)) { 190 layers.set(i, matchingDefault); 191 changed = true; 192 } 193 } 194 195 if (changed) { 196 save(); 197 } 198 } 199 200 private boolean isSimilar(ImageryInfo iiA, ImageryInfo iiB) { 201 if (iiA.getId() != null && iiB.getId() != null) return iiA.getId().equals(iiB.getId()); 202 return isSimilar(iiA.getUrl(), iiB.getUrl()); 203 } 204 122 205 // some additional checks to respect extended URLs in preferences (legacy workaround) 123 206 private boolean isSimilar(String a, String b) { 124 207 return Objects.equals(a, b) || (a != null && b != null && !a.isEmpty() && !b.isEmpty() && (a.contains(b) || b.contains(a))); 125 208 } 126 209 127 210 public void add(ImageryInfo info) { 128 211 layers.add(info); … … 161 244 Collections.sort(instance.layers); 162 245 } 246 247 /** 248 * Get unique id for ImageryInfo. 249 * 250 * This takes care, that no id is used twice (due to a user error) 251 * @param info the ImageryInfo to look up 252 * @return null, if there is no id or the id is used twice, 253 * the corresponding id otherwise 254 */ 255 public String getUniqueId(ImageryInfo info) { 256 if (info.getId() != null && layerIds.get(info.getId()) == info) { 257 return info.getId(); 258 } 259 return null; 260 } 163 261 } -
trunk/src/org/openstreetmap/josm/data/imagery/Shape.java
r7005 r7186 7 7 import java.util.ArrayList; 8 8 import java.util.List; 9 import java.util.Objects; 9 10 10 11 import org.openstreetmap.gui.jmapviewer.Coordinate; … … 84 85 coords.add(new Coordinate(LatLon.roundToOsmPrecision(lat), LatLon.roundToOsmPrecision(lon))); 85 86 } 87 88 @Override 89 public int hashCode() { 90 int hash = 5; 91 hash = 47 * hash + Objects.hashCode(this.coords); 92 return hash; 93 } 94 95 @Override 96 public boolean equals(Object obj) { 97 if (obj == null) { 98 return false; 99 } 100 if (getClass() != obj.getClass()) { 101 return false; 102 } 103 final Shape other = (Shape) obj; 104 if (!Objects.equals(this.coords, other.coords)) { 105 return false; 106 } 107 return true; 108 } 109 110 86 111 } -
trunk/src/org/openstreetmap/josm/gui/ImageryMenu.java
r7176 r7186 80 80 }; 81 81 82 private JMenuItem singleOffset = new JMenuItem(offsetAction);82 private final JMenuItem singleOffset = new JMenuItem(offsetAction); 83 83 private JMenuItem offsetMenuItem = singleOffset; 84 private MapRectifierWMSmenuAction rectaction = new MapRectifierWMSmenuAction();84 private final MapRectifierWMSmenuAction rectaction = new MapRectifierWMSmenuAction(); 85 85 86 86 public ImageryMenu(JMenu subMenu) { -
trunk/src/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreference.java
r7005 r7186 880 880 */ 881 881 public static void initialize() { 882 ImageryLayerInfo.instance.clear();883 ImageryLayerInfo.instance.loadDefaults(false);884 882 ImageryLayerInfo.instance.load(); 885 883 OffsetBookmark.loadBookmarks(); -
trunk/src/org/openstreetmap/josm/io/imagery/ImageryReader.java
r7083 r7186 112 112 if (Arrays.asList(new String[] { 113 113 "name", 114 "id", 114 115 "type", 115 116 "default", … … 204 205 entry.setTranslatedName(accumulator.toString()); 205 206 break; 207 case "id": 208 entry.setId(accumulator.toString()); 209 break; 206 210 case "type": 207 211 boolean found = false;
Note:
See TracChangeset
for help on using the changeset viewer.