1 package tim.prune.function;
3 import java.io.InputStream;
5 import java.util.ArrayList;
7 import javax.swing.JOptionPane;
8 import javax.xml.parsers.SAXParser;
9 import javax.xml.parsers.SAXParserFactory;
12 import tim.prune.I18nManager;
13 import tim.prune.data.DataPoint;
14 import tim.prune.data.Field;
15 import tim.prune.data.Latitude;
16 import tim.prune.data.Longitude;
17 import tim.prune.function.gpsies.GenericDownloaderFunction;
18 import tim.prune.function.gpsies.GpsiesTrack;
21 * Function to search Wikipedia for place names
23 public class SearchWikipediaNames extends GenericDownloaderFunction
26 private String _searchTerm = null;
27 /** Maximum number of results to get */
28 private static final int MAX_RESULTS = 20;
32 * @param inApp App object
34 public SearchWikipediaNames(App inApp) {
41 public String getNameKey() {
42 return "function.searchwikipedianames";
46 * @param inColNum index of column, 0 or 1
47 * @return key for this column
49 protected String getColumnKey(int inColNum)
51 if (inColNum == 0) return "dialog.wikipedia.column.name";
56 * Before dialog is shown, need to get search term
60 Object search = JOptionPane.showInputDialog(_app.getFrame(),
61 I18nManager.getText("dialog.searchwikipedianames.search"),
62 I18nManager.getText(getNameKey()),
63 JOptionPane.QUESTION_MESSAGE, null, null, "");
66 _searchTerm = search.toString();
67 if (!_searchTerm.equals("")) {
74 * Run method to call geonames in separate thread
78 _statusLabel.setText(I18nManager.getText("confirm.running"));
80 String descMessage = "";
81 InputStream inStream = null;
83 // language (only de and en available)
84 String lang = I18nManager.getText("wikipedia.lang");
85 if (lang.equals("de") || lang.equals("als")) {
91 // Example http://ws.geonames.org/wikipediaSearch?q=london&maxRows=10
92 String urlString = "http://ws.geonames.org/wikipediaSearch?title=" + _searchTerm + "&maxRows=" + MAX_RESULTS
94 // Parse the returned XML with a special handler
95 GetWikipediaXmlHandler xmlHandler = new GetWikipediaXmlHandler();
98 URL url = new URL(urlString);
99 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
100 inStream = url.openStream();
101 saxParser.parse(inStream, xmlHandler);
103 catch (Exception e) {
104 descMessage = e.getClass().getName() + " - " + e.getMessage();
106 // Close stream and ignore errors
109 } catch (Exception e) {}
110 // Add track list to model
111 ArrayList<GpsiesTrack> trackList = xmlHandler.getTrackList();
112 // TODO: Do a better job of sorting replies by relevance - use three different lists
113 _trackListModel.addTracks(trackList);
115 // Set status label according to error or "none found", leave blank if ok
116 if (descMessage.equals("") && (trackList == null || trackList.size() == 0)) {
117 descMessage = I18nManager.getText("dialog.gpsies.nonefound");
119 _statusLabel.setText(descMessage);
123 * Load the selected track or point
125 protected void loadSelected()
127 // Find the row selected in the table and get the corresponding track
128 int rowNum = _trackTable.getSelectedRow();
129 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
131 String coords = _trackListModel.getTrack(rowNum).getDownloadLink();
132 String[] latlon = coords.split(",");
133 if (latlon.length == 2)
135 DataPoint point = new DataPoint(new Latitude(latlon[0]), new Longitude(latlon[1]), null);
136 point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false);
137 _app.createPoint(point);