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://tile.openstreetmap.org/"));
42 _sourceList.add(new OsmMapSource("Osma", "http://tah.openstreetmap.org/Tiles/tile/"));
43 _sourceList.add(new OsmMapSource("Cyclemap", "http://andy.sandbox.cloudmade.com/tiles/cycle/"));
44 _sourceList.add(new OsmMapSource("Reitkarte", "http://wanderreitkarte.de/hills/",
45 "http://topo2.wanderreitkarte.de/topo/", 18));
46 _sourceList.add(new MffMapSource("Mapsforfree", "http://maps-for-free.com/layer/relief/", ".jpg",
47 "http://maps-for-free.com/layer/water/", ".gif", 11));
48 _sourceList.add(new CloudmadeMapSource("Pale Dawn", "998", 18));
52 * Add custom sources from Config to the library
54 private static void addConfigSources()
56 String configString = Config.getConfigString(Config.KEY_MAPSOURCE_LIST);
57 if (configString != null && configString.length() > 10)
59 // Loop over sources in string, separated by vertical bars
60 int splitPos = configString.indexOf('|');
63 String sourceString = configString.substring(0, splitPos);
64 MapSource source = OsmMapSource.fromConfig(sourceString);
65 if (source == null) {source = CloudmadeMapSource.fromConfig(sourceString);}
67 _sourceList.add(source);
69 configString = configString.substring(splitPos+1);
70 splitPos = configString.indexOf('|');
76 * @return current number of sources
78 public static int getNumSources() {
79 return _sourceList.size();
83 * Add the given MapSource to the list (at the end)
84 * @param inSource MapSource object
86 public static void addSource(MapSource inSource) {
87 // Check whether source is already there? Check whether valid?
88 _sourceList.add(inSource);
92 * @param inIndex source index number
93 * @return corresponding map source object
95 public static MapSource getSource(int inIndex)
97 // Check whether within range
98 if (inIndex < 0 || inIndex >= _sourceList.size()) {return null;}
99 return _sourceList.get(inIndex);
103 * Delete the specified source
104 * @param inIndex index of source to delete
106 public static void deleteSource(int inIndex)
108 if (inIndex >= _numFixedSources) {
109 _sourceList.remove(inIndex);
114 * Check whether the given name already exists in the library (case-insensitive)
115 * @param inName name to check
116 * @return true if already exists, false otherwise
118 public static boolean hasSourceName(String inName)
120 if (inName == null) {return false;}
121 String checkName = inName.toLowerCase().trim();
122 for (int i=0; i<getNumSources(); i++)
124 String name = getSource(i).getName().toLowerCase();
125 if (name.equals(checkName)) {return true;}
131 * @return String containing all custom-added sources as a |-separated list
133 public static String getConfigString()
135 StringBuilder builder = new StringBuilder();
136 for (int i=getNumFixedSources(); i<getNumSources(); i++) {
137 builder.append(getSource(i).getConfigString()).append('|');
139 return builder.toString();