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.Main; 019import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask; 020import org.openstreetmap.josm.gui.ExtendedDialog; 021import org.openstreetmap.josm.gui.Notification; 022import org.openstreetmap.josm.gui.widgets.HistoryComboBox; 023import org.openstreetmap.josm.io.OsmApi; 024import org.openstreetmap.josm.spi.preferences.Config; 025import org.openstreetmap.josm.tools.Logging; 026import org.openstreetmap.josm.tools.Utils; 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"), null, false); 039 } 040 041 @Override 042 public void actionPerformed(ActionEvent e) { 043 HistoryComboBox searchTermBox = new HistoryComboBox(); 044 List<String> searchHistory = new LinkedList<>(Config.getPref().getList(HISTORY_KEY, new LinkedList<String>())); 045 Collections.reverse(searchHistory); 046 searchTermBox.setPossibleItems(searchHistory); 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(Main.parent, tr("Search for notes"), tr("Search for notes"), tr("Cancel")) 058 .setContent(contentPanel) 059 .setButtonIcons("note_search", "cancel"); 060 if (ed.showDialog().getValue() != 1) { 061 return; 062 } 063 064 String searchTerm = Optional.ofNullable(searchTermBox.getText()).orElse("").trim(); 065 if (searchTerm.isEmpty()) { 066 new Notification(tr("You must enter a search term")) 067 .setIcon(JOptionPane.WARNING_MESSAGE) 068 .show(); 069 return; 070 } 071 072 searchTermBox.addCurrentItemToHistory(); 073 Config.getPref().putList(HISTORY_KEY, searchTermBox.getHistory()); 074 075 performSearch(searchTerm); 076 } 077 078 /** 079 * Perform search. 080 * @param searchTerm search term 081 */ 082 public void performSearch(String searchTerm) { 083 084 String trimmedSearchTerm = searchTerm.trim(); 085 086 try { 087 final long id = Long.parseLong(trimmedSearchTerm); 088 new DownloadNotesTask().download(id, null); 089 return; 090 } catch (NumberFormatException ignore) { 091 Logging.trace(ignore); 092 } 093 094 int noteLimit = Config.getPref().getInt("osm.notes.downloadLimit", 1000); 095 int closedLimit = Config.getPref().getInt("osm.notes.daysClosed", 7); 096 097 StringBuilder sb = new StringBuilder(128); 098 sb.append(OsmApi.getOsmApi().getBaseUrl()) 099 .append("notes/search?limit=") 100 .append(noteLimit) 101 .append("&closed=") 102 .append(closedLimit) 103 .append("&q=") 104 .append(Utils.encodeUrl(trimmedSearchTerm)); 105 106 new DownloadNotesTask().loadUrl(false, sb.toString(), null); 107 } 108}