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

Last change on this file since 9447 was 8855, checked in by Don-vip, 9 years ago

sonar - Unused private method should be removed
sonar - Unused protected methods should be removed
sonar - Sections of code should not be "commented out"
sonar - Empty statements should be removed
sonar - squid:S1172 - Unused method parameters should be removed
sonar - squid:S1481 - Unused local variables should be removed

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