source: josm/trunk/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java@ 12851

Last change on this file since 12851 was 12851, checked in by bastiK, 7 years ago

see #15229 - extract "struct" handling from Preference to StructUtils

  • Property svn:eol-style set to native
File size: 14.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.HashMap;
12import java.util.HashSet;
13import java.util.List;
14import java.util.Map;
15import java.util.Objects;
16import java.util.Set;
17import java.util.TreeSet;
18import java.util.concurrent.ExecutorService;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.data.StructUtils;
22import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
23import org.openstreetmap.josm.gui.PleaseWaitRunnable;
24import org.openstreetmap.josm.io.CachedFile;
25import org.openstreetmap.josm.io.OfflineAccessException;
26import org.openstreetmap.josm.io.OnlineResource;
27import org.openstreetmap.josm.io.imagery.ImageryReader;
28import org.openstreetmap.josm.spi.preferences.Config;
29import org.openstreetmap.josm.tools.Logging;
30import org.openstreetmap.josm.tools.Utils;
31import org.xml.sax.SAXException;
32
33/**
34 * Manages the list of imagery entries that are shown in the imagery menu.
35 */
36public class ImageryLayerInfo {
37
38 /** Unique instance */
39 public static final ImageryLayerInfo instance = new ImageryLayerInfo();
40 /** List of all usable layers */
41 private final List<ImageryInfo> layers = new ArrayList<>();
42 /** List of layer ids of all usable layers */
43 private final Map<String, ImageryInfo> layerIds = new HashMap<>();
44 /** List of all available default layers */
45 private static final List<ImageryInfo> defaultLayers = new ArrayList<>();
46 /** List of all available default layers (including mirrors) */
47 private static final List<ImageryInfo> allDefaultLayers = new ArrayList<>();
48 /** List of all layer ids of available default layers (including mirrors) */
49 private static final Map<String, ImageryInfo> defaultLayerIds = new HashMap<>();
50
51 private static final String[] DEFAULT_LAYER_SITES = {
52 Main.getJOSMWebsite()+"/maps"
53 };
54
55 /**
56 * Returns the list of imagery layers sites.
57 * @return the list of imagery layers sites
58 * @since 7434
59 */
60 public static Collection<String> getImageryLayersSites() {
61 return Config.getPref().getList("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES));
62 }
63
64 private ImageryLayerInfo() {
65 }
66
67 /**
68 * Constructs a new {@code ImageryLayerInfo} from an existing one.
69 * @param info info to copy
70 */
71 public ImageryLayerInfo(ImageryLayerInfo info) {
72 layers.addAll(info.layers);
73 }
74
75 /**
76 * Clear the lists of layers.
77 */
78 public void clear() {
79 layers.clear();
80 layerIds.clear();
81 }
82
83 /**
84 * Loads the custom as well as default imagery entries.
85 * @param fastFail whether opening HTTP connections should fail fast, see {@link ImageryReader#setFastFail(boolean)}
86 */
87 public void load(boolean fastFail) {
88 clear();
89 List<ImageryPreferenceEntry> entries = StructUtils.getListOfStructs(
90 Config.getPref(), "imagery.entries", null, ImageryPreferenceEntry.class);
91 if (entries != null) {
92 for (ImageryPreferenceEntry prefEntry : entries) {
93 try {
94 ImageryInfo i = new ImageryInfo(prefEntry);
95 add(i);
96 } catch (IllegalArgumentException e) {
97 Logging.warn("Unable to load imagery preference entry:"+e);
98 }
99 }
100 Collections.sort(layers);
101 }
102 loadDefaults(false, null, fastFail);
103 }
104
105 /**
106 * Loads the available imagery entries.
107 *
108 * The data is downloaded from the JOSM website (or loaded from cache).
109 * Entries marked as "default" are added to the user selection, if not already present.
110 *
111 * @param clearCache if true, clear the cache and start a fresh download.
112 * @param worker executor service which will perform the loading.
113 * If null, it should be performed using a {@link PleaseWaitRunnable} in the background
114 * @param fastFail whether opening HTTP connections should fail fast, see {@link ImageryReader#setFastFail(boolean)}
115 * @since 12634
116 */
117 public void loadDefaults(boolean clearCache, ExecutorService worker, boolean fastFail) {
118 final DefaultEntryLoader loader = new DefaultEntryLoader(clearCache, fastFail);
119 if (worker == null) {
120 loader.realRun();
121 loader.finish();
122 } else {
123 worker.execute(loader);
124 }
125 }
126
127 /**
128 * Loader/updater of the available imagery entries
129 */
130 class DefaultEntryLoader extends PleaseWaitRunnable {
131
132 private final boolean clearCache;
133 private final boolean fastFail;
134 private final List<ImageryInfo> newLayers = new ArrayList<>();
135 private ImageryReader reader;
136 private boolean canceled;
137 private boolean loadError;
138
139 DefaultEntryLoader(boolean clearCache, boolean fastFail) {
140 super(tr("Update default entries"));
141 this.clearCache = clearCache;
142 this.fastFail = fastFail;
143 }
144
145 @Override
146 protected void cancel() {
147 canceled = true;
148 Utils.close(reader);
149 }
150
151 @Override
152 protected void realRun() {
153 for (String source : getImageryLayersSites()) {
154 if (canceled) {
155 return;
156 }
157 loadSource(source);
158 }
159 }
160
161 protected void loadSource(String source) {
162 boolean online = true;
163 try {
164 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(source, Main.getJOSMWebsite());
165 } catch (OfflineAccessException e) {
166 Logging.log(Logging.LEVEL_WARN, e);
167 online = false;
168 }
169 if (clearCache && online) {
170 CachedFile.cleanup(source);
171 }
172 try {
173 reader = new ImageryReader(source);
174 reader.setFastFail(fastFail);
175 Collection<ImageryInfo> result = reader.parse();
176 newLayers.addAll(result);
177 } catch (IOException ex) {
178 loadError = true;
179 Logging.log(Logging.LEVEL_ERROR, ex);
180 } catch (SAXException ex) {
181 loadError = true;
182 Logging.error(ex);
183 }
184 }
185
186 @Override
187 protected void finish() {
188 defaultLayers.clear();
189 allDefaultLayers.clear();
190 defaultLayers.addAll(newLayers);
191 for (ImageryInfo layer : newLayers) {
192 allDefaultLayers.add(layer);
193 for (ImageryInfo sublayer : layer.getMirrors()) {
194 allDefaultLayers.add(sublayer);
195 }
196 }
197 defaultLayerIds.clear();
198 Collections.sort(defaultLayers);
199 Collections.sort(allDefaultLayers);
200 buildIdMap(allDefaultLayers, defaultLayerIds);
201 updateEntriesFromDefaults(!loadError);
202 buildIdMap(layers, layerIds);
203 if (!loadError && !defaultLayerIds.isEmpty()) {
204 dropOldEntries();
205 }
206 }
207 }
208
209 /**
210 * Build the mapping of unique ids to {@link ImageryInfo}s.
211 * @param lst input list
212 * @param idMap output map
213 */
214 private static void buildIdMap(List<ImageryInfo> lst, Map<String, ImageryInfo> idMap) {
215 idMap.clear();
216 Set<String> notUnique = new HashSet<>();
217 for (ImageryInfo i : lst) {
218 if (i.getId() != null) {
219 if (idMap.containsKey(i.getId())) {
220 notUnique.add(i.getId());
221 Logging.error("Id ''{0}'' is not unique - used by ''{1}'' and ''{2}''!",
222 i.getId(), i.getName(), idMap.get(i.getId()).getName());
223 continue;
224 }
225 idMap.put(i.getId(), i);
226 }
227 }
228 for (String i : notUnique) {
229 idMap.remove(i);
230 }
231 }
232
233 /**
234 * Update user entries according to the list of default entries.
235 * @param dropold if <code>true</code> old entries should be removed
236 * @since 11706
237 */
238 public void updateEntriesFromDefaults(boolean dropold) {
239 // add new default entries to the user selection
240 boolean changed = false;
241 Collection<String> knownDefaults = new TreeSet<>(Config.getPref().getList("imagery.layers.default"));
242 Collection<String> newKnownDefaults = new TreeSet<>();
243 for (ImageryInfo def : defaultLayers) {
244 if (def.isDefaultEntry()) {
245 boolean isKnownDefault = false;
246 for (String entry : knownDefaults) {
247 if (entry.equals(def.getId())) {
248 isKnownDefault = true;
249 newKnownDefaults.add(entry);
250 knownDefaults.remove(entry);
251 break;
252 } else if (isSimilar(entry, def.getUrl())) {
253 isKnownDefault = true;
254 if (def.getId() != null) {
255 newKnownDefaults.add(def.getId());
256 }
257 knownDefaults.remove(entry);
258 break;
259 }
260 }
261 boolean isInUserList = false;
262 if (!isKnownDefault) {
263 if (def.getId() != null) {
264 newKnownDefaults.add(def.getId());
265 for (ImageryInfo i : layers) {
266 if (isSimilar(def, i)) {
267 isInUserList = true;
268 break;
269 }
270 }
271 } else {
272 Logging.error("Default imagery ''{0}'' has no id. Skipping.", def.getName());
273 }
274 }
275 if (!isKnownDefault && !isInUserList) {
276 add(new ImageryInfo(def));
277 changed = true;
278 }
279 }
280 }
281 if (!dropold && !knownDefaults.isEmpty()) {
282 newKnownDefaults.addAll(knownDefaults);
283 }
284 Config.getPref().putList("imagery.layers.default", new ArrayList<>(newKnownDefaults));
285
286 // automatically update user entries with same id as a default entry
287 for (int i = 0; i < layers.size(); i++) {
288 ImageryInfo info = layers.get(i);
289 if (info.getId() == null) {
290 continue;
291 }
292 ImageryInfo matchingDefault = defaultLayerIds.get(info.getId());
293 if (matchingDefault != null && !matchingDefault.equalsPref(info)) {
294 layers.set(i, matchingDefault);
295 Logging.info(tr("Update imagery ''{0}''", info.getName()));
296 changed = true;
297 }
298 }
299
300 if (changed) {
301 save();
302 }
303 }
304
305 /**
306 * Drop entries with Id which do no longer exist (removed from defaults).
307 * @since 11527
308 */
309 public void dropOldEntries() {
310 List<String> drop = new ArrayList<>();
311
312 for (Map.Entry<String, ImageryInfo> info : layerIds.entrySet()) {
313 if (!defaultLayerIds.containsKey(info.getKey())) {
314 remove(info.getValue());
315 drop.add(info.getKey());
316 Logging.info(tr("Drop old imagery ''{0}''", info.getValue().getName()));
317 }
318 }
319
320 if (!drop.isEmpty()) {
321 for (String id : drop) {
322 layerIds.remove(id);
323 }
324 save();
325 }
326 }
327
328 private static boolean isSimilar(ImageryInfo iiA, ImageryInfo iiB) {
329 if (iiA == null)
330 return false;
331 if (!iiA.getImageryType().equals(iiB.getImageryType()))
332 return false;
333 if (iiA.getId() != null && iiB.getId() != null) return iiA.getId().equals(iiB.getId());
334 return isSimilar(iiA.getUrl(), iiB.getUrl());
335 }
336
337 // some additional checks to respect extended URLs in preferences (legacy workaround)
338 private static boolean isSimilar(String a, String b) {
339 return Objects.equals(a, b) || (a != null && b != null && !a.isEmpty() && !b.isEmpty() && (a.contains(b) || b.contains(a)));
340 }
341
342 /**
343 * Add a new imagery entry.
344 * @param info imagery entry to add
345 */
346 public void add(ImageryInfo info) {
347 layers.add(info);
348 }
349
350 /**
351 * Remove an imagery entry.
352 * @param info imagery entry to remove
353 */
354 public void remove(ImageryInfo info) {
355 layers.remove(info);
356 }
357
358 /**
359 * Save the list of imagery entries to preferences.
360 */
361 public void save() {
362 List<ImageryPreferenceEntry> entries = new ArrayList<>();
363 for (ImageryInfo info : layers) {
364 entries.add(new ImageryPreferenceEntry(info));
365 }
366 StructUtils.putListOfStructs(Config.getPref(), "imagery.entries", entries, ImageryPreferenceEntry.class);
367 }
368
369 /**
370 * List of usable layers
371 * @return unmodifiable list containing usable layers
372 */
373 public List<ImageryInfo> getLayers() {
374 return Collections.unmodifiableList(layers);
375 }
376
377 /**
378 * List of available default layers
379 * @return unmodifiable list containing available default layers
380 */
381 public List<ImageryInfo> getDefaultLayers() {
382 return Collections.unmodifiableList(defaultLayers);
383 }
384
385 /**
386 * List of all available default layers (including mirrors)
387 * @return unmodifiable list containing available default layers
388 * @since 11570
389 */
390 public List<ImageryInfo> getAllDefaultLayers() {
391 return Collections.unmodifiableList(allDefaultLayers);
392 }
393
394 public static void addLayer(ImageryInfo info) {
395 instance.add(info);
396 instance.save();
397 }
398
399 public static void addLayers(Collection<ImageryInfo> infos) {
400 for (ImageryInfo i : infos) {
401 instance.add(i);
402 }
403 instance.save();
404 Collections.sort(instance.layers);
405 }
406
407 /**
408 * Get unique id for ImageryInfo.
409 *
410 * This takes care, that no id is used twice (due to a user error)
411 * @param info the ImageryInfo to look up
412 * @return null, if there is no id or the id is used twice,
413 * the corresponding id otherwise
414 */
415 public String getUniqueId(ImageryInfo info) {
416 if (info.getId() != null && layerIds.get(info.getId()) == info) {
417 return info.getId();
418 }
419 return null;
420 }
421}
Note: See TracBrowser for help on using the repository browser.