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

Last change on this file since 14182 was 14153, checked in by Don-vip, 6 years ago

see #15229 - deprecate Main.parent and Main itself

  • Property svn:eol-style set to native
File size: 3.9 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.util.Collections;
10import java.util.LinkedList;
11import java.util.List;
12import java.util.Optional;
13
14import javax.swing.JLabel;
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17
18import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
19import org.openstreetmap.josm.actions.downloadtasks.DownloadParams;
20import org.openstreetmap.josm.gui.ExtendedDialog;
21import org.openstreetmap.josm.gui.MainApplication;
22import org.openstreetmap.josm.gui.Notification;
23import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
24import org.openstreetmap.josm.io.OsmApi;
25import org.openstreetmap.josm.spi.preferences.Config;
26import org.openstreetmap.josm.tools.Logging;
27import org.openstreetmap.josm.tools.Utils;
28
29/**
30 * Action to use the Notes search API to download all notes matching a given search term.
31 * @since 8071
32 */
33public class SearchNotesDownloadAction extends JosmAction {
34
35 private static final String HISTORY_KEY = "osm.notes.searchHistory";
36
37 /** Constructs a new note search action */
38 public SearchNotesDownloadAction() {
39 super(tr("Search Notes..."), "note_search", tr("Download notes from the note search API"), null, false);
40 }
41
42 @Override
43 public void actionPerformed(ActionEvent e) {
44 HistoryComboBox searchTermBox = new HistoryComboBox();
45 List<String> searchHistory = new LinkedList<>(Config.getPref().getList(HISTORY_KEY, new LinkedList<String>()));
46 Collections.reverse(searchHistory);
47 searchTermBox.setPossibleItems(searchHistory);
48
49 JPanel contentPanel = new JPanel(new GridBagLayout());
50 GridBagConstraints gc = new GridBagConstraints();
51 gc.fill = GridBagConstraints.HORIZONTAL;
52 gc.weightx = 1.0;
53 gc.anchor = GridBagConstraints.FIRST_LINE_START;
54 contentPanel.add(new JLabel(tr("Search the OSM API for notes containing words:")), gc);
55 gc.gridy = 1;
56 contentPanel.add(searchTermBox, gc);
57
58 ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(), tr("Search for notes"), tr("Search for notes"), tr("Cancel"))
59 .setContent(contentPanel)
60 .setButtonIcons("note_search", "cancel");
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 Config.getPref().putList(HISTORY_KEY, searchTermBox.getHistory());
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.