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.awt.event.KeyEvent;
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;
026import org.openstreetmap.josm.tools.Shortcut;
027
028/**
029 * Action to use the Notes search API to download all notes matching a given search term.
030 * @since 8071
031 */
032public class SearchNotesDownloadAction extends JosmAction {
033
034    private static final String HISTORY_KEY = "osm.notes.searchHistory";
035
036    /** Constructs a new note search action */
037    public SearchNotesDownloadAction() {
038        super(tr("Search Notes..."), "note_search", tr("Download notes from the note search API"),
039                Shortcut.registerShortcut("file:notesearch",
040                tr("File: {0}", tr("Search Notes...")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false, false);
041    }
042
043    @Override
044    public void actionPerformed(ActionEvent e) {
045        HistoryComboBox searchTermBox = new HistoryComboBox();
046        searchTermBox.getModel().prefs().load(HISTORY_KEY);
047
048        JPanel contentPanel = new JPanel(new GridBagLayout());
049        GridBagConstraints gc = new GridBagConstraints();
050        gc.fill = GridBagConstraints.HORIZONTAL;
051        gc.weightx = 1.0;
052        gc.anchor = GridBagConstraints.FIRST_LINE_START;
053        contentPanel.add(new JLabel(tr("Search the OSM API for notes containing words:")), gc);
054        gc.gridy = 1;
055        contentPanel.add(searchTermBox, gc);
056
057        ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(), tr("Search for notes"), tr("Search for notes"), tr("Cancel"))
058            .setContent(contentPanel)
059            .setButtonIcons("note_search", "cancel");
060        ed.configureContextsensitiveHelp("/Action/SearchNotesDownload", true /* show help button */);
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        searchTermBox.getModel().prefs().save(HISTORY_KEY);
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}