]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/SearchOpenCachingDeFunction.java
20e2fc81d4de00698f7265c4651212bf71ff740d
[GpsPrune.git] / src / tim / prune / function / SearchOpenCachingDeFunction.java
1 package tim.prune.function;
2
3 import java.io.InputStream;
4 import java.net.URL;
5 import java.util.ArrayList;
6
7 import javax.xml.parsers.SAXParser;
8 import javax.xml.parsers.SAXParserFactory;
9
10 import tim.prune.App;
11 import tim.prune.I18nManager;
12 import tim.prune.data.DataPoint;
13 import tim.prune.data.Field;
14 import tim.prune.data.Latitude;
15 import tim.prune.data.Longitude;
16 import tim.prune.function.search.GenericDownloaderFunction;
17 import tim.prune.function.search.SearchResult;
18
19 /**
20  * Function to load information about geocaches nearby to the current point
21  * using the service at opencaching.de
22  */
23 public class SearchOpenCachingDeFunction extends GenericDownloaderFunction
24 {
25         /** Maximum distance from point in km */
26         private static final int MAX_DISTANCE = 50;
27
28
29         /**
30          * Constructor
31          * @param inApp App object
32          */
33         public SearchOpenCachingDeFunction(App inApp) {
34                 super(inApp);
35         }
36
37         /**
38          * @return name key
39          */
40         public String getNameKey() {
41                 return "function.searchopencachingde";
42         }
43
44         /**
45          * @param inColNum index of column, 0 or 1
46          * @return key for this column
47          */
48         protected String getColumnKey(int inColNum)
49         {
50                 if (inColNum == 0) return "dialog.wikipedia.column.name";
51                 return "dialog.wikipedia.column.distance";
52         }
53
54
55         /**
56          * Run method to call service in a separate thread
57          */
58         public void run()
59         {
60                 _statusLabel.setText(I18nManager.getText("confirm.running"));
61                 // Get coordinates from current point
62                 DataPoint point = _app.getTrackInfo().getCurrentPoint();
63                 if (point == null)
64                 {
65                         return;
66                 }
67
68                 final double lat = point.getLatitude().getDouble();
69                 final double lon = point.getLongitude().getDouble();
70                 submitSearch(lat, lon);
71
72                 // Set status label according to error or "none found", leave blank if ok
73                 if (_errorMessage == null && _trackListModel.isEmpty()) {
74                         _errorMessage = I18nManager.getText("dialog.geocaching.nonefound");
75                 }
76                 _statusLabel.setText(_errorMessage == null ? "" : _errorMessage);
77         }
78
79         /**
80          * Submit the search for the given parameters
81          * @param inLat latitude
82          * @param inLon longitude
83          */
84         private void submitSearch(double inLat, double inLon)
85         {
86                 // The only parameters are lat and long from the current point
87                 String urlString = "http://opencaching.de/search.php?searchto=searchbydistance&showresult=1"
88                         + "&output=XML&sort=bydistance&lat=" + inLat
89                         + "&lon=" + inLon + "&distance=" + MAX_DISTANCE + "&unit=km";
90                 // Parse the returned XML with a special handler
91                 OpenCachingDeXmlHandler xmlHandler = new OpenCachingDeXmlHandler();
92                 InputStream inStream = null;
93
94                 try
95                 {
96                         URL url = new URL(urlString);
97                         SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
98                         inStream = url.openStream();
99                         saxParser.parse(inStream, xmlHandler);
100                 }
101                 catch (Exception e) {
102                         _errorMessage = e.getClass().getName() + " - " + e.getMessage();
103                 }
104                 // Close stream and ignore errors
105                 try {
106                         inStream.close();
107                 } catch (Exception e) {}
108                 // Add track list to model
109                 ArrayList<SearchResult> trackList = xmlHandler.getTrackList();
110                 _trackListModel.addTracks(trackList);
111
112                 // Show error message if any
113                 if (_trackListModel.isEmpty())
114                 {
115                         String error = xmlHandler.getErrorMessage();
116                         if (error != null && !error.equals(""))
117                         {
118                                 _app.showErrorMessageNoLookup(getNameKey(), error);
119                                 _errorMessage = error;
120                         }
121                 }
122         }
123
124         /**
125          * Load the selected point(s)
126          */
127         protected void loadSelected()
128         {
129                 // Find the rows selected in the table and get the corresponding coords
130                 int numSelected = _trackTable.getSelectedRowCount();
131                 if (numSelected < 1) return;
132                 int[] rowNums = _trackTable.getSelectedRows();
133                 for (int i=0; i<numSelected; i++)
134                 {
135                         int rowNum = rowNums[i];
136                         if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
137                         {
138                                 String lat = _trackListModel.getTrack(rowNum).getLatitude();
139                                 String lon = _trackListModel.getTrack(rowNum).getLongitude();
140                                 if (lat != null && lon != null)
141                                 {
142                                         DataPoint point = new DataPoint(new Latitude(lat), new Longitude(lon), null);
143                                         point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false);
144                                         _app.createPoint(point);
145                                 }
146                         }
147                 }
148                 // Close the dialog
149                 _cancelled = true;
150                 _dialog.dispose();
151         }
152 }