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