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

Last change on this file since 3872 was 3826, checked in by framm, 13 years ago

Add a "blacklisted" property to imagery layers; set this property from a
compiled-in list of regular expressions; disable blacklisted entries in
the Imagery menu.

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedReader;
7import java.io.IOException;
8import java.io.InputStreamReader;
9import java.io.UnsupportedEncodingException;
10import java.util.ArrayList;
11import java.util.Arrays;
12import java.util.Collection;
13import java.util.Collections;
14import java.util.LinkedList;
15import java.util.List;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
19import org.openstreetmap.josm.io.MirroredInputStream;
20
21public class ImageryLayerInfo {
22
23 public static final ImageryLayerInfo instance = new ImageryLayerInfo();
24 ArrayList<ImageryInfo> layers = new ArrayList<ImageryInfo>();
25 ArrayList<ImageryInfo> defaultLayers = new ArrayList<ImageryInfo>();
26
27 private final static String[] DEFAULT_LAYER_SITES = {
28 "http://josm.openstreetmap.de/maps"
29 };
30
31 public void load() {
32 layers.clear();
33 Collection<String> defaults = Main.pref.getCollection(
34 "imagery.layers.default", Collections.<String>emptySet());
35 for(Collection<String> c : Main.pref.getArray("imagery.layers",
36 Collections.<Collection<String>>emptySet())) {
37 add(new ImageryInfo(c));
38 }
39
40 ArrayList<String> defaultsSave = new ArrayList<String>();
41 for(String source : Main.pref.getCollection("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES)))
42 {
43 try
44 {
45 MirroredInputStream s = new MirroredInputStream(source, -1);
46 InputStreamReader r;
47 try
48 {
49 r = new InputStreamReader(s, "UTF-8");
50 }
51 catch (UnsupportedEncodingException e)
52 {
53 r = new InputStreamReader(s);
54 }
55 BufferedReader reader = new BufferedReader(r);
56 String line;
57 while((line = reader.readLine()) != null)
58 {
59 String val[] = line.split(";");
60 if(!line.startsWith("#") && (val.length == 3 || val.length == 4)) {
61 boolean force = "true".equals(val[0]);
62 String name = tr(val[1]);
63 String url = val[2];
64 String eulaAcceptanceRequired = null;
65
66 if (val.length == 4) {
67 // 4th parameter optional for license agreement (EULA)
68 eulaAcceptanceRequired = val[3];
69 }
70
71 defaultLayers.add(new ImageryInfo(name, url, eulaAcceptanceRequired));
72
73 if (force) {
74 defaultsSave.add(url);
75 if (!defaults.contains(url)) {
76 for (ImageryInfo i : layers) {
77 if ((i.getImageryType() == ImageryType.WMS && url.equals(i.getUrl()))
78 || url.equals(i.getFullUrl())) {
79 force = false;
80 }
81 }
82 if (force) {
83 add(new ImageryInfo(name, url));
84 }
85 }
86 }
87 }
88 }
89 }
90 catch (IOException e)
91 {
92 }
93 }
94
95 Main.pref.putCollection("imagery.layers.default", defaultsSave.size() > 0
96 ? defaultsSave : defaults);
97 Collections.sort(layers);
98 save();
99 }
100
101 public void add(ImageryInfo info) {
102 layers.add(info);
103 }
104
105 public void remove(ImageryInfo info) {
106 layers.remove(info);
107 }
108
109 public void save() {
110 LinkedList<Collection<String>> coll = new LinkedList<Collection<String>>();
111 for (ImageryInfo info : layers) {
112 coll.add(info.getInfoArray());
113 }
114 Main.pref.putArray("imagery.layers", coll);
115 }
116
117 public List<ImageryInfo> getLayers() {
118 return Collections.unmodifiableList(layers);
119 }
120
121 public List<ImageryInfo> getDefaultLayers() {
122 return Collections.unmodifiableList(defaultLayers);
123 }
124
125 public static void addLayer(ImageryInfo info) {
126 instance.add(info);
127 instance.save();
128 Main.main.menu.imageryMenu.refreshImageryMenu();
129 }
130}
Note: See TracBrowser for help on using the repository browser.