1 package tim.prune.function;
3 import java.io.InputStream;
5 import java.util.ArrayList;
7 import javax.xml.parsers.SAXParser;
8 import javax.xml.parsers.SAXParserFactory;
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;
20 * Function to load information about geocaches nearby to the current point
21 * using the service at opencaching.de
23 public class SearchOpenCachingDeFunction extends GenericDownloaderFunction
25 /** Maximum distance from point in km */
26 private static final int MAX_DISTANCE = 50;
31 * @param inApp App object
33 public SearchOpenCachingDeFunction(App inApp) {
40 public String getNameKey() {
41 return "function.searchopencachingde";
45 * @param inColNum index of column, 0 or 1
46 * @return key for this column
48 protected String getColumnKey(int inColNum)
50 if (inColNum == 0) return "dialog.wikipedia.column.name";
51 return "dialog.wikipedia.column.distance";
56 * Run method to call service in a separate thread
60 _statusLabel.setText(I18nManager.getText("confirm.running"));
61 // Get coordinates from current point
62 DataPoint point = _app.getTrackInfo().getCurrentPoint();
68 final double lat = point.getLatitude().getDouble();
69 final double lon = point.getLongitude().getDouble();
70 submitSearch(lat, lon);
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");
76 _statusLabel.setText(_errorMessage == null ? "" : _errorMessage);
80 * Submit the search for the given parameters
81 * @param inLat latitude
82 * @param inLon longitude
84 private void submitSearch(double inLat, double inLon)
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;
96 URL url = new URL(urlString);
97 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
98 inStream = url.openStream();
99 saxParser.parse(inStream, xmlHandler);
101 catch (Exception e) {
102 _errorMessage = e.getClass().getName() + " - " + e.getMessage();
104 // Close stream and ignore errors
107 } catch (Exception e) {}
108 // Add track list to model
109 ArrayList<SearchResult> trackList = xmlHandler.getTrackList();
110 _trackListModel.addTracks(trackList);
112 // Show error message if any
113 if (_trackListModel.isEmpty())
115 String error = xmlHandler.getErrorMessage();
116 if (error != null && !error.equals(""))
118 _app.showErrorMessageNoLookup(getNameKey(), error);
119 _errorMessage = error;
125 * Load the selected point(s)
127 protected void loadSelected()
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++)
135 int rowNum = rowNums[i];
136 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
138 String lat = _trackListModel.getTrack(rowNum).getLatitude();
139 String lon = _trackListModel.getTrack(rowNum).getLongitude();
140 if (lat != null && lon != null)
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);