001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.GridBagConstraints;
007import java.awt.GridBagLayout;
008import java.awt.event.ActionEvent;
009import java.util.Collections;
010import java.util.Optional;
011
012import javax.swing.JLabel;
013import javax.swing.JOptionPane;
014import javax.swing.JPanel;
015
016import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
017import org.openstreetmap.josm.actions.downloadtasks.DownloadParams;
018import org.openstreetmap.josm.gui.ExtendedDialog;
019import org.openstreetmap.josm.gui.MainApplication;
020import org.openstreetmap.josm.gui.Notification;
021import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
022import org.openstreetmap.josm.io.OsmApi;
023import org.openstreetmap.josm.spi.preferences.Config;
024import org.openstreetmap.josm.tools.Logging;
025import org.openstreetmap.josm.tools.Utils;
026
027/**
028 * Action to use the Notes search API to download all notes matching a given search term.
029 * @since 8071
030 */
031public class SearchNotesDownloadAction extends JosmAction {
032
033    private static final String HISTORY_KEY = "osm.notes.searchHistory";
034
035    /** Constructs a new note search action */
036    public SearchNotesDownloadAction() {
037        super(tr("Search Notes..."), "note_search", tr("Download notes from the note search API"), null, false);
038    }
039
040    @Override
041    public void actionPerformed(ActionEvent e) {
042        HistoryComboBox searchTermBox = new HistoryComboBox();
043        searchTermBox.setPossibleItemsTopDown(Config.getPref().getList(HISTORY_KEY, Collections.emptyList()));
044
045        JPanel contentPanel = new JPanel(new GridBagLayout());
046        GridBagConstraints gc = new GridBagConstraints();
047        gc.fill = GridBagConstraints.HORIZONTAL;
048        gc.weightx = 1.0;
049        gc.anchor = GridBagConstraints.FIRST_LINE_START;
050        contentPanel.add(new JLabel(tr("Search the OSM API for notes containing words:")), gc);
051        gc.gridy = 1;
052        contentPanel.add(searchTermBox, gc);
053
054        ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(), tr("Search for notes"), tr("Search for notes"), tr("Cancel"))
055            .setContent(contentPanel)
056            .setButtonIcons("note_search", "cancel");
057        ed.configureContextsensitiveHelp("/Action/SearchNotesDownload", true /* show help button */);
058        if (ed.showDialog().getValue() != 1) {
059            return;
060        }
061
062        String searchTerm = Optional.ofNullable(searchTermBox.getText()).orElse("").trim();
063        if (searchTerm.isEmpty()) {
064            new Notification(tr("You must enter a search term"))
065                .setIcon(JOptionPane.WARNING_MESSAGE)
066                .show();
067            return;
068        }
069
070        searchTermBox.addCurrentItemToHistory();
071        Config.getPref().putList(HISTORY_KEY, searchTermBox.getHistory());
072
073        performSearch(searchTerm);
074    }
075
076    /**
077     * Perform search.
078     * @param searchTerm search term
079     */
080    public void performSearch(String searchTerm) {
081
082        String trimmedSearchTerm = searchTerm.trim();
083
084        try {
085            final long id = Long.parseLong(trimmedSearchTerm);
086            new DownloadNotesTask().download(id, null);
087            return;
088        } catch (NumberFormatException ignore) {
089            Logging.trace(ignore);
090        }
091
092        int noteLimit = Config.getPref().getInt("osm.notes.downloadLimit", 1000);
093        int closedLimit = Config.getPref().getInt("osm.notes.daysClosed", 7);
094
095        StringBuilder sb = new StringBuilder(128);
096        sb.append(OsmApi.getOsmApi().getBaseUrl())
097            .append("notes/search?limit=")
098            .append(noteLimit)
099            .append("&closed=")
100            .append(closedLimit)
101            .append("&q=")
102            .append(Utils.encodeUrl(trimmedSearchTerm));
103
104        new DownloadNotesTask().loadUrl(new DownloadParams(), sb.toString(), null);
105    }
106}