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", "http://[abc].tile.openstreetmap.org/"));
42 _sourceList.add(new OsmMapSource("Cyclemap", "http://[abc].tile.opencyclemap.org/cycle/"));
43 _sourceList.add(new OsmMapSource("Reitkarte", "http://topo[234].wanderreitkarte.de/topo/"));
44 _sourceList.add(new MffMapSource("Mapsforfree", "http://maps-for-free.com/layer/relief/", "jpg",
45 "http://maps-for-free.com/layer/water/", "gif", 11));
46 _sourceList.add(new OsmMapSource("Hikebikemap", "http://toolserver.org/tiles/hikebike/",
47 "http://toolserver.org/~cmarqu/hill/", 18));
48 _sourceList.add(new OsmMapSource("Openseamap", "http://tile.openstreetmap.org/",
49 "http://tiles.openseamap.org/seamark/", 18));
50 _sourceList.add(new CloudmadeMapSource("Pale Dawn", "998", 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);
67 if (source == null) {source = CloudmadeMapSource.fromConfig(sourceString);}
69 _sourceList.add(source);
71 configString = configString.substring(splitPos+1);
72 splitPos = configString.indexOf('|');
78 * @return current number of sources
80 public static int getNumSources() {
81 return _sourceList.size();
85 * Add the given MapSource to the list (at the end)
86 * @param inSource MapSource object
88 public static void addSource(MapSource inSource) {
89 // Check whether source is already there? Check whether valid?
90 _sourceList.add(inSource);
94 * Edit the given MapSource object by replacing with a new one
95 * @param inOriginal existing MapSource object
96 * @param inNewSource new MapSource object
98 public static void editSource(MapSource inOriginal, MapSource inNewSource)
100 // Check whether original source is still there
101 int origPos = _sourceList.indexOf(inOriginal);
103 addSource(inNewSource);
106 _sourceList.set(origPos, inNewSource);
111 * @param inIndex source index number
112 * @return corresponding map source object
114 public static MapSource getSource(int inIndex)
116 // Check whether within range
117 if (inIndex < 0 || inIndex >= _sourceList.size()) {return null;}
118 return _sourceList.get(inIndex);
122 * Delete the specified source
123 * @param inIndex index of source to delete
125 public static void deleteSource(int inIndex)
127 if (inIndex >= _numFixedSources) {
128 _sourceList.remove(inIndex);
133 * Check whether the given name already exists in the library (case-insensitive)
134 * @param inName name to check
135 * @return true if already exists, false otherwise
137 public static boolean hasSourceName(String inName)
139 if (inName == null) {return false;}
140 String checkName = inName.toLowerCase().trim();
141 for (int i=0; i<getNumSources(); i++)
143 String name = getSource(i).getName().toLowerCase();
144 if (name.equals(checkName)) {return true;}
150 * @return String containing all custom-added sources as a |-separated list
152 public static String getConfigString()
154 StringBuilder builder = new StringBuilder();
155 for (int i=getNumFixedSources(); i<getNumSources(); i++) {
156 builder.append(getSource(i).getConfigString()).append('|');
158 return builder.toString();