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 public void performSearch(String searchTerm) { 078 079 searchTerm = searchTerm.trim(); 080 081 try { 082 final long id = Long.parseLong(searchTerm); 083 new DownloadNotesTask().download(id, null); 084 return; 085 } catch (NumberFormatException ignore) { 086 if (Main.isTraceEnabled()) { 087 Main.trace(ignore.getMessage()); 088 } 089 } 090 091 int noteLimit = Main.pref.getInteger("osm.notes.downloadLimit", 1000); 092 int closedLimit = Main.pref.getInteger("osm.notes.daysCloased", 7); 093 094 StringBuilder sb = new StringBuilder(); 095 sb.append(OsmApi.getOsmApi().getBaseUrl()) 096 .append("notes/search?limit=") 097 .append(noteLimit) 098 .append("&closed=") 099 .append(closedLimit) 100 .append("&q=") 101 .append(Utils.encodeUrl(searchTerm)); 102 103 new DownloadNotesTask().loadUrl(false, sb.toString(), null); 104 } 105}