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

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

see #8902 - string.equals("") => string.isEmpty() (patch by shinigami)

  • Property svn:eol-style set to native
File size: 5.5 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.xml.sax.SAXException;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
15import org.openstreetmap.josm.io.MirroredInputStream;
16import org.openstreetmap.josm.io.imagery.ImageryReader;
17import org.openstreetmap.josm.tools.Utils;
18
19/**
20 * Manages the list of imagery entries that are shown in the imagery menu.
21 */
22public 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 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.size() > 0
124 ? defaultsSave : defaults);
125 }
126
127 // some additional checks to respect extended URLs in preferences (legacy workaround)
128 private boolean isSimilar(String a, String b) {
129 return Utils.equal(a, b) || (a != null && b != null && !a.isEmpty() && !b.isEmpty() && (a.contains(b) || b.contains(a)));
130 }
131
132 public void add(ImageryInfo info) {
133 layers.add(info);
134 }
135
136 public void remove(ImageryInfo info) {
137 layers.remove(info);
138 }
139
140 public void save() {
141 List<ImageryPreferenceEntry> entries = new ArrayList<ImageryPreferenceEntry>();
142 for (ImageryInfo info : layers) {
143 entries.add(new ImageryPreferenceEntry(info));
144 }
145 Main.pref.putListOfStructs("imagery.entries", entries, ImageryPreferenceEntry.class);
146 }
147
148 public List<ImageryInfo> getLayers() {
149 return Collections.unmodifiableList(layers);
150 }
151
152 public List<ImageryInfo> getDefaultLayers() {
153 return Collections.unmodifiableList(defaultLayers);
154 }
155
156 public static void addLayer(ImageryInfo info) {
157 instance.add(info);
158 instance.save();
159 }
160
161 public static void addLayers(Collection<ImageryInfo> infos) {
162 for (ImageryInfo i : infos) {
163 instance.add(i);
164 }
165 instance.save();
166 Collections.sort(instance.layers);
167 }
168}
Note: See TracBrowser for help on using the repository browser.