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

Last change on this file since 12711 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 3.7 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.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);
38 }
39
40 @Override
41 public void actionPerformed(ActionEvent e) {
42 HistoryComboBox searchTermBox = new HistoryComboBox();
43 List<String> searchHistory = new LinkedList<>(Main.pref.getCollection(HISTORY_KEY, new LinkedList<String>()));
44 Collections.reverse(searchHistory);
45 searchTermBox.setPossibleItems(searchHistory);
46
47 JPanel contentPanel = new JPanel(new GridBagLayout());
48 GridBagConstraints gc = new GridBagConstraints();
49 gc.fill = GridBagConstraints.HORIZONTAL;
50 gc.weightx = 1.0;
51 gc.anchor = GridBagConstraints.FIRST_LINE_START;
52 contentPanel.add(new JLabel(tr("Search the OSM API for notes containing words:")), gc);
53 gc.gridy = 1;
54 contentPanel.add(searchTermBox, gc);
55
56 ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Search for notes"), tr("Search for notes"), tr("Cancel"))
57 .setContent(contentPanel)
58 .setButtonIcons("note_search", "cancel");
59 if (ed.showDialog().getValue() != 1) {
60 return;
61 }
62
63 String searchTerm = Optional.ofNullable(searchTermBox.getText()).orElse("").trim();
64 if (searchTerm.isEmpty()) {
65 new Notification(tr("You must enter a search term"))
66 .setIcon(JOptionPane.WARNING_MESSAGE)
67 .show();
68 return;
69 }
70
71 searchTermBox.addCurrentItemToHistory();
72 Main.pref.putCollection(HISTORY_KEY, searchTermBox.getHistory());
73
74 performSearch(searchTerm);
75 }
76
77 /**
78 * Perform search.
79 * @param searchTerm search term
80 */
81 public void performSearch(String searchTerm) {
82
83 String trimmedSearchTerm = searchTerm.trim();
84
85 try {
86 final long id = Long.parseLong(trimmedSearchTerm);
87 new DownloadNotesTask().download(id, null);
88 return;
89 } catch (NumberFormatException ignore) {
90 Logging.trace(ignore);
91 }
92
93 int noteLimit = Main.pref.getInteger("osm.notes.downloadLimit", 1000);
94 int closedLimit = Main.pref.getInteger("osm.notes.daysClosed", 7);
95
96 StringBuilder sb = new StringBuilder(128);
97 sb.append(OsmApi.getOsmApi().getBaseUrl())
98 .append("notes/search?limit=")
99 .append(noteLimit)
100 .append("&closed=")
101 .append(closedLimit)
102 .append("&q=")
103 .append(Utils.encodeUrl(trimmedSearchTerm));
104
105 new DownloadNotesTask().loadUrl(false, sb.toString(), null);
106 }
107}
Note: See TracBrowser for help on using the repository browser.