]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/map/MapSourceLibrary.java
b82fd4e7a23e197a80120eb6d2b2e0c7a854f409
[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                 _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         }
51
52         /**
53          * Add custom sources from Config to the library
54          */
55         private static void addConfigSources()
56         {
57                 String configString = Config.getConfigString(Config.KEY_MAPSOURCE_LIST);
58                 if (configString != null && configString.length() > 10)
59                 {
60                         // Loop over sources in string, separated by vertical bars
61                         int splitPos = configString.indexOf('|');
62                         while (splitPos > 0)
63                         {
64                                 String sourceString = configString.substring(0, splitPos);
65                                 MapSource source = OsmMapSource.fromConfig(sourceString);
66                                 if (source != null) {
67                                         _sourceList.add(source);
68                                 }
69                                 configString = configString.substring(splitPos+1);
70                                 splitPos = configString.indexOf('|');
71                         }
72                 }
73         }
74
75         /**
76          * @return current number of sources
77          */
78         public static int getNumSources() {
79                 return _sourceList.size();
80         }
81
82         /**
83          * Add the given MapSource to the list (at the end)
84          * @param inSource MapSource object
85          */
86         public static void addSource(MapSource inSource) {
87                 // Check whether source is already there?  Check whether valid?
88                 _sourceList.add(inSource);
89         }
90
91         /**
92          * Edit the given MapSource object by replacing with a new one
93          * @param inOriginal existing MapSource object
94          * @param inNewSource new MapSource object
95          */
96         public static void editSource(MapSource inOriginal, MapSource inNewSource)
97         {
98                 // Check whether original source is still there
99                 int origPos = _sourceList.indexOf(inOriginal);
100                 if (origPos < 0) {
101                         addSource(inNewSource);
102                 }
103                 else {
104                         _sourceList.set(origPos, inNewSource);
105                 }
106         }
107
108         /**
109          * @param inIndex source index number
110          * @return corresponding map source object
111          */
112         public static MapSource getSource(int inIndex)
113         {
114                 // Check whether within range
115                 if (inIndex < 0 || inIndex >= _sourceList.size()) {return null;}
116                 return _sourceList.get(inIndex);
117         }
118
119         /**
120          * Delete the specified source
121          * @param inIndex index of source to delete
122          */
123         public static void deleteSource(int inIndex)
124         {
125                 if (inIndex >= _numFixedSources) {
126                         _sourceList.remove(inIndex);
127                 }
128         }
129
130         /**
131          * Check whether the given name already exists in the library (case-insensitive)
132          * @param inName name to check
133          * @return true if already exists, false otherwise
134          */
135         public static boolean hasSourceName(String inName)
136         {
137                 if (inName == null) {return false;}
138                 String checkName = inName.toLowerCase().trim();
139                 for (int i=0; i<getNumSources(); i++)
140                 {
141                         String name = getSource(i).getName().toLowerCase();
142                         if (name.equals(checkName)) {return true;}
143                 }
144                 return false;
145         }
146
147         /**
148          * @return String containing all custom-added sources as a |-separated list
149          */
150         public static String getConfigString()
151         {
152                 StringBuilder builder = new StringBuilder();
153                 for (int i=getNumFixedSources(); i<getNumSources(); i++) {
154                         builder.append(getSource(i).getConfigString()).append('|');
155                 }
156                 return builder.toString();
157         }
158 }