]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/gpsies/GetGpsiesFunction.java
804c4ce12e8d897369c47037b5b85c0951fff186
[GpsPrune.git] / tim / prune / function / gpsies / GetGpsiesFunction.java
1 package tim.prune.function.gpsies;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.util.ArrayList;
7
8 import javax.xml.parsers.SAXParser;
9 import javax.xml.parsers.SAXParserFactory;
10
11 import tim.prune.App;
12 import tim.prune.I18nManager;
13 import tim.prune.load.xml.XmlFileLoader;
14 import tim.prune.load.xml.ZipFileLoader;
15
16 /**
17  * Function to load track information from Gpsies.com
18  * according to the currently viewed area
19  */
20 public class GetGpsiesFunction extends GenericDownloaderFunction
21 {
22         /** Number of results per page */
23         private static final int RESULTS_PER_PAGE = 20;
24         /** Maximum number of results to get */
25         private static final int MAX_RESULTS = 60;
26
27
28         /**
29          * Constructor
30          * @param inApp App object
31          */
32         public GetGpsiesFunction(App inApp) {
33                 super(inApp);
34         }
35
36         /**
37          * @return name key
38          */
39         public String getNameKey() {
40                 return "function.getgpsies";
41         }
42
43         /**
44          * @param inColNum index of column, 0 or 1
45          * @return key for this column
46          */
47         protected String getColumnKey(int inColNum)
48         {
49                 if (inColNum == 0) return "dialog.gpsies.column.name";
50                 return "dialog.gpsies.column.length";
51         }
52
53
54         /**
55          * Run method to call gpsies.com in separate thread
56          */
57         public void run()
58         {
59                 _statusLabel.setText(I18nManager.getText("confirm.running"));
60                 // Act on callback to update list and send another request if necessary
61                 double[] coords = _app.getViewport().getBounds();
62                 // Example http://www.gpsies.com/api.do?BBOX=10,51,12,53&limit=20&trackTypes=jogging&filetype=kml&device=Run.GPS
63                 int currPage = 1;
64
65                 ArrayList<GpsiesTrack> trackList = null;
66                 URL url = null;
67                 String descMessage = "";
68                 InputStream inStream = null;
69                 // Loop for each page of the results
70                 do
71                 {
72                         String urlString = "http://www.gpsies.com/api.do?BBOX=" +
73                                 coords[1] + "," + coords[0] + "," + coords[3] + "," + coords[2] +
74                                 "&limit=" + RESULTS_PER_PAGE + "&resultPage=" + currPage;
75                         // Parse the returned XML with a special handler
76                         GpsiesXmlHandler xmlHandler = new GpsiesXmlHandler();
77                         try
78                         {
79                                 url = new URL(urlString);
80                                 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
81                                 inStream = url.openStream();
82                                 saxParser.parse(inStream, xmlHandler);
83                         }
84                         catch (Exception e) {
85                                 descMessage = e.getClass().getName() + " - " + e.getMessage();
86                         }
87                         // Close stream and ignore errors
88                         try {
89                                 inStream.close();
90                         } catch (Exception e) {}
91                         // Add track list to model
92                         trackList = xmlHandler.getTrackList();
93                         _trackListModel.addTracks(trackList);
94
95                         // Compare number of results with results per page and call again if necessary
96                         currPage++;
97                 }
98                 while (trackList != null && trackList.size() == RESULTS_PER_PAGE
99                         && _trackListModel.getRowCount() < MAX_RESULTS && !_cancelled);
100                 // Set status label according to error or "none found", leave blank if ok
101                 if (descMessage.equals("") && (trackList == null || trackList.size() == 0)) {
102                         descMessage = I18nManager.getText("dialog.gpsies.nonefound");
103                 }
104                 _statusLabel.setText(descMessage);
105         }
106
107         /**
108          * Load the selected track or point
109          */
110         protected void loadSelected()
111         {
112                 // Find the row selected in the table and get the corresponding track
113                 int rowNum = _trackTable.getSelectedRow();
114                 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
115                 {
116                         String url = _trackListModel.getTrack(rowNum).getDownloadLink();
117                         XmlFileLoader xmlLoader = new XmlFileLoader(_app);
118                         ZipFileLoader loader = new ZipFileLoader(_app, xmlLoader);
119                         try
120                         {
121                                 loader.openStream(new URL(url).openStream());
122                         }
123                         catch (IOException ioe) {
124                                 System.err.println("IO Exception : " + ioe.getMessage());
125                         }
126                 }
127                 // Close the dialog
128                 _cancelled = true;
129                 _dialog.dispose();
130         }
131 }