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

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

see #8465 - replace Utils.equal by Objects.equals, new in Java 7

  • Property svn:eol-style set to native
File size: 5.3 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;
10import java.util.Objects;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
14import org.openstreetmap.josm.io.MirroredInputStream;
15import org.openstreetmap.josm.io.imagery.ImageryReader;
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 try {
77 ImageryReader reader = new ImageryReader(source);
78 Collection<ImageryInfo> result = reader.parse();
79 defaultLayers.addAll(result);
80 } catch (IOException ex) {
81 Main.error(ex, false);
82 continue;
83 } catch (SAXException ex) {
84 Main.error(ex);
85 continue;
86 }
87 }
88 while (defaultLayers.remove(null));
89
90 Collection<String> defaults = Main.pref.getCollection("imagery.layers.default");
91 List<String> defaultsSave = new ArrayList<>();
92 for (ImageryInfo def : defaultLayers) {
93 if (def.isDefaultEntry()) {
94 defaultsSave.add(def.getUrl());
95
96 boolean isKnownDefault = false;
97 for (String url : defaults) {
98 if (isSimilar(url, def.getUrl())) {
99 isKnownDefault = true;
100 break;
101 }
102 }
103 boolean isInUserList = false;
104 if (!isKnownDefault) {
105 for (ImageryInfo i : layers) {
106 if (isSimilar(def.getUrl(), i.getUrl())) {
107 isInUserList = true;
108 break;
109 }
110 }
111 }
112 if (!isKnownDefault && !isInUserList) {
113 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
122 // some additional checks to respect extended URLs in preferences (legacy workaround)
123 private boolean isSimilar(String a, String b) {
124 return Objects.equals(a, b) || (a != null && b != null && !a.isEmpty() && !b.isEmpty() && (a.contains(b) || b.contains(a)));
125 }
126
127 public void add(ImageryInfo info) {
128 layers.add(info);
129 }
130
131 public void remove(ImageryInfo info) {
132 layers.remove(info);
133 }
134
135 public void save() {
136 List<ImageryPreferenceEntry> entries = new ArrayList<>();
137 for (ImageryInfo info : layers) {
138 entries.add(new ImageryPreferenceEntry(info));
139 }
140 Main.pref.putListOfStructs("imagery.entries", entries, ImageryPreferenceEntry.class);
141 }
142
143 public List<ImageryInfo> getLayers() {
144 return Collections.unmodifiableList(layers);
145 }
146
147 public List<ImageryInfo> getDefaultLayers() {
148 return Collections.unmodifiableList(defaultLayers);
149 }
150
151 public static void addLayer(ImageryInfo info) {
152 instance.add(info);
153 instance.save();
154 }
155
156 public static void addLayers(Collection<ImageryInfo> infos) {
157 for (ImageryInfo i : infos) {
158 instance.add(i);
159 }
160 instance.save();
161 Collections.sort(instance.layers);
162 }
163}
Note: See TracBrowser for help on using the repository browser.