Ticket #10960: note_search.patch

File note_search.patch, 5.6 KB (added by ToeBee, 9 years ago)
  • src/org/openstreetmap/josm/actions/SearchNotesDownloadAction.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.actions;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import java.awt.GridBagConstraints;
     7import java.awt.GridBagLayout;
     8import java.awt.event.ActionEvent;
     9import java.io.UnsupportedEncodingException;
     10import java.net.URLEncoder;
     11import java.util.Collections;
     12import java.util.LinkedList;
     13import java.util.List;
     14
     15import javax.swing.JLabel;
     16import javax.swing.JOptionPane;
     17import javax.swing.JPanel;
     18
     19import org.openstreetmap.josm.Main;
     20import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
     21import org.openstreetmap.josm.gui.ExtendedDialog;
     22import org.openstreetmap.josm.gui.Notification;
     23import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
     24import org.openstreetmap.josm.io.OsmApi;
     25
     26/**
     27 * Action to use the Notes search API to download all
     28 * notes matching a given search term
     29 */
     30public class SearchNotesDownloadAction extends JosmAction {
     31
     32    private static final String HISTORY_KEY = "osm.notes.searchHistory";
     33
     34    /** Constructs a new note search action */
     35    public SearchNotesDownloadAction() {
     36        super(tr("Search Notes..."), "note_search", tr("Download notes from the note search API"), null, false);
     37    }
     38
     39    @Override
     40    public void actionPerformed(ActionEvent e) {
     41        Main.debug("searching for notes");
     42
     43        HistoryComboBox searchTermBox = new HistoryComboBox();
     44        List<String> searchHistory = new LinkedList<>(Main.pref.getCollection(HISTORY_KEY, new LinkedList<String>()));
     45        Collections.reverse(searchHistory);
     46        searchTermBox.setPossibleItems(searchHistory);
     47
     48        JPanel contentPanel = new JPanel(new GridBagLayout());
     49        GridBagConstraints gc = new GridBagConstraints();
     50        gc.fill = GridBagConstraints.HORIZONTAL;
     51        gc.weightx = 1.0;
     52        gc.anchor = GridBagConstraints.FIRST_LINE_START;
     53        contentPanel.add(new JLabel(tr("Search the OSM API for notes containing words:")), gc);
     54        gc.gridy = 1;
     55        contentPanel.add(searchTermBox, gc);
     56
     57        ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Search for notes"), new String[] {tr("Search for notes"), tr("Cancel")});
     58        ed.setContent(contentPanel);
     59        ed.setButtonIcons(new String[] {"note_search.png", "cancel.png"});
     60        ed.showDialog();
     61        if (ed.getValue() != 1) {
     62            return;
     63        }
     64
     65        String searchTerm = searchTermBox.getText();
     66        if (searchTerm == null || searchTerm.trim().isEmpty()) {
     67            Notification notification = new Notification(tr("You must enter a search term"));
     68            notification.setIcon(JOptionPane.WARNING_MESSAGE);
     69            notification.show();
     70            return;
     71        }
     72
     73        searchTermBox.addCurrentItemToHistory();
     74        Main.pref.putCollection(HISTORY_KEY, searchTermBox.getHistory());
     75
     76        searchTerm = searchTerm.trim();
     77        int noteLimit = Main.pref.getInteger("osm.notes.downloadLimit", 1000);
     78        int closedLimit = Main.pref.getInteger("osm.notes.daysCloased", 7);
     79
     80        StringBuilder sb = new StringBuilder();
     81        sb.append(OsmApi.getOsmApi().getBaseUrl());
     82        sb.append("notes/search?limit=");
     83        sb.append(noteLimit);
     84        sb.append("&closed=");
     85        sb.append(closedLimit);
     86        sb.append("&q=");
     87        try {
     88            sb.append(URLEncoder.encode(searchTerm, "UTF-8"));
     89        } catch (UnsupportedEncodingException ex) {
     90            Main.error(ex, true); //thrown if UTF-8 isn't supported which seems unlikely.
     91            return;
     92        }
     93
     94        DownloadNotesTask task = new DownloadNotesTask();
     95        task.loadUrl(false, sb.toString(), null);
     96    }
     97}
  • src/org/openstreetmap/josm/gui/MainMenu.java

     
    7777import org.openstreetmap.josm.actions.ReverseWayAction;
    7878import org.openstreetmap.josm.actions.SaveAction;
    7979import org.openstreetmap.josm.actions.SaveAsAction;
     80import org.openstreetmap.josm.actions.SearchNotesDownloadAction;
    8081import org.openstreetmap.josm.actions.SelectAllAction;
    8182import org.openstreetmap.josm.actions.SelectNonBranchingWaySequencesAction;
    8283import org.openstreetmap.josm.actions.SessionLoadAction;
     
    151152    public final DownloadAction download = new DownloadAction();
    152153    /** File / Download object... **/
    153154    public final DownloadPrimitiveAction downloadPrimitive = new DownloadPrimitiveAction();
     155    /** File / Search Notes... **/
     156    public final SearchNotesDownloadAction searchNotes = new SearchNotesDownloadAction();
    154157    /** File / Download parent ways/relations... **/
    155158    public final DownloadReferrersAction downloadReferrers = new DownloadReferrersAction();
    156159    /** File / Close open changesets... **/
     
    614617        fileMenu.addSeparator();
    615618        add(fileMenu, download);
    616619        add(fileMenu, downloadPrimitive);
     620        //TODO: Remove conditional when notes are un-hidden
     621        if (Main.pref.getBoolean("osm.notes.enableDownload", false)) {
     622            add(fileMenu, searchNotes);
     623        }
    617624        add(fileMenu, downloadReferrers);
    618625        add(fileMenu, update);
    619626        add(fileMenu, updateSelection);