[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;
|
---|
| 12 |
|
---|
| 13 | import javax.swing.JLabel;
|
---|
| 14 | import javax.swing.JOptionPane;
|
---|
| 15 | import javax.swing.JPanel;
|
---|
| 16 |
|
---|
| 17 | import org.openstreetmap.josm.Main;
|
---|
| 18 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
|
---|
| 19 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
| 20 | import org.openstreetmap.josm.gui.Notification;
|
---|
| 21 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
| 22 | import org.openstreetmap.josm.io.OsmApi;
|
---|
[8304] | 23 | import org.openstreetmap.josm.tools.Utils;
|
---|
[8071] | 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Action to use the Notes search API to download all notes matching a given search term.
|
---|
| 27 | * @since 8071
|
---|
| 28 | */
|
---|
| 29 | public class SearchNotesDownloadAction extends JosmAction {
|
---|
| 30 |
|
---|
| 31 | private static final String HISTORY_KEY = "osm.notes.searchHistory";
|
---|
| 32 |
|
---|
| 33 | /** Constructs a new note search action */
|
---|
| 34 | public SearchNotesDownloadAction() {
|
---|
| 35 | super(tr("Search Notes..."), "note_search", tr("Download notes from the note search API"), null, false);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | @Override
|
---|
| 39 | public void actionPerformed(ActionEvent e) {
|
---|
| 40 | HistoryComboBox searchTermBox = new HistoryComboBox();
|
---|
| 41 | List<String> searchHistory = new LinkedList<>(Main.pref.getCollection(HISTORY_KEY, new LinkedList<String>()));
|
---|
| 42 | Collections.reverse(searchHistory);
|
---|
| 43 | searchTermBox.setPossibleItems(searchHistory);
|
---|
| 44 |
|
---|
| 45 | JPanel contentPanel = new JPanel(new GridBagLayout());
|
---|
| 46 | GridBagConstraints gc = new GridBagConstraints();
|
---|
| 47 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
| 48 | gc.weightx = 1.0;
|
---|
| 49 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
| 50 | contentPanel.add(new JLabel(tr("Search the OSM API for notes containing words:")), gc);
|
---|
| 51 | gc.gridy = 1;
|
---|
| 52 | contentPanel.add(searchTermBox, gc);
|
---|
| 53 |
|
---|
| 54 | ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Search for notes"),
|
---|
| 55 | new String[] {tr("Search for notes"), tr("Cancel")});
|
---|
| 56 | ed.setContent(contentPanel);
|
---|
| 57 | ed.setButtonIcons(new String[] {"note_search", "cancel"});
|
---|
| 58 | ed.showDialog();
|
---|
| 59 | if (ed.getValue() != 1) {
|
---|
| 60 | return;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | String searchTerm = searchTermBox.getText();
|
---|
| 64 | if (searchTerm == null || searchTerm.trim().isEmpty()) {
|
---|
| 65 | Notification notification = new Notification(tr("You must enter a search term"));
|
---|
| 66 | notification.setIcon(JOptionPane.WARNING_MESSAGE);
|
---|
| 67 | notification.show();
|
---|
| 68 | return;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | searchTermBox.addCurrentItemToHistory();
|
---|
| 72 | Main.pref.putCollection(HISTORY_KEY, searchTermBox.getHistory());
|
---|
| 73 |
|
---|
[8196] | 74 | performSearch(searchTerm);
|
---|
| 75 |
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public void performSearch(String searchTerm) {
|
---|
| 79 |
|
---|
[8071] | 80 | searchTerm = searchTerm.trim();
|
---|
[8196] | 81 |
|
---|
| 82 | try {
|
---|
| 83 | final long id = Long.parseLong(searchTerm);
|
---|
| 84 | new DownloadNotesTask().download(false, id, null);
|
---|
| 85 | return;
|
---|
| 86 | } catch (NumberFormatException ignore) {
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[8071] | 89 | int noteLimit = Main.pref.getInteger("osm.notes.downloadLimit", 1000);
|
---|
| 90 | int closedLimit = Main.pref.getInteger("osm.notes.daysCloased", 7);
|
---|
| 91 |
|
---|
| 92 | StringBuilder sb = new StringBuilder();
|
---|
| 93 | sb.append(OsmApi.getOsmApi().getBaseUrl());
|
---|
| 94 | sb.append("notes/search?limit=");
|
---|
| 95 | sb.append(noteLimit);
|
---|
| 96 | sb.append("&closed=");
|
---|
| 97 | sb.append(closedLimit);
|
---|
| 98 | sb.append("&q=");
|
---|
[8304] | 99 | sb.append(Utils.encodeUrl(searchTerm));
|
---|
[8071] | 100 |
|
---|
| 101 | new DownloadNotesTask().loadUrl(false, sb.toString(), null);
|
---|
| 102 | }
|
---|
| 103 | }
|
---|