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://wanderreitkarte.de/hills/",
44 "http://topo2.wanderreitkarte.de/topo/", 18));
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://toolserver.org/tiles/hikebike/",
48 "http://toolserver.org/~cmarqu/hill/", 18));
49 _sourceList.add(new OsmMapSource("Openseamap", "http://tile.openstreetmap.org/",
50 "http://tiles.openseamap.org/seamark/", 18));
51 _sourceList.add(new CloudmadeMapSource("Pale Dawn", "998", 18));
55 * Add custom sources from Config to the library
57 private static void addConfigSources()
59 String configString = Config.getConfigString(Config.KEY_MAPSOURCE_LIST);
60 if (configString != null && configString.length() > 10)
62 // Loop over sources in string, separated by vertical bars
63 int splitPos = configString.indexOf('|');
66 String sourceString = configString.substring(0, splitPos);
67 MapSource source = OsmMapSource.fromConfig(sourceString);
68 if (source == null) {source = CloudmadeMapSource.fromConfig(sourceString);}
70 _sourceList.add(source);
72 configString = configString.substring(splitPos+1);
73 splitPos = configString.indexOf('|');
79 * @return current number of sources
81 public static int getNumSources() {
82 return _sourceList.size();
86 * Add the given MapSource to the list (at the end)
87 * @param inSource MapSource object
89 public static void addSource(MapSource inSource) {
90 // Check whether source is already there? Check whether valid?
91 _sourceList.add(inSource);
95 * Edit the given MapSource object by replacing with a new one
96 * @param inOriginal existing MapSource object
97 * @param inNewSource new MapSource object
99 public static void editSource(MapSource inOriginal, MapSource inNewSource)
101 // Check whether original source is still there
102 int origPos = _sourceList.indexOf(inOriginal);
104 addSource(inNewSource);
107 _sourceList.set(origPos, inNewSource);
112 * @param inIndex source index number
113 * @return corresponding map source object
115 public static MapSource getSource(int inIndex)
117 // Check whether within range
118 if (inIndex < 0 || inIndex >= _sourceList.size()) {return null;}
119 return _sourceList.get(inIndex);
123 * Delete the specified source
124 * @param inIndex index of source to delete
126 public static void deleteSource(int inIndex)
128 if (inIndex >= _numFixedSources) {
129 _sourceList.remove(inIndex);
134 * Check whether the given name already exists in the library (case-insensitive)
135 * @param inName name to check
136 * @return true if already exists, false otherwise
138 public static boolean hasSourceName(String inName)
140 if (inName == null) {return false;}
141 String checkName = inName.toLowerCase().trim();
142 for (int i=0; i<getNumSources(); i++)
144 String name = getSource(i).getName().toLowerCase();
145 if (name.equals(checkName)) {return true;}
151 * @return String containing all custom-added sources as a |-separated list
153 public static String getConfigString()
155 StringBuilder builder = new StringBuilder();
156 for (int i=getNumFixedSources(); i<getNumSources(); i++) {
157 builder.append(getSource(i).getConfigString()).append('|');
159 return builder.toString();