]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/map/MapSourceLibrary.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / gui / map / MapSourceLibrary.java
1 package tim.prune.gui.map;
2
3 import java.util.ArrayList;
4
5 import tim.prune.config.Config;
6
7 /**
8  * Class to hold a library for all the map sources
9  * and provide access to each one
10  */
11 public abstract class MapSourceLibrary
12 {
13         /** list of map sources */
14         private static ArrayList<MapSource> _sourceList = null;
15         /** Number of fixed sources */
16         private static int _numFixedSources = 0;
17
18         // Static block to initialise source list
19         static
20         {
21                 _sourceList = new ArrayList<MapSource>();
22                 addFixedSources();
23                 _numFixedSources = _sourceList.size();
24                 addConfigSources();
25         }
26
27         /** Private constructor to block instantiation */
28         private MapSourceLibrary() {}
29
30
31         /** @return number of fixed sources which shouldn't be deleted */
32         public static int getNumFixedSources() {
33                 return _numFixedSources;
34         }
35
36         /**
37          * Initialise source list by adding bare minimum
38          */
39         private static void addFixedSources()
40         {
41                 final String THUNDERFOREST_APIKEY = "c32212f10b13496786b98dc6c42e5c3c";
42                 _sourceList.add(new OsmMapSource("Mapnik", "http://[abc].tile.openstreetmap.org/"));
43                 OsmMapSource cycleSource = new OsmMapSource("OpenCycleMap", "http://[abc].tile.thunderforest.com/cycle/");
44                 cycleSource.setApiKey(THUNDERFOREST_APIKEY);
45                 _sourceList.add(cycleSource);
46                 OsmMapSource outdoorsSource = new OsmMapSource("Outdoors", "http://[abc].tile.thunderforest.com/outdoors/");
47                 outdoorsSource.setApiKey(THUNDERFOREST_APIKEY);
48                 _sourceList.add(outdoorsSource);
49                 _sourceList.add(new OsmMapSource("Reitkarte", "http://topo[234].wanderreitkarte.de/topo/"));
50                 _sourceList.add(new MffMapSource("Mapsforfree", "http://maps-for-free.com/layer/relief/", "jpg",
51                         "http://maps-for-free.com/layer/water/", "gif", 11));
52                 _sourceList.add(new OsmMapSource("Hikebikemap", "http://[abc].tiles.wmflabs.org/hikebike/",
53                         "http://[abc].tiles.wmflabs.org/hillshading/", 18));
54                 _sourceList.add(new OsmMapSource("OpenSeaMap", "http://tile.openstreetmap.org/",
55                         "http://tiles.openseamap.org/seamark/", 18));
56         }
57
58         /**
59          * Add custom sources from Config to the library
60          */
61         private static void addConfigSources()
62         {
63                 String configString = Config.getConfigString(Config.KEY_MAPSOURCE_LIST);
64                 if (configString != null && configString.length() > 10)
65                 {
66                         // Loop over sources in string, separated by vertical bars
67                         int splitPos = configString.indexOf('|');
68                         while (splitPos > 0)
69                         {
70                                 String sourceString = configString.substring(0, splitPos);
71                                 MapSource source = OsmMapSource.fromConfig(sourceString);
72                                 if (source != null) {
73                                         _sourceList.add(source);
74                                 }
75                                 configString = configString.substring(splitPos+1);
76                                 splitPos = configString.indexOf('|');
77                         }
78                 }
79         }
80
81         /**
82          * @return current number of sources
83          */
84         public static int getNumSources() {
85                 return _sourceList.size();
86         }
87
88         /**
89          * Add the given MapSource to the list (at the end)
90          * @param inSource MapSource object
91          */
92         public static void addSource(MapSource inSource) {
93                 // Check whether source is already there?  Check whether valid?
94                 _sourceList.add(inSource);
95         }
96
97         /**
98          * Edit the given MapSource object by replacing with a new one
99          * @param inOriginal existing MapSource object
100          * @param inNewSource new MapSource object
101          */
102         public static void editSource(MapSource inOriginal, MapSource inNewSource)
103         {
104                 // Check whether original source is still there
105                 int origPos = _sourceList.indexOf(inOriginal);
106                 if (origPos < 0) {
107                         addSource(inNewSource);
108                 }
109                 else {
110                         _sourceList.set(origPos, inNewSource);
111                 }
112         }
113
114         /**
115          * @param inIndex source index number
116          * @return corresponding map source object
117          */
118         public static MapSource getSource(int inIndex)
119         {
120                 // Check whether within range
121                 if (inIndex < 0 || inIndex >= _sourceList.size()) {return null;}
122                 return _sourceList.get(inIndex);
123         }
124
125         /**
126          * Delete the specified source
127          * @param inIndex index of source to delete
128          */
129         public static void deleteSource(int inIndex)
130         {
131                 if (inIndex >= _numFixedSources) {
132                         _sourceList.remove(inIndex);
133                 }
134         }
135
136         /**
137          * Check whether the given name already exists in the library (case-insensitive)
138          * @param inName name to check
139          * @return true if already exists, false otherwise
140          */
141         public static boolean hasSourceName(String inName)
142         {
143                 if (inName == null) {return false;}
144                 String checkName = inName.toLowerCase().trim();
145                 for (int i=0; i<getNumSources(); i++)
146                 {
147                         String name = getSource(i).getName().toLowerCase();
148                         if (name.equals(checkName)) {return true;}
149                 }
150                 return false;
151         }
152
153         /**
154          * @return String containing all custom-added sources as a |-separated list
155          */
156         public static String getConfigString()
157         {
158                 StringBuilder builder = new StringBuilder();
159                 for (int i=getNumFixedSources(); i<getNumSources(); i++) {
160                         builder.append(getSource(i).getConfigString()).append('|');
161                 }
162                 return builder.toString();
163         }
164 }