Changeset 16419 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2020-05-16T00:08:03+02:00 (4 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/download/PlaceSelection.java
r16418 r16419 16 16 import java.text.DecimalFormat; 17 17 import java.util.ArrayList; 18 import java.util.Collection; 18 19 import java.util.Collections; 19 20 import java.util.List; 21 import java.util.Objects; 20 22 import java.util.StringTokenizer; 23 import java.util.function.BiFunction; 24 import java.util.function.Consumer; 21 25 22 26 import javax.swing.AbstractAction; … … 57 61 import org.openstreetmap.josm.tools.ImageProvider; 58 62 import org.openstreetmap.josm.tools.Logging; 59 import org.openstreetmap.josm.tools.Utils;60 63 import org.xml.sax.SAXException; 61 64 import org.xml.sax.SAXParseException; … … 74 77 private DownloadDialog parent; 75 78 private static final Server[] SERVERS = { 76 new Server("Nominatim", NameFinder .NOMINATIM_URL, tr("Class Type"), tr("Bounds"))79 new Server("Nominatim", NameFinder::buildNominatimURL, tr("Class Type"), tr("Bounds")) 77 80 }; 78 private final JosmComboBox<Server> server = new JosmComboBox<>(SERVERS);81 private final JosmComboBox<Server> serverComboBox = new JosmComboBox<>(SERVERS); 79 82 80 83 private static class Server { 81 84 public final String name; 82 public final String url;85 public final BiFunction<String, Collection<SearchResult>, URL> urlFunction; 83 86 public final String thirdcol; 84 87 public final String fourthcol; 85 88 86 Server(String n, Stringu, String t, String f) {89 Server(String n, BiFunction<String, Collection<SearchResult>, URL> u, String t, String f) { 87 90 name = n; 88 url = u;91 urlFunction = u; 89 92 thirdcol = t; 90 93 fourthcol = f; … … 102 105 103 106 lpanel.add(new JLabel(tr("Choose the server for searching:")), GBC.std(0, 0).weight(0, 0).insets(0, 0, 5, 0)); 104 lpanel.add(server , GBC.std(1, 0).fill(GBC.HORIZONTAL));107 lpanel.add(serverComboBox, GBC.std(1, 0).fill(GBC.HORIZONTAL)); 105 108 String s = Config.getPref().get("namefinder.server", SERVERS[0].name); 106 109 for (int i = 0; i < SERVERS.length; ++i) { 107 110 if (SERVERS[i].name.equals(s)) { 108 server .setSelectedIndex(i);111 serverComboBox.setSelectedIndex(i); 109 112 } 110 113 } … … 171 174 } 172 175 176 /** 177 * Action to perform initial search, and (if query is unchanged) load more results. 178 */ 173 179 class SearchAction extends AbstractAction implements DocumentListener { 174 180 181 String lastSearchExpression; 182 boolean isSearchMore; 183 175 184 SearchAction() { 176 putValue(NAME, tr("Search..."));177 185 new ImageProvider("dialogs", "search").getResource().attachImageIcon(this, true); 178 putValue(SHORT_DESCRIPTION, tr("Click to start searching for places")); 179 updateEnabledState(); 186 updateState(); 180 187 } 181 188 182 189 @Override 183 190 public void actionPerformed(ActionEvent e) { 184 if (!isEnabled() || cbSearchExpression.getText().trim().isEmpty()) 191 String searchExpression = cbSearchExpression.getText(); 192 if (!isEnabled() || searchExpression.trim().isEmpty()) 185 193 return; 186 194 cbSearchExpression.addCurrentItemToHistory(); 187 195 Config.getPref().putList(HISTORY_KEY, cbSearchExpression.getHistory()); 188 NameQueryTask task = new NameQueryTask(cbSearchExpression.getText()); 196 Server server = (Server) serverComboBox.getSelectedItem(); 197 URL url = server.urlFunction.apply(searchExpression, isSearchMore ? model.getData() : Collections.emptyList()); 198 NameQueryTask task = new NameQueryTask(url, data -> { 199 if (isSearchMore) { 200 model.addData(data); 201 } else { 202 model.setData(data); 203 } 204 Config.getPref().put("namefinder.server", server.name); 205 columnmodel.setHeadlines(server.thirdcol, server.fourthcol); 206 lastSearchExpression = searchExpression; 207 updateState(); 208 }); 189 209 MainApplication.worker.submit(task); 190 210 } 191 211 192 protected final void updateEnabledState() { 193 setEnabled(!cbSearchExpression.getText().trim().isEmpty()); 212 protected final void updateState() { 213 String searchExpression = cbSearchExpression.getText(); 214 setEnabled(!searchExpression.trim().isEmpty()); 215 isSearchMore = Objects.equals(lastSearchExpression, searchExpression) && !model.getData().isEmpty(); 216 if (isSearchMore) { 217 putValue(NAME, tr("Search more...")); 218 putValue(SHORT_DESCRIPTION, tr("Click to search for more places")); 219 } else { 220 putValue(NAME, tr("Search...")); 221 putValue(SHORT_DESCRIPTION, tr("Click to start searching for places")); 222 } 194 223 } 195 224 196 225 @Override 197 226 public void changedUpdate(DocumentEvent e) { 198 update EnabledState();227 updateState(); 199 228 } 200 229 201 230 @Override 202 231 public void insertUpdate(DocumentEvent e) { 203 update EnabledState();232 updateState(); 204 233 } 205 234 206 235 @Override 207 236 public void removeUpdate(DocumentEvent e) { 208 updateEnabledState(); 209 } 210 } 211 212 class NameQueryTask extends PleaseWaitRunnable { 213 214 private final String searchExpression; 237 updateState(); 238 } 239 } 240 241 static class NameQueryTask extends PleaseWaitRunnable { 242 243 private final URL url; 244 private final Consumer<List<SearchResult>> dataConsumer; 215 245 private HttpClient connection; 216 246 private List<SearchResult> data; 217 247 private boolean canceled; 218 private final Server useserver;219 248 private Exception lastException; 220 249 221 NameQueryTask( String searchExpression) {250 NameQueryTask(URL url, Consumer<List<SearchResult>> dataConsumer) { 222 251 super(tr("Querying name server"), false /* don't ignore exceptions */); 223 this.searchExpression = searchExpression; 224 useserver = (Server) server.getSelectedItem(); 225 Config.getPref().put("namefinder.server", useserver.name); 252 this.url = url; 253 this.dataConsumer = dataConsumer; 226 254 } 227 255 … … 244 272 return; 245 273 } 246 columnmodel.setHeadlines(useserver.thirdcol, useserver.fourthcol); 247 model.setData(this.data); 274 dataConsumer.accept(data); 248 275 } 249 276 250 277 @Override 251 278 protected void realRun() throws SAXException, IOException, OsmTransferException { 252 String urlString = useserver.url+Utils.encodeUrl(searchExpression);253 254 279 try { 255 280 getProgressMonitor().indeterminateSubTask(tr("Querying name server ...")); 256 URL url = new URL(urlString);257 281 synchronized (this) { 258 282 connection = HttpClient.create(url); … … 265 289 if (!canceled) { 266 290 // Nominatim sometimes returns garbage, see #5934, #10643 267 Logging.log(Logging.LEVEL_WARN, tr("Error occurred with query ''{0}'': ''{1}''", url String, e.getMessage()), e);291 Logging.log(Logging.LEVEL_WARN, tr("Error occurred with query ''{0}'': ''{1}''", url, e.getMessage()), e); 268 292 GuiHelper.runInEDTAndWait(() -> HelpAwareOptionPane.showOptionDialog( 269 293 MainApplication.getMainFrame(), … … 276 300 if (!canceled) { 277 301 OsmTransferException ex = new OsmTransferException(e); 278 ex.setUrl(url String);302 ex.setUrl(url.toString()); 279 303 lastException = ex; 280 304 } … … 309 333 } 310 334 fireTableDataChanged(); 335 } 336 337 public void addData(List<SearchResult> data) { 338 this.data.addAll(data); 339 fireTableDataChanged(); 340 } 341 342 public List<SearchResult> getData() { 343 return Collections.unmodifiableList(data); 311 344 } 312 345 -
trunk/src/org/openstreetmap/josm/io/NameFinder.java
r15969 r16419 6 6 import java.io.IOException; 7 7 import java.io.Reader; 8 import java.io.UncheckedIOException; 9 import java.net.MalformedURLException; 8 10 import java.net.URL; 11 import java.util.Collection; 9 12 import java.util.Collections; 10 13 import java.util.LinkedList; 11 14 import java.util.List; 15 import java.util.Optional; 16 import java.util.stream.Collectors; 12 17 13 18 import javax.xml.parsers.ParserConfigurationException; … … 51 56 52 57 /** 58 * Builds the Nominatim URL for performing the given search 59 * @param searchExpression the Nominatim query 60 * @return the Nominatim URL 61 */ 62 public static URL buildNominatimURL(String searchExpression) { 63 return buildNominatimURL(searchExpression, Collections.emptyList()); 64 } 65 66 /** 67 * Builds the Nominatim URL for performing the given search and excluding the results (of a previous search) 68 * @param searchExpression the Nominatim query 69 * @param excludeResults the results to exclude 70 * @return the Nominatim URL 71 * @see <a href="https://nominatim.org/release-docs/develop/api/Search/#result-limitation">Result limitation in Nominatim Documentation</a> 72 */ 73 public static URL buildNominatimURL(String searchExpression, Collection<SearchResult> excludeResults) { 74 try { 75 final String excludeString = excludeResults.isEmpty() 76 ? "" 77 : excludeResults.stream() 78 .map(SearchResult::getPlaceId) 79 .map(String::valueOf) 80 .collect(Collectors.joining(",", "&exclude_place_ids=", "")); 81 return new URL(NOMINATIM_URL_PROP.get() + Utils.encodeUrl(searchExpression) + excludeString); 82 } catch (MalformedURLException ex) { 83 throw new UncheckedIOException(ex); 84 } 85 } 86 87 /** 53 88 * Performs a Nominatim search. 54 89 * @param searchExpression Nominatim search expression … … 57 92 */ 58 93 public static List<SearchResult> queryNominatim(final String searchExpression) throws IOException { 59 return query( new URL(NOMINATIM_URL_PROP.get() + Utils.encodeUrl(searchExpression)));94 return query(buildNominatimURL(searchExpression)); 60 95 } 61 96 … … 108 143 private Bounds bounds; 109 144 private PrimitiveId osmId; 145 private long placeId; 110 146 111 147 /** … … 179 215 public final PrimitiveId getOsmId() { 180 216 return osmId; 217 } 218 219 /** 220 * Returns the Nominatim place id. 221 * @return the Nominatim place id 222 */ 223 public long getPlaceId() { 224 return placeId; 181 225 } 182 226 … … 249 293 currentResult.osmId = new SimplePrimitiveId(Long.parseLong(osmId), OsmPrimitiveType.from(osmType)); 250 294 } 295 currentResult.placeId = Optional.ofNullable(atts.getValue("place_id")).filter(s -> !s.isEmpty()) 296 .map(Long::parseLong).orElse(0L); 251 297 data.add(currentResult); 252 298 }
Note:
See TracChangeset
for help on using the changeset viewer.