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.io.UnsupportedEncodingException;
010import java.net.URLEncoder;
011import java.util.Collections;
012import java.util.LinkedList;
013import java.util.List;
014
015import javax.swing.JLabel;
016import javax.swing.JOptionPane;
017import javax.swing.JPanel;
018
019import org.openstreetmap.josm.Main;
020import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
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;
025
026/**
027 * Action to use the Notes search API to download all notes matching a given search term.
028 * @since 8071
029 */
030public class SearchNotesDownloadAction extends JosmAction {
031
032    private static final String HISTORY_KEY = "osm.notes.searchHistory";
033
034    /** Constructs a new note search action */
035    public SearchNotesDownloadAction() {
036        super(tr("Search Notes..."), "note_search", tr("Download notes from the note search API"), null, false);
037    }
038
039    @Override
040    public void actionPerformed(ActionEvent e) {
041        HistoryComboBox searchTermBox = new HistoryComboBox();
042        List<String> searchHistory = new LinkedList<>(Main.pref.getCollection(HISTORY_KEY, new LinkedList<String>()));
043        Collections.reverse(searchHistory);
044        searchTermBox.setPossibleItems(searchHistory);
045
046        JPanel contentPanel = new JPanel(new GridBagLayout());
047        GridBagConstraints gc = new GridBagConstraints();
048        gc.fill = GridBagConstraints.HORIZONTAL;
049        gc.weightx = 1.0;
050        gc.anchor = GridBagConstraints.FIRST_LINE_START;
051        contentPanel.add(new JLabel(tr("Search the OSM API for notes containing words:")), gc);
052        gc.gridy = 1;
053        contentPanel.add(searchTermBox, gc);
054
055        ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Search for notes"),
056                new String[] {tr("Search for notes"), tr("Cancel")});
057        ed.setContent(contentPanel);
058        ed.setButtonIcons(new String[] {"note_search", "cancel"});
059        ed.showDialog();
060        if (ed.getValue() != 1) {
061            return;
062        }
063
064        String searchTerm = searchTermBox.getText();
065        if (searchTerm == null || searchTerm.trim().isEmpty()) {
066            Notification notification = new Notification(tr("You must enter a search term"));
067            notification.setIcon(JOptionPane.WARNING_MESSAGE);
068            notification.show();
069            return;
070        }
071
072        searchTermBox.addCurrentItemToHistory();
073        Main.pref.putCollection(HISTORY_KEY, searchTermBox.getHistory());
074
075        searchTerm = searchTerm.trim();
076        int noteLimit = Main.pref.getInteger("osm.notes.downloadLimit", 1000);
077        int closedLimit = Main.pref.getInteger("osm.notes.daysCloased", 7);
078
079        StringBuilder sb = new StringBuilder();
080        sb.append(OsmApi.getOsmApi().getBaseUrl());
081        sb.append("notes/search?limit=");
082        sb.append(noteLimit);
083        sb.append("&closed=");
084        sb.append(closedLimit);
085        sb.append("&q=");
086        try {
087            sb.append(URLEncoder.encode(searchTerm, "UTF-8"));
088        } catch (UnsupportedEncodingException ex) {
089            Main.error(ex, true); // thrown if UTF-8 isn't supported which seems unlikely.
090            return;
091        }
092
093        new DownloadNotesTask().loadUrl(false, sb.toString(), null);
094    }
095}