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.actions.downloadtasks.DownloadNotesTask; 019import org.openstreetmap.josm.actions.downloadtasks.DownloadParams; 020import org.openstreetmap.josm.gui.ExtendedDialog; 021import org.openstreetmap.josm.gui.MainApplication; 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(MainApplication.getMainFrame(), tr("Search for notes"), tr("Search for notes"), tr("Cancel")) 059 .setContent(contentPanel) 060 .setButtonIcons("note_search", "cancel"); 061 ed.configureContextsensitiveHelp("/Action/SearchNotesDownload", true /* show help button */); 062 if (ed.showDialog().getValue() != 1) { 063 return; 064 } 065 066 String searchTerm = Optional.ofNullable(searchTermBox.getText()).orElse("").trim(); 067 if (searchTerm.isEmpty()) { 068 new Notification(tr("You must enter a search term")) 069 .setIcon(JOptionPane.WARNING_MESSAGE) 070 .show(); 071 return; 072 } 073 074 searchTermBox.addCurrentItemToHistory(); 075 Config.getPref().putList(HISTORY_KEY, searchTermBox.getHistory()); 076 077 performSearch(searchTerm); 078 } 079 080 /** 081 * Perform search. 082 * @param searchTerm search term 083 */ 084 public void performSearch(String searchTerm) { 085 086 String trimmedSearchTerm = searchTerm.trim(); 087 088 try { 089 final long id = Long.parseLong(trimmedSearchTerm); 090 new DownloadNotesTask().download(id, null); 091 return; 092 } catch (NumberFormatException ignore) { 093 Logging.trace(ignore); 094 } 095 096 int noteLimit = Config.getPref().getInt("osm.notes.downloadLimit", 1000); 097 int closedLimit = Config.getPref().getInt("osm.notes.daysClosed", 7); 098 099 StringBuilder sb = new StringBuilder(128); 100 sb.append(OsmApi.getOsmApi().getBaseUrl()) 101 .append("notes/search?limit=") 102 .append(noteLimit) 103 .append("&closed=") 104 .append(closedLimit) 105 .append("&q=") 106 .append(Utils.encodeUrl(trimmedSearchTerm)); 107 108 new DownloadNotesTask().loadUrl(new DownloadParams(), sb.toString(), null); 109 } 110}