| 1 | // License: GPL. For details, see LICENSE file. |
| 2 | package org.openstreetmap.josm.actions; |
| 3 | |
| 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
| 5 | |
| 6 | import java.awt.GridBagConstraints; |
| 7 | import java.awt.GridBagLayout; |
| 8 | import java.awt.event.ActionEvent; |
| 9 | import java.io.UnsupportedEncodingException; |
| 10 | import java.net.URLEncoder; |
| 11 | import java.util.Collections; |
| 12 | import java.util.LinkedList; |
| 13 | import java.util.List; |
| 14 | |
| 15 | import javax.swing.JLabel; |
| 16 | import javax.swing.JOptionPane; |
| 17 | import javax.swing.JPanel; |
| 18 | |
| 19 | import org.openstreetmap.josm.Main; |
| 20 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask; |
| 21 | import org.openstreetmap.josm.gui.ExtendedDialog; |
| 22 | import org.openstreetmap.josm.gui.Notification; |
| 23 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox; |
| 24 | import org.openstreetmap.josm.io.OsmApi; |
| 25 | |
| 26 | /** |
| 27 | * Action to use the Notes search API to download all |
| 28 | * notes matching a given search term |
| 29 | */ |
| 30 | public class SearchNotesDownloadAction extends JosmAction { |
| 31 | |
| 32 | private static final String HISTORY_KEY = "osm.notes.searchHistory"; |
| 33 | |
| 34 | /** Constructs a new note search action */ |
| 35 | public SearchNotesDownloadAction() { |
| 36 | super(tr("Search Notes..."), "note_search", tr("Download notes from the note search API"), null, false); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public void actionPerformed(ActionEvent e) { |
| 41 | Main.debug("searching for notes"); |
| 42 | |
| 43 | HistoryComboBox searchTermBox = new HistoryComboBox(); |
| 44 | List<String> searchHistory = new LinkedList<>(Main.pref.getCollection(HISTORY_KEY, new LinkedList<String>())); |
| 45 | Collections.reverse(searchHistory); |
| 46 | searchTermBox.setPossibleItems(searchHistory); |
| 47 | |
| 48 | JPanel contentPanel = new JPanel(new GridBagLayout()); |
| 49 | GridBagConstraints gc = new GridBagConstraints(); |
| 50 | gc.fill = GridBagConstraints.HORIZONTAL; |
| 51 | gc.weightx = 1.0; |
| 52 | gc.anchor = GridBagConstraints.FIRST_LINE_START; |
| 53 | contentPanel.add(new JLabel(tr("Search the OSM API for notes containing words:")), gc); |
| 54 | gc.gridy = 1; |
| 55 | contentPanel.add(searchTermBox, gc); |
| 56 | |
| 57 | ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Search for notes"), new String[] {tr("Search for notes"), tr("Cancel")}); |
| 58 | ed.setContent(contentPanel); |
| 59 | ed.setButtonIcons(new String[] {"note_search.png", "cancel.png"}); |
| 60 | ed.showDialog(); |
| 61 | if (ed.getValue() != 1) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | String searchTerm = searchTermBox.getText(); |
| 66 | if (searchTerm == null || searchTerm.trim().isEmpty()) { |
| 67 | Notification notification = new Notification(tr("You must enter a search term")); |
| 68 | notification.setIcon(JOptionPane.WARNING_MESSAGE); |
| 69 | notification.show(); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | searchTermBox.addCurrentItemToHistory(); |
| 74 | Main.pref.putCollection(HISTORY_KEY, searchTermBox.getHistory()); |
| 75 | |
| 76 | searchTerm = searchTerm.trim(); |
| 77 | int noteLimit = Main.pref.getInteger("osm.notes.downloadLimit", 1000); |
| 78 | int closedLimit = Main.pref.getInteger("osm.notes.daysCloased", 7); |
| 79 | |
| 80 | StringBuilder sb = new StringBuilder(); |
| 81 | sb.append(OsmApi.getOsmApi().getBaseUrl()); |
| 82 | sb.append("notes/search?limit="); |
| 83 | sb.append(noteLimit); |
| 84 | sb.append("&closed="); |
| 85 | sb.append(closedLimit); |
| 86 | sb.append("&q="); |
| 87 | try { |
| 88 | sb.append(URLEncoder.encode(searchTerm, "UTF-8")); |
| 89 | } catch (UnsupportedEncodingException ex) { |
| 90 | Main.error(ex, true); //thrown if UTF-8 isn't supported which seems unlikely. |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | DownloadNotesTask task = new DownloadNotesTask(); |
| 95 | task.loadUrl(false, sb.toString(), null); |
| 96 | } |
| 97 | } |