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.util.Collections;
|
---|
10 | import java.util.LinkedList;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
|
---|
19 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
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.tools.Utils;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Action to use the Notes search API to download all notes matching a given search term.
|
---|
27 | * @since 8071
|
---|
28 | */
|
---|
29 | public class SearchNotesDownloadAction extends JosmAction {
|
---|
30 |
|
---|
31 | private static final String HISTORY_KEY = "osm.notes.searchHistory";
|
---|
32 |
|
---|
33 | /** Constructs a new note search action */
|
---|
34 | public SearchNotesDownloadAction() {
|
---|
35 | super(tr("Search Notes..."), "note_search", tr("Download notes from the note search API"), null, false);
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public void actionPerformed(ActionEvent e) {
|
---|
40 | HistoryComboBox searchTermBox = new HistoryComboBox();
|
---|
41 | List<String> searchHistory = new LinkedList<>(Main.pref.getCollection(HISTORY_KEY, new LinkedList<String>()));
|
---|
42 | Collections.reverse(searchHistory);
|
---|
43 | searchTermBox.setPossibleItems(searchHistory);
|
---|
44 |
|
---|
45 | JPanel contentPanel = new JPanel(new GridBagLayout());
|
---|
46 | GridBagConstraints gc = new GridBagConstraints();
|
---|
47 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
48 | gc.weightx = 1.0;
|
---|
49 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
50 | contentPanel.add(new JLabel(tr("Search the OSM API for notes containing words:")), gc);
|
---|
51 | gc.gridy = 1;
|
---|
52 | contentPanel.add(searchTermBox, gc);
|
---|
53 |
|
---|
54 | ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Search for notes"),
|
---|
55 | new String[] {tr("Search for notes"), tr("Cancel")});
|
---|
56 | ed.setContent(contentPanel);
|
---|
57 | ed.setButtonIcons(new String[] {"note_search", "cancel"});
|
---|
58 | ed.showDialog();
|
---|
59 | if (ed.getValue() != 1) {
|
---|
60 | return;
|
---|
61 | }
|
---|
62 |
|
---|
63 | String searchTerm = searchTermBox.getText();
|
---|
64 | if (searchTerm == null || searchTerm.trim().isEmpty()) {
|
---|
65 | Notification notification = new Notification(tr("You must enter a search term"));
|
---|
66 | notification.setIcon(JOptionPane.WARNING_MESSAGE);
|
---|
67 | notification.show();
|
---|
68 | return;
|
---|
69 | }
|
---|
70 |
|
---|
71 | searchTermBox.addCurrentItemToHistory();
|
---|
72 | Main.pref.putCollection(HISTORY_KEY, searchTermBox.getHistory());
|
---|
73 |
|
---|
74 | performSearch(searchTerm);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public void performSearch(String searchTerm) {
|
---|
78 |
|
---|
79 | searchTerm = searchTerm.trim();
|
---|
80 |
|
---|
81 | try {
|
---|
82 | final long id = Long.parseLong(searchTerm);
|
---|
83 | new DownloadNotesTask().download(id, null);
|
---|
84 | return;
|
---|
85 | } catch (NumberFormatException ignore) {
|
---|
86 | if (Main.isTraceEnabled()) {
|
---|
87 | Main.trace(ignore.getMessage());
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | int noteLimit = Main.pref.getInteger("osm.notes.downloadLimit", 1000);
|
---|
92 | int closedLimit = Main.pref.getInteger("osm.notes.daysCloased", 7);
|
---|
93 |
|
---|
94 | StringBuilder sb = new StringBuilder();
|
---|
95 | sb.append(OsmApi.getOsmApi().getBaseUrl())
|
---|
96 | .append("notes/search?limit=")
|
---|
97 | .append(noteLimit)
|
---|
98 | .append("&closed=")
|
---|
99 | .append(closedLimit)
|
---|
100 | .append("&q=")
|
---|
101 | .append(Utils.encodeUrl(searchTerm));
|
---|
102 |
|
---|
103 | new DownloadNotesTask().loadUrl(false, sb.toString(), null);
|
---|
104 | }
|
---|
105 | }
|
---|