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

Last change on this file since 16509 was 16509, checked in by GerdP, 4 years ago

see #19296: Actions should avoid to install listeners which are not needed

  • either don't call installAdapters() or overwrite listenToSelectionChange()
  • partly reverts previous changes so that installAdapters() is not overwritten

One has to be careful because installAdapters() also calls initEnabledState()

  • Property svn:eol-style set to native
File size: 3.8 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.Optional;
11
12import javax.swing.JLabel;
13import javax.swing.JOptionPane;
14import javax.swing.JPanel;
15
16import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
17import org.openstreetmap.josm.actions.downloadtasks.DownloadParams;
18import org.openstreetmap.josm.gui.ExtendedDialog;
19import org.openstreetmap.josm.gui.MainApplication;
20import org.openstreetmap.josm.gui.Notification;
21import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
22import org.openstreetmap.josm.io.OsmApi;
23import org.openstreetmap.josm.spi.preferences.Config;
24import org.openstreetmap.josm.tools.Logging;
25import org.openstreetmap.josm.tools.Utils;
26
27/**
28 * Action to use the Notes search API to download all notes matching a given search term.
29 * @since 8071
30 */
31public class SearchNotesDownloadAction extends JosmAction {
32
33 private static final String HISTORY_KEY = "osm.notes.searchHistory";
34
35 /** Constructs a new note search action */
36 public SearchNotesDownloadAction() {
37 super(tr("Search Notes..."), "note_search", tr("Download notes from the note search API"), null, false, false);
38 }
39
40 @Override
41 public void actionPerformed(ActionEvent e) {
42 HistoryComboBox searchTermBox = new HistoryComboBox();
43 searchTermBox.setPossibleItemsTopDown(Config.getPref().getList(HISTORY_KEY, Collections.emptyList()));
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(MainApplication.getMainFrame(), tr("Search for notes"), tr("Search for notes"), tr("Cancel"))
55 .setContent(contentPanel)
56 .setButtonIcons("note_search", "cancel");
57 ed.configureContextsensitiveHelp("/Action/SearchNotesDownload", true /* show help button */);
58 if (ed.showDialog().getValue() != 1) {
59 return;
60 }
61
62 String searchTerm = Optional.ofNullable(searchTermBox.getText()).orElse("").trim();
63 if (searchTerm.isEmpty()) {
64 new Notification(tr("You must enter a search term"))
65 .setIcon(JOptionPane.WARNING_MESSAGE)
66 .show();
67 return;
68 }
69
70 searchTermBox.addCurrentItemToHistory();
71 Config.getPref().putList(HISTORY_KEY, searchTermBox.getHistory());
72
73 performSearch(searchTerm);
74 }
75
76 /**
77 * Perform search.
78 * @param searchTerm search term
79 */
80 public void performSearch(String searchTerm) {
81
82 String trimmedSearchTerm = searchTerm.trim();
83
84 try {
85 final long id = Long.parseLong(trimmedSearchTerm);
86 new DownloadNotesTask().download(id, null);
87 return;
88 } catch (NumberFormatException ignore) {
89 Logging.trace(ignore);
90 }
91
92 int noteLimit = Config.getPref().getInt("osm.notes.downloadLimit", 1000);
93 int closedLimit = Config.getPref().getInt("osm.notes.daysClosed", 7);
94
95 StringBuilder sb = new StringBuilder(128);
96 sb.append(OsmApi.getOsmApi().getBaseUrl())
97 .append("notes/search?limit=")
98 .append(noteLimit)
99 .append("&closed=")
100 .append(closedLimit)
101 .append("&q=")
102 .append(Utils.encodeUrl(trimmedSearchTerm));
103
104 new DownloadNotesTask().loadUrl(new DownloadParams(), sb.toString(), null);
105 }
106}
Note: See TracBrowser for help on using the repository browser.