]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/map/MapTileConfig.java
Version 9, February 2010
[GpsPrune.git] / tim / prune / gui / map / MapTileConfig.java
1 package tim.prune.gui.map;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5
6 import tim.prune.config.Config;
7
8 /**
9  * Class to hold the config for the map tiles
10  * and retrieve the correct URL prefix
11  */
12 public class MapTileConfig
13 {
14         /** Index of map server */
15         private int _index = 0;
16         /** Url for other */
17         private String _url = null;
18
19         /** server urls for known maps */
20         private static final String[] SERVER_URLS = {
21                 "http://tile.openstreetmap.org/", // mapnik
22                 "http://tah.openstreetmap.org/Tiles/tile/",      // osma
23                 "http://andy.sandbox.cloudmade.com/tiles/cycle/" // cyclemap
24         };
25         /** Index of 'other' server with freeform url */
26         private static final int OTHER_SERVER_NUM = 3;
27
28
29         /**
30          * Default constructor using Config
31          */
32         public MapTileConfig()
33         {
34                 _index = Config.getConfigInt(Config.KEY_MAPSERVERINDEX);
35                 _url = fixUrl(Config.getConfigString(Config.KEY_MAPSERVERURL));
36                 // reset index wrong or if other url too short
37                 if (_index < 0 || _index > OTHER_SERVER_NUM ||
38                         (_index == OTHER_SERVER_NUM && (_url == null || _url.length() < 5)))
39                 {
40                         _index = 0;
41                 }
42         }
43
44         /**
45          * @return url
46          */
47         public String getUrl()
48         {
49                 if (_index == OTHER_SERVER_NUM) {return _url;}
50                 return SERVER_URLS[_index];
51         }
52
53         /**
54          * Checks the given url for having the right prefix and trailing slash
55          * @param inUrl url to check
56          * @return validated url with correct prefix and trailing slash, or null
57          */
58         private static String fixUrl(String inUrl)
59         {
60                 if (inUrl == null || inUrl.equals("")) {return null;}
61                 String url = inUrl;
62                 // check prefix
63                 try {
64                         new URL(url);
65                 }
66                 catch (MalformedURLException e) {
67                         // add the http protocol
68                         url = "http://" + url;
69                 }
70                 // check trailing /
71                 if (!url.endsWith("/")) {
72                         url = url + "/";
73                 }
74                 return url;
75         }
76
77         /**
78          * @param inOther other config object
79          * @return true if the objects are exactly the same
80          */
81         public boolean equals(MapTileConfig inOther)
82         {
83                 // Other object must be non-null and must have same index
84                 if (inOther == null || inOther._index != _index) {return false;}
85                 // Check url if other selected
86                 if (_index == OTHER_SERVER_NUM) {
87                         return inOther._url.equals(_url);
88                 }
89                 // Not other so must match
90                 return true;
91         }
92 }