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

Last change on this file since 7005 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import java.io.IOException;
5import java.util.ArrayList;
6import java.util.Arrays;
7import java.util.Collection;
8import java.util.Collections;
9import java.util.List;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
13import org.openstreetmap.josm.io.MirroredInputStream;
14import org.openstreetmap.josm.io.imagery.ImageryReader;
15import org.openstreetmap.josm.tools.Utils;
16import org.xml.sax.SAXException;
17
18/**
19 * Manages the list of imagery entries that are shown in the imagery menu.
20 */
21public class ImageryLayerInfo {
22
23 public static final ImageryLayerInfo instance = new ImageryLayerInfo();
24 List<ImageryInfo> layers = new ArrayList<>();
25 static List<ImageryInfo> defaultLayers = new ArrayList<>();
26
27 private static final String[] DEFAULT_LAYER_SITES = {
28 Main.getJOSMWebsite()+"/maps"
29 };
30
31 private ImageryLayerInfo() {
32 }
33
34 public ImageryLayerInfo(ImageryLayerInfo info) {
35 layers.addAll(info.layers);
36 }
37
38 public void clear() {
39 layers.clear();
40 }
41
42 public void load() {
43 boolean addedDefault = !layers.isEmpty();
44 List<ImageryPreferenceEntry> entries = Main.pref.getListOfStructs("imagery.entries", null, ImageryPreferenceEntry.class);
45 if (entries != null) {
46 for (ImageryPreferenceEntry prefEntry : entries) {
47 try {
48 ImageryInfo i = new ImageryInfo(prefEntry);
49 add(i);
50 } catch (IllegalArgumentException e) {
51 Main.warn("Unable to load imagery preference entry:"+e);
52 }
53 }
54 Collections.sort(layers);
55 }
56 if (addedDefault) {
57 save();
58 }
59 }
60
61 /**
62 * Loads the available imagery entries.
63 *
64 * The data is downloaded from the JOSM website (or loaded from cache).
65 * Entries marked as "default" are added to the user selection, if not
66 * already present.
67 *
68 * @param clearCache if true, clear the cache and start a fresh download.
69 */
70 public void loadDefaults(boolean clearCache) {
71 defaultLayers.clear();
72 for (String source : Main.pref.getCollection("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES))) {
73 if (clearCache) {
74 MirroredInputStream.cleanup(source);
75 }
76 MirroredInputStream stream = null;
77 try {
78 ImageryReader reader = new ImageryReader(source);
79 Collection<ImageryInfo> result = reader.parse();
80 defaultLayers.addAll(result);
81 } catch (IOException ex) {
82 Utils.close(stream);
83 Main.error(ex, false);
84 continue;
85 } catch (SAXException ex) {
86 Utils.close(stream);
87 Main.error(ex);
88 continue;
89 }
90 }
91 while (defaultLayers.remove(null));
92
93 Collection<String> defaults = Main.pref.getCollection("imagery.layers.default");
94 List<String> defaultsSave = new ArrayList<>();
95 for (ImageryInfo def : defaultLayers) {
96 if (def.isDefaultEntry()) {
97 defaultsSave.add(def.getUrl());
98
99 boolean isKnownDefault = false;
100 for (String url : defaults) {
101 if (isSimilar(url, def.getUrl())) {
102 isKnownDefault = true;
103 break;
104 }
105 }
106 boolean isInUserList = false;
107 if (!isKnownDefault) {
108 for (ImageryInfo i : layers) {
109 if (isSimilar(def.getUrl(), i.getUrl())) {
110 isInUserList = true;
111 break;
112 }
113 }
114 }
115 if (!isKnownDefault && !isInUserList) {
116 add(new ImageryInfo(def));
117 }
118 }
119 }
120
121 Collections.sort(defaultLayers);
122 Main.pref.putCollection("imagery.layers.default", defaultsSave.isEmpty() ? defaults : defaultsSave);
123 }
124
125 // some additional checks to respect extended URLs in preferences (legacy workaround)
126 private boolean isSimilar(String a, String b) {
127 return Utils.equal(a, b) || (a != null && b != null && !a.isEmpty() && !b.isEmpty() && (a.contains(b) || b.contains(a)));
128 }
129
130 public void add(ImageryInfo info) {
131 layers.add(info);
132 }
133
134 public void remove(ImageryInfo info) {
135 layers.remove(info);
136 }
137
138 public void save() {
139 List<ImageryPreferenceEntry> entries = new ArrayList<>();
140 for (ImageryInfo info : layers) {
141 entries.add(new ImageryPreferenceEntry(info));
142 }
143 Main.pref.putListOfStructs("imagery.entries", entries, ImageryPreferenceEntry.class);
144 }
145
146 public List<ImageryInfo> getLayers() {
147 return Collections.unmodifiableList(layers);
148 }
149
150 public List<ImageryInfo> getDefaultLayers() {
151 return Collections.unmodifiableList(defaultLayers);
152 }
153
154 public static void addLayer(ImageryInfo info) {
155 instance.add(info);
156 instance.save();
157 }
158
159 public static void addLayers(Collection<ImageryInfo> infos) {
160 for (ImageryInfo i : infos) {
161 instance.add(i);
162 }
163 instance.save();
164 Collections.sort(instance.layers);
165 }
166}
Note: See TracBrowser for help on using the repository browser.