1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagConstraints;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.util.Optional;
|
---|
11 |
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.JPanel;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
|
---|
17 | import org.openstreetmap.josm.actions.downloadtasks.DownloadParams;
|
---|
18 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
19 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
20 | import org.openstreetmap.josm.gui.Notification;
|
---|
21 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
22 | import org.openstreetmap.josm.io.OsmApi;
|
---|
23 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
24 | import org.openstreetmap.josm.tools.Logging;
|
---|
25 | import org.openstreetmap.josm.tools.Utils;
|
---|
26 | import 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 | */
|
---|
32 | public 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 | }
|
---|