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