]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/SearchWikipediaNames.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / function / SearchWikipediaNames.java
1 package tim.prune.function;
2
3 import java.io.InputStream;
4 import java.io.UnsupportedEncodingException;
5 import java.net.URL;
6 import java.net.URLEncoder;
7 import java.util.ArrayList;
8
9 import javax.swing.JOptionPane;
10 import javax.xml.parsers.SAXParser;
11 import javax.xml.parsers.SAXParserFactory;
12
13 import tim.prune.App;
14 import tim.prune.I18nManager;
15 import tim.prune.data.DataPoint;
16 import tim.prune.data.Field;
17 import tim.prune.data.Latitude;
18 import tim.prune.data.Longitude;
19 import tim.prune.function.gpsies.GenericDownloaderFunction;
20 import tim.prune.function.gpsies.GpsiesTrack;
21
22 /**
23  * Function to search Wikipedia for place names
24  */
25 public class SearchWikipediaNames extends GenericDownloaderFunction
26 {
27         /** search term */
28         private String _searchTerm = null;
29         /** Maximum number of results to get */
30         private static final int MAX_RESULTS = 20;
31         /** Username to use for geonames queries */
32         private static final String GEONAMES_USERNAME = "gpsprune";
33
34         /**
35          * Constructor
36          * @param inApp App object
37          */
38         public SearchWikipediaNames(App inApp) {
39                 super(inApp);
40         }
41
42         /**
43          * @return name key
44          */
45         public String getNameKey() {
46                 return "function.searchwikipedianames";
47         }
48
49         /**
50          * @param inColNum index of column, 0 or 1
51          * @return key for this column
52          */
53         protected String getColumnKey(int inColNum)
54         {
55                 if (inColNum == 0) return "dialog.wikipedia.column.name";
56                 return null;
57         }
58
59         /**
60          * Before dialog is shown, need to get search term
61          */
62         public void begin()
63         {
64                 Object search = JOptionPane.showInputDialog(_app.getFrame(),
65                         I18nManager.getText("dialog.searchwikipedianames.search"),
66                         I18nManager.getText(getNameKey()),
67                         JOptionPane.QUESTION_MESSAGE, null, null, "");
68                 if (search != null)
69                 {
70                         _searchTerm = search.toString();
71                         if (!_searchTerm.equals("")) {
72                                 super.begin();
73                         }
74                 }
75         }
76
77         /**
78          * Run method to call geonames in separate thread
79          */
80         public void run()
81         {
82                 _statusLabel.setText(I18nManager.getText("confirm.running"));
83
84                 String descMessage = "";
85                 InputStream inStream = null;
86
87                 // language (only de and en available)
88                 String lang = I18nManager.getText("wikipedia.lang");
89                 if (lang.equals("de") || lang.equals("als")) {
90                         lang = "de";
91                 }
92                 else {
93                         lang = "en";
94                 }
95                 // Replace awkward characters with character equivalents
96                 String searchTerm;
97                 try {
98                         searchTerm = URLEncoder.encode(_searchTerm, "UTF-8");
99                 } catch (UnsupportedEncodingException e1) {
100                         searchTerm = _searchTerm;
101                 }
102                 // Example http://ws.geonames.org/wikipediaSearch?q=london&maxRows=10
103                 String urlString = "http://api.geonames.org/wikipediaSearch?title=" + searchTerm
104                         + "&maxRows=" + MAX_RESULTS + "&lang=" + lang + "&username=" + GEONAMES_USERNAME;
105                 // Parse the returned XML with a special handler
106                 GetWikipediaXmlHandler xmlHandler = new GetWikipediaXmlHandler();
107                 try
108                 {
109                         URL url = new URL(urlString);
110                         SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
111                         inStream = url.openStream();
112                         saxParser.parse(inStream, xmlHandler);
113                 }
114                 catch (Exception e) {
115                         descMessage = e.getClass().getName() + " - " + e.getMessage();
116                 }
117                 // Close stream and ignore errors
118                 try {
119                         inStream.close();
120                 } catch (Exception e) {}
121                 // Add track list to model
122                 ArrayList<GpsiesTrack> trackList = xmlHandler.getTrackList();
123                 // TODO: Do a better job of sorting replies by relevance - use three different lists
124                 _trackListModel.addTracks(trackList);
125
126                 // Set status label according to error or "none found", leave blank if ok
127                 if (descMessage.equals("") && (trackList == null || trackList.size() == 0)) {
128                         descMessage = I18nManager.getText("dialog.gpsies.nonefound");
129                 }
130                 _statusLabel.setText(descMessage);
131         }
132
133
134         /**
135          * Load the selected point(s)
136          */
137         protected void loadSelected()
138         {
139                 // Find the rows selected in the table and get the corresponding coords
140                 int numSelected = _trackTable.getSelectedRowCount();
141                 if (numSelected < 1) return;
142                 int[] rowNums = _trackTable.getSelectedRows();
143                 for (int i=0; i<numSelected; i++)
144                 {
145                         int rowNum = rowNums[i];
146                         if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
147                         {
148                                 String coords = _trackListModel.getTrack(rowNum).getDownloadLink();
149                                 String[] latlon = coords.split(",");
150                                 if (latlon.length == 2)
151                                 {
152                                         DataPoint point = new DataPoint(new Latitude(latlon[0]), new Longitude(latlon[1]), null);
153                                         point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false);
154                                         _app.createPoint(point);
155                                 }
156                         }
157                 }
158                 // Close the dialog
159                 _cancelled = true;
160                 _dialog.dispose();
161         }
162 }