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

Last change on this file since 14693 was 14656, checked in by simon04, 5 years ago

see #17178 - Search notes: add help button

  • 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.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 ed.configureContextsensitiveHelp("/Action/SearchNotesDownload", true /* show help button */);
62 if (ed.showDialog().getValue() != 1) {
63 return;
64 }
65
66 String searchTerm = Optional.ofNullable(searchTermBox.getText()).orElse("").trim();
67 if (searchTerm.isEmpty()) {
68 new Notification(tr("You must enter a search term"))
69 .setIcon(JOptionPane.WARNING_MESSAGE)
70 .show();
71 return;
72 }
73
74 searchTermBox.addCurrentItemToHistory();
75 Config.getPref().putList(HISTORY_KEY, searchTermBox.getHistory());
76
77 performSearch(searchTerm);
78 }
79
80 /**
81 * Perform search.
82 * @param searchTerm search term
83 */
84 public void performSearch(String searchTerm) {
85
86 String trimmedSearchTerm = searchTerm.trim();
87
88 try {
89 final long id = Long.parseLong(trimmedSearchTerm);
90 new DownloadNotesTask().download(id, null);
91 return;
92 } catch (NumberFormatException ignore) {
93 Logging.trace(ignore);
94 }
95
96 int noteLimit = Config.getPref().getInt("osm.notes.downloadLimit", 1000);
97 int closedLimit = Config.getPref().getInt("osm.notes.daysClosed", 7);
98
99 StringBuilder sb = new StringBuilder(128);
100 sb.append(OsmApi.getOsmApi().getBaseUrl())
101 .append("notes/search?limit=")
102 .append(noteLimit)
103 .append("&closed=")
104 .append(closedLimit)
105 .append("&q=")
106 .append(Utils.encodeUrl(trimmedSearchTerm));
107
108 new DownloadNotesTask().loadUrl(new DownloadParams(), sb.toString(), null);
109 }
110}
Note: See TracBrowser for help on using the repository browser.