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