[8071] | 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.util.Collections;
|
---|
| 10 | import java.util.LinkedList;
|
---|
| 11 | import java.util.List;
|
---|
[11889] | 12 | import java.util.Optional;
|
---|
[8071] | 13 |
|
---|
| 14 | import javax.swing.JLabel;
|
---|
| 15 | import javax.swing.JOptionPane;
|
---|
| 16 | import javax.swing.JPanel;
|
---|
| 17 |
|
---|
| 18 | import org.openstreetmap.josm.Main;
|
---|
| 19 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
|
---|
| 20 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
| 21 | import org.openstreetmap.josm.gui.Notification;
|
---|
| 22 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
| 23 | import org.openstreetmap.josm.io.OsmApi;
|
---|
[12846] | 24 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[12620] | 25 | import org.openstreetmap.josm.tools.Logging;
|
---|
[8304] | 26 | import org.openstreetmap.josm.tools.Utils;
|
---|
[8071] | 27 |
|
---|
| 28 | /**
|
---|
| 29 | * Action to use the Notes search API to download all notes matching a given search term.
|
---|
| 30 | * @since 8071
|
---|
| 31 | */
|
---|
| 32 | public class SearchNotesDownloadAction extends JosmAction {
|
---|
| 33 |
|
---|
| 34 | private static final String HISTORY_KEY = "osm.notes.searchHistory";
|
---|
| 35 |
|
---|
| 36 | /** Constructs a new note search action */
|
---|
| 37 | public SearchNotesDownloadAction() {
|
---|
| 38 | super(tr("Search Notes..."), "note_search", tr("Download notes from the note search API"), null, false);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | @Override
|
---|
| 42 | public void actionPerformed(ActionEvent e) {
|
---|
| 43 | HistoryComboBox searchTermBox = new HistoryComboBox();
|
---|
[12846] | 44 | List<String> searchHistory = new LinkedList<>(Config.getPref().getList(HISTORY_KEY, new LinkedList<String>()));
|
---|
[8071] | 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 |
|
---|
[12279] | 57 | ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Search for notes"), tr("Search for notes"), tr("Cancel"))
|
---|
| 58 | .setContent(contentPanel)
|
---|
| 59 | .setButtonIcons("note_search", "cancel");
|
---|
| 60 | if (ed.showDialog().getValue() != 1) {
|
---|
[8071] | 61 | return;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[11889] | 64 | String searchTerm = Optional.ofNullable(searchTermBox.getText()).orElse("").trim();
|
---|
| 65 | if (searchTerm.isEmpty()) {
|
---|
[12279] | 66 | new Notification(tr("You must enter a search term"))
|
---|
| 67 | .setIcon(JOptionPane.WARNING_MESSAGE)
|
---|
| 68 | .show();
|
---|
[8071] | 69 | return;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | searchTermBox.addCurrentItemToHistory();
|
---|
[12846] | 73 | Config.getPref().putList(HISTORY_KEY, searchTermBox.getHistory());
|
---|
[8071] | 74 |
|
---|
[8196] | 75 | performSearch(searchTerm);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[10242] | 78 | /**
|
---|
| 79 | * Perform search.
|
---|
| 80 | * @param searchTerm search term
|
---|
| 81 | */
|
---|
[8196] | 82 | public void performSearch(String searchTerm) {
|
---|
| 83 |
|
---|
[10242] | 84 | String trimmedSearchTerm = searchTerm.trim();
|
---|
[8196] | 85 |
|
---|
| 86 | try {
|
---|
[10242] | 87 | final long id = Long.parseLong(trimmedSearchTerm);
|
---|
[8855] | 88 | new DownloadNotesTask().download(id, null);
|
---|
[8196] | 89 | return;
|
---|
| 90 | } catch (NumberFormatException ignore) {
|
---|
[12620] | 91 | Logging.trace(ignore);
|
---|
[8196] | 92 | }
|
---|
| 93 |
|
---|
[12846] | 94 | int noteLimit = Config.getPref().getInt("osm.notes.downloadLimit", 1000);
|
---|
| 95 | int closedLimit = Config.getPref().getInt("osm.notes.daysClosed", 7);
|
---|
[8071] | 96 |
|
---|
[10242] | 97 | StringBuilder sb = new StringBuilder(128);
|
---|
[8379] | 98 | sb.append(OsmApi.getOsmApi().getBaseUrl())
|
---|
| 99 | .append("notes/search?limit=")
|
---|
| 100 | .append(noteLimit)
|
---|
| 101 | .append("&closed=")
|
---|
| 102 | .append(closedLimit)
|
---|
| 103 | .append("&q=")
|
---|
[10242] | 104 | .append(Utils.encodeUrl(trimmedSearchTerm));
|
---|
[8071] | 105 |
|
---|
| 106 | new DownloadNotesTask().loadUrl(false, sb.toString(), null);
|
---|
| 107 | }
|
---|
| 108 | }
|
---|