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