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.LinkedList;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
|
---|
14 | import org.openstreetmap.josm.io.imagery.ImageryReader;
|
---|
15 | import org.openstreetmap.josm.io.MirroredInputStream;
|
---|
16 | import org.openstreetmap.josm.tools.Utils;
|
---|
17 | import org.xml.sax.SAXException;
|
---|
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 | "http://josm.openstreetmap.de/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.size() != 0;
|
---|
45 | List<ImageryPreferenceEntry> entries = Main.pref.getListOfStructs("imagery.entries", null, ImageryPreferenceEntry.class);
|
---|
46 | if (entries == null) {
|
---|
47 | /* FIXME: Remove old format ~ March 2012 */
|
---|
48 | boolean hasOld = loadOld();
|
---|
49 | if (hasOld) {
|
---|
50 | save();
|
---|
51 | }
|
---|
52 | } else {
|
---|
53 | for (ImageryPreferenceEntry prefEntry : entries) {
|
---|
54 | ImageryInfo i = new ImageryInfo(prefEntry);
|
---|
55 | add(i);
|
---|
56 | }
|
---|
57 | Collections.sort(layers);
|
---|
58 | }
|
---|
59 | if (addedDefault) {
|
---|
60 | save();
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public boolean loadOld() {
|
---|
65 | Collection<Collection<String>> entries = Main.pref.getArray("imagery.layers", null);
|
---|
66 | if (entries != null) {
|
---|
67 | for (Collection<String> c : Main.pref.getArray("imagery.layers",
|
---|
68 | Collections.<Collection<String>>emptySet())) {
|
---|
69 | ImageryInfo i = new ImageryInfo(c);
|
---|
70 | String url = i.getUrl();
|
---|
71 | if(url != null) {
|
---|
72 | /* FIXME: Remove the attribution copy stuff end of 2011 */
|
---|
73 | if(!i.hasAttribution()) {
|
---|
74 | for(ImageryInfo d : defaultLayers) {
|
---|
75 | if(url.equals(d.getUrl())) {
|
---|
76 | i.copyAttribution(d);
|
---|
77 | i.setBounds(d.getBounds());
|
---|
78 | break;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | add(i);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | Collections.sort(layers);
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void loadDefaults(boolean clearCache) {
|
---|
92 | defaultLayers.clear();
|
---|
93 | for (String source : Main.pref.getCollection("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES))) {
|
---|
94 | if (clearCache) {
|
---|
95 | MirroredInputStream.cleanup(source);
|
---|
96 | }
|
---|
97 | MirroredInputStream stream = null;
|
---|
98 | try {
|
---|
99 | ImageryReader reader = new ImageryReader(source);
|
---|
100 | Collection<ImageryInfo> result = reader.parse();
|
---|
101 | defaultLayers.addAll(result);
|
---|
102 | } catch (IOException ex) {
|
---|
103 | Utils.close(stream);
|
---|
104 | ex.printStackTrace();
|
---|
105 | continue;
|
---|
106 | } catch (SAXException sex) {
|
---|
107 | Utils.close(stream);
|
---|
108 | sex.printStackTrace();
|
---|
109 | continue;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | while (defaultLayers.remove(null)) {}
|
---|
113 |
|
---|
114 | Collection<String> defaults = Main.pref.getCollection("imagery.layers.default");
|
---|
115 | ArrayList<String> defaultsSave = new ArrayList<String>();
|
---|
116 | for (ImageryInfo def : defaultLayers) {
|
---|
117 | if (def.isDefaultEntry()) {
|
---|
118 | defaultsSave.add(def.getUrl());
|
---|
119 |
|
---|
120 | boolean isKnownDefault = false;
|
---|
121 | for (String url : defaults) {
|
---|
122 | if (isSimilar(url, def.getUrl())) {
|
---|
123 | isKnownDefault = true;
|
---|
124 | break;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | boolean isInUserList = false;
|
---|
128 | if (!isKnownDefault) {
|
---|
129 | for (ImageryInfo i : layers) {
|
---|
130 | if (isSimilar(def.getUrl(), i.getUrl())) {
|
---|
131 | isInUserList = true;
|
---|
132 | break;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | if (!isKnownDefault && !isInUserList) {
|
---|
137 | add(new ImageryInfo(def));
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | Collections.sort(defaultLayers);
|
---|
143 | Main.pref.putCollection("imagery.layers.default", defaultsSave.size() > 0
|
---|
144 | ? defaultsSave : defaults);
|
---|
145 | }
|
---|
146 |
|
---|
147 | // some additional checks to respect extended URLs in preferences (legacy workaround)
|
---|
148 | private boolean isSimilar(String a, String b) {
|
---|
149 | return Utils.equal(a, b) || (a != null && b != null && !"".equals(a) && !"".equals(b) && (a.contains(b) || b.contains(a)));
|
---|
150 | }
|
---|
151 |
|
---|
152 | public void add(ImageryInfo info) {
|
---|
153 | layers.add(info);
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void remove(ImageryInfo info) {
|
---|
157 | layers.remove(info);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public void save() {
|
---|
161 | List<ImageryPreferenceEntry> entries = new ArrayList<ImageryPreferenceEntry>();
|
---|
162 | for (ImageryInfo info : layers) {
|
---|
163 | entries.add(new ImageryPreferenceEntry(info));
|
---|
164 | }
|
---|
165 | Main.pref.putListOfStructs("imagery.entries", entries, ImageryPreferenceEntry.class);
|
---|
166 | }
|
---|
167 |
|
---|
168 | public List<ImageryInfo> getLayers() {
|
---|
169 | return Collections.unmodifiableList(layers);
|
---|
170 | }
|
---|
171 |
|
---|
172 | public List<ImageryInfo> getDefaultLayers() {
|
---|
173 | return Collections.unmodifiableList(defaultLayers);
|
---|
174 | }
|
---|
175 |
|
---|
176 | public static void addLayer(ImageryInfo info) {
|
---|
177 | instance.add(info);
|
---|
178 | instance.save();
|
---|
179 | Main.main.menu.imageryMenu.refreshImageryMenu();
|
---|
180 | }
|
---|
181 | }
|
---|