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

Last change on this file since 12911 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

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