source: josm/trunk/src/org/openstreetmap/josm/actions/SearchNotesDownloadAction.java

Last change on this file was 18173, checked in by Don-vip, 3 years ago

fix #20690 - fix #21240 - Refactoring of UploadDialog, HistoryComboBox and AutoCompletingComboBox (patch by marcello)

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
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.awt.event.KeyEvent;
10import java.util.Optional;
11
12import javax.swing.JLabel;
13import javax.swing.JOptionPane;
14import javax.swing.JPanel;
15
16import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
17import org.openstreetmap.josm.actions.downloadtasks.DownloadParams;
18import org.openstreetmap.josm.gui.ExtendedDialog;
19import org.openstreetmap.josm.gui.MainApplication;
20import org.openstreetmap.josm.gui.Notification;
21import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
22import org.openstreetmap.josm.io.OsmApi;
23import org.openstreetmap.josm.spi.preferences.Config;
24import org.openstreetmap.josm.tools.Logging;
25import org.openstreetmap.josm.tools.Utils;
26import org.openstreetmap.josm.tools.Shortcut;
27
28/**
29 * Action to use the Notes search API to download all notes matching a given search term.
30 * @since 8071
31 */
32public class SearchNotesDownloadAction extends JosmAction {
33
34 private static final String HISTORY_KEY = "osm.notes.searchHistory";
35
36 /** Constructs a new note search action */
37 public SearchNotesDownloadAction() {
38 super(tr("Search Notes..."), "note_search", tr("Download notes from the note search API"),
39 Shortcut.registerShortcut("file:notesearch",
40 tr("File: {0}", tr("Search Notes...")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false, false);
41 }
42
43 @Override
44 public void actionPerformed(ActionEvent e) {
45 HistoryComboBox searchTermBox = new HistoryComboBox();
46 searchTermBox.getModel().prefs().load(HISTORY_KEY);
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(MainApplication.getMainFrame(), tr("Search for notes"), tr("Search for notes"), tr("Cancel"))
58 .setContent(contentPanel)
59 .setButtonIcons("note_search", "cancel");
60 ed.configureContextsensitiveHelp("/Action/SearchNotesDownload", true /* show help button */);
61 if (ed.showDialog().getValue() != 1) {
62 return;
63 }
64
65 String searchTerm = Optional.ofNullable(searchTermBox.getText()).orElse("").trim();
66 if (searchTerm.isEmpty()) {
67 new Notification(tr("You must enter a search term"))
68 .setIcon(JOptionPane.WARNING_MESSAGE)
69 .show();
70 return;
71 }
72
73 searchTermBox.addCurrentItemToHistory();
74 searchTermBox.getModel().prefs().save(HISTORY_KEY);
75
76 performSearch(searchTerm);
77 }
78
79 /**
80 * Perform search.
81 * @param searchTerm search term
82 */
83 public void performSearch(String searchTerm) {
84
85 String trimmedSearchTerm = searchTerm.trim();
86
87 try {
88 final long id = Long.parseLong(trimmedSearchTerm);
89 new DownloadNotesTask().download(id, null);
90 return;
91 } catch (NumberFormatException ignore) {
92 Logging.trace(ignore);
93 }
94
95 int noteLimit = Config.getPref().getInt("osm.notes.downloadLimit", 1000);
96 int closedLimit = Config.getPref().getInt("osm.notes.daysClosed", 7);
97
98 StringBuilder sb = new StringBuilder(128);
99 sb.append(OsmApi.getOsmApi().getBaseUrl())
100 .append("notes/search?limit=")
101 .append(noteLimit)
102 .append("&closed=")
103 .append(closedLimit)
104 .append("&q=")
105 .append(Utils.encodeUrl(trimmedSearchTerm));
106
107 new DownloadNotesTask().loadUrl(new DownloadParams(), sb.toString(), null);
108 }
109}
Note: See TracBrowser for help on using the repository browser.