1 package tim.prune.gui.map;
3 import java.util.ArrayList;
5 import tim.prune.config.Config;
8 * Class to hold a library for all the map sources
9 * and provide access to each one
11 public abstract class MapSourceLibrary
13 /** list of map sources */
14 private static ArrayList<MapSource> _sourceList = null;
15 /** Number of fixed sources */
16 private static int _numFixedSources = 0;
18 // Static block to initialise source list
21 _sourceList = new ArrayList<MapSource>();
23 _numFixedSources = _sourceList.size();
27 /** Private constructor to block instantiation */
28 private MapSourceLibrary() {}
31 /** @return number of fixed sources which shouldn't be deleted */
32 public static int getNumFixedSources() {
33 return _numFixedSources;
37 * Initialise source list by adding bare minimum
39 private static void addFixedSources()
41 _sourceList.add(new OsmMapSource("Mapnik", "https://[abc].tile.openstreetmap.org/"));
42 _sourceList.add(new OsmMapSource("Cycling Trails", "https://[abc].tile.openstreetmap.org/", "png",
43 "https://tile.waymarkedtrails.org/cycling/", "png", 18));
44 _sourceList.add(new OsmMapSource("Reitkarte", "http://topo[234].wanderreitkarte.de/topo/"));
45 _sourceList.add(new MffMapSource("Mapsforfree", "http://maps-for-free.com/layer/relief/", "jpg",
46 "http://maps-for-free.com/layer/water/", "gif", 11));
47 _sourceList.add(new OsmMapSource("Hikebikemap", "http://[abc].tiles.wmflabs.org/hikebike/",
48 "http://[abc].tiles.wmflabs.org/hillshading/", 18));
49 _sourceList.add(new OsmMapSource("OpenSeaMap", "http://tile.openstreetmap.org/",
50 "http://tiles.openseamap.org/seamark/", 18));
54 * Add custom sources from Config to the library
56 private static void addConfigSources()
58 String configString = Config.getConfigString(Config.KEY_MAPSOURCE_LIST);
59 if (configString != null && configString.length() > 10)
61 // Loop over sources in string, separated by vertical bars
62 int splitPos = configString.indexOf('|');
65 String sourceString = configString.substring(0, splitPos);
66 MapSource source = OsmMapSource.fromConfig(sourceString);
68 _sourceList.add(source);
70 configString = configString.substring(splitPos+1);
71 splitPos = configString.indexOf('|');
77 * @return current number of sources
79 public static int getNumSources() {
80 return _sourceList.size();
84 * Add the given MapSource to the list (at the end)
85 * @param inSource MapSource object
87 public static void addSource(MapSource inSource) {
88 // Check whether source is already there? Check whether valid?
89 _sourceList.add(inSource);
93 * Edit the given MapSource object by replacing with a new one
94 * @param inOriginal existing MapSource object
95 * @param inNewSource new MapSource object
97 public static void editSource(MapSource inOriginal, MapSource inNewSource)
99 // Check whether original source is still there
100 int origPos = _sourceList.indexOf(inOriginal);
102 addSource(inNewSource);
105 _sourceList.set(origPos, inNewSource);
110 * @param inIndex source index number
111 * @return corresponding map source object
113 public static MapSource getSource(int inIndex)
115 // Check whether within range
116 if (inIndex < 0 || inIndex >= _sourceList.size()) {return null;}
117 return _sourceList.get(inIndex);
121 * Delete the specified source
122 * @param inIndex index of source to delete
124 public static void deleteSource(int inIndex)
126 if (inIndex >= _numFixedSources) {
127 _sourceList.remove(inIndex);
132 * Check whether the given name already exists in the library (case-insensitive)
133 * @param inName name to check
134 * @return true if already exists, false otherwise
136 public static boolean hasSourceName(String inName)
138 if (inName == null) {return false;}
139 String checkName = inName.toLowerCase().trim();
140 for (int i=0; i<getNumSources(); i++)
142 String name = getSource(i).getName().toLowerCase();
143 if (name.equals(checkName)) {return true;}
149 * @return String containing all custom-added sources as a |-separated list
151 public static String getConfigString()
153 StringBuilder builder = new StringBuilder();
154 for (int i=getNumFixedSources(); i<getNumSources(); i++) {
155 builder.append(getSource(i).getConfigString()).append('|');
157 return builder.toString();