]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/SearchWikipediaNames.java
4377df81d5a39bbdd65bc897d0bf575104b2011e
[GpsPrune.git] / tim / prune / function / SearchWikipediaNames.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.swing.JOptionPane;
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.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;
19
20 /**
21  * Function to search Wikipedia for place names
22  */
23 public class SearchWikipediaNames extends GenericDownloaderFunction
24 {
25         /** search term */
26         private String _searchTerm = null;
27         /** Maximum number of results to get */
28         private static final int MAX_RESULTS = 20;
29
30         /**
31          * Constructor
32          * @param inApp App object
33          */
34         public SearchWikipediaNames(App inApp) {
35                 super(inApp);
36         }
37
38         /**
39          * @return name key
40          */
41         public String getNameKey() {
42                 return "function.searchwikipedianames";
43         }
44
45         /**
46          * @param inColNum index of column, 0 or 1
47          * @return key for this column
48          */
49         protected String getColumnKey(int inColNum)
50         {
51                 if (inColNum == 0) return "dialog.wikipedia.column.name";
52                 return null;
53         }
54
55         /**
56          * Before dialog is shown, need to get search term
57          */
58         public void begin()
59         {
60                 Object search = JOptionPane.showInputDialog(_app.getFrame(),
61                         I18nManager.getText("dialog.searchwikipedianames.search"),
62                         I18nManager.getText(getNameKey()),
63                         JOptionPane.QUESTION_MESSAGE, null, null, "");
64                 if (search != null)
65                 {
66                         _searchTerm = search.toString();
67                         if (!_searchTerm.equals("")) {
68                                 super.begin();
69                         }
70                 }
71         }
72
73         /**
74          * Run method to call geonames in separate thread
75          */
76         public void run()
77         {
78                 _statusLabel.setText(I18nManager.getText("confirm.running"));
79
80                 String descMessage = "";
81                 InputStream inStream = null;
82
83                 // language (only de and en available)
84                 String lang = I18nManager.getText("wikipedia.lang");
85                 if (lang.equals("de") || lang.equals("als")) {
86                         lang = "de";
87                 }
88                 else {
89                         lang = "en";
90                 }
91                 // Example http://ws.geonames.org/wikipediaSearch?q=london&maxRows=10
92                 String urlString = "http://ws.geonames.org/wikipediaSearch?title=" + _searchTerm + "&maxRows=" + MAX_RESULTS
93                         + "&lang=" + lang;
94                 // Parse the returned XML with a special handler
95                 GetWikipediaXmlHandler xmlHandler = new GetWikipediaXmlHandler();
96                 try
97                 {
98                         URL url = new URL(urlString);
99                         SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
100                         inStream = url.openStream();
101                         saxParser.parse(inStream, xmlHandler);
102                 }
103                 catch (Exception e) {
104                         descMessage = e.getClass().getName() + " - " + e.getMessage();
105                 }
106                 // Close stream and ignore errors
107                 try {
108                         inStream.close();
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);
114
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");
118                 }
119                 _statusLabel.setText(descMessage);
120         }
121
122         /**
123          * Load the selected track or point
124          */
125         protected void loadSelected()
126         {
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())
130                 {
131                         String coords = _trackListModel.getTrack(rowNum).getDownloadLink();
132                         String[] latlon = coords.split(",");
133                         if (latlon.length == 2)
134                         {
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);
138                         }
139                 }
140                 // Close the dialog
141                 _cancelled = true;
142                 _dialog.dispose();
143         }
144 }