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;
012
013import javax.swing.JLabel;
014import javax.swing.JOptionPane;
015import javax.swing.JPanel;
016
017import org.openstreetmap.josm.Main;
018import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
019import org.openstreetmap.josm.gui.ExtendedDialog;
020import org.openstreetmap.josm.gui.Notification;
021import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
022import org.openstreetmap.josm.io.OsmApi;
023import org.openstreetmap.josm.tools.Utils;
024
025/**
026 * Action to use the Notes search API to download all notes matching a given search term.
027 * @since 8071
028 */
029public class SearchNotesDownloadAction extends JosmAction {
030
031    private static final String HISTORY_KEY = "osm.notes.searchHistory";
032
033    /** Constructs a new note search action */
034    public SearchNotesDownloadAction() {
035        super(tr("Search Notes..."), "note_search", tr("Download notes from the note search API"), null, false);
036    }
037
038    @Override
039    public void actionPerformed(ActionEvent e) {
040        HistoryComboBox searchTermBox = new HistoryComboBox();
041        List<String> searchHistory = new LinkedList<>(Main.pref.getCollection(HISTORY_KEY, new LinkedList<String>()));
042        Collections.reverse(searchHistory);
043        searchTermBox.setPossibleItems(searchHistory);
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(Main.parent, tr("Search for notes"),
055                new String[] {tr("Search for notes"), tr("Cancel")});
056        ed.setContent(contentPanel);
057        ed.setButtonIcons(new String[] {"note_search", "cancel"});
058        ed.showDialog();
059        if (ed.getValue() != 1) {
060            return;
061        }
062
063        String searchTerm = searchTermBox.getText();
064        if (searchTerm == null || searchTerm.trim().isEmpty()) {
065            Notification notification = new Notification(tr("You must enter a search term"));
066            notification.setIcon(JOptionPane.WARNING_MESSAGE);
067            notification.show();
068            return;
069        }
070
071        searchTermBox.addCurrentItemToHistory();
072        Main.pref.putCollection(HISTORY_KEY, searchTermBox.getHistory());
073
074        performSearch(searchTerm);
075    }
076
077    /**
078     * Perform search.
079     * @param searchTerm search term
080     */
081    public void performSearch(String searchTerm) {
082
083        String trimmedSearchTerm = searchTerm.trim();
084
085        try {
086            final long id = Long.parseLong(trimmedSearchTerm);
087            new DownloadNotesTask().download(id, null);
088            return;
089        } catch (NumberFormatException ignore) {
090            if (Main.isTraceEnabled()) {
091                Main.trace(ignore.getMessage());
092            }
093        }
094
095        int noteLimit = Main.pref.getInteger("osm.notes.downloadLimit", 1000);
096        int closedLimit = Main.pref.getInteger("osm.notes.daysCloased", 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(false, sb.toString(), null);
108    }
109}