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

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

sonar - squid:S3878 - Arrays should not be created for varargs parameters

  • 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.Utils;
25
26/**
27 * Action to use the Notes search API to download all notes matching a given search term.
28 * @since 8071
29 */
30public class SearchNotesDownloadAction extends JosmAction {
31
32 private static final String HISTORY_KEY = "osm.notes.searchHistory";
33
34 /** Constructs a new note search action */
35 public SearchNotesDownloadAction() {
36 super(tr("Search Notes..."), "note_search", tr("Download notes from the note search API"), null, false);
37 }
38
39 @Override
40 public void actionPerformed(ActionEvent e) {
41 HistoryComboBox searchTermBox = new HistoryComboBox();
42 List<String> searchHistory = new LinkedList<>(Main.pref.getCollection(HISTORY_KEY, new LinkedList<String>()));
43 Collections.reverse(searchHistory);
44 searchTermBox.setPossibleItems(searchHistory);
45
46 JPanel contentPanel = new JPanel(new GridBagLayout());
47 GridBagConstraints gc = new GridBagConstraints();
48 gc.fill = GridBagConstraints.HORIZONTAL;
49 gc.weightx = 1.0;
50 gc.anchor = GridBagConstraints.FIRST_LINE_START;
51 contentPanel.add(new JLabel(tr("Search the OSM API for notes containing words:")), gc);
52 gc.gridy = 1;
53 contentPanel.add(searchTermBox, gc);
54
55 ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Search for notes"), tr("Search for notes"), tr("Cancel"))
56 .setContent(contentPanel)
57 .setButtonIcons("note_search", "cancel");
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 Main.pref.putCollection(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 Main.trace(ignore);
90 }
91
92 int noteLimit = Main.pref.getInteger("osm.notes.downloadLimit", 1000);
93 int closedLimit = Main.pref.getInteger("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(false, sb.toString(), null);
105 }
106}
Note: See TracBrowser for help on using the repository browser.