| 1 | // License: GPL. For details, see LICENSE file. |
| 2 | package org.openstreetmap.josm.gui; |
| 3 | |
| 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
| 5 | |
| 6 | import java.awt.BorderLayout; |
| 7 | import java.awt.Dimension; |
| 8 | import java.awt.GridBagLayout; |
| 9 | import java.awt.event.ActionEvent; |
| 10 | |
| 11 | import javax.swing.AbstractAction; |
| 12 | import javax.swing.JButton; |
| 13 | import javax.swing.JComboBox; |
| 14 | import javax.swing.JDialog; |
| 15 | import javax.swing.JLabel; |
| 16 | import javax.swing.JPanel; |
| 17 | import javax.swing.JSpinner; |
| 18 | import javax.swing.JTabbedPane; |
| 19 | import javax.swing.SpinnerNumberModel; |
| 20 | |
| 21 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask; |
| 22 | import org.openstreetmap.josm.actions.downloadtasks.DownloadParams; |
| 23 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction; |
| 24 | import org.openstreetmap.josm.gui.help.HelpUtil; |
| 25 | import org.openstreetmap.josm.gui.util.WindowGeometry; |
| 26 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox; |
| 27 | import org.openstreetmap.josm.gui.widgets.JosmTextArea; |
| 28 | import org.openstreetmap.josm.io.OsmApi; |
| 29 | import org.openstreetmap.josm.spi.preferences.Config; |
| 30 | import org.openstreetmap.josm.tools.GBC; |
| 31 | import org.openstreetmap.josm.tools.ImageProvider; |
| 32 | import org.openstreetmap.josm.tools.InputMapUtils; |
| 33 | import org.openstreetmap.josm.tools.Logging; |
| 34 | import org.openstreetmap.josm.tools.Utils; |
| 35 | |
| 36 | /** |
| 37 | * Dialog displayed to the user to download note data. |
| 38 | */ |
| 39 | public class NoteDownloadDialog extends JDialog { |
| 40 | |
| 41 | protected HistoryComboBox fromBox; |
| 42 | protected HistoryComboBox queryBox; |
| 43 | protected HistoryComboBox toBox; |
| 44 | protected HistoryComboBox useridBox; |
| 45 | protected HistoryComboBox usernameBox; |
| 46 | protected JComboBox orderBox; |
| 47 | protected JComboBox sortBox; |
| 48 | protected JosmTextArea idText; |
| 49 | protected JSpinner closedSpinner; |
| 50 | protected JSpinner limitSpinner; |
| 51 | protected JTabbedPane pane; |
| 52 | |
| 53 | /** |
| 54 | * Constructs a new {@code NoteDownloadDialog}. |
| 55 | */ |
| 56 | public NoteDownloadDialog() { |
| 57 | super(MainApplication.getMainFrame(), tr("Download Notes"), ModalityType.DOCUMENT_MODAL); |
| 58 | |
| 59 | new WindowGeometry( |
| 60 | getClass().getName() + ".geometry", |
| 61 | WindowGeometry.centerInWindow( |
| 62 | getParent(), |
| 63 | new Dimension(500, 300) |
| 64 | ) |
| 65 | ).applySafe(this); |
| 66 | |
| 67 | JPanel searchPanel = new JPanel(); |
| 68 | searchPanel.setLayout(new GridBagLayout()); |
| 69 | |
| 70 | searchPanel.add(new JLabel(tr("Containing words:")), GBC.eol()); |
| 71 | queryBox = new HistoryComboBox(); |
| 72 | searchPanel.add(queryBox, GBC.eol().fill(GBC.HORIZONTAL)); |
| 73 | |
| 74 | searchPanel.add(new JLabel(tr("Username:")), GBC.eol()); |
| 75 | usernameBox = new HistoryComboBox(); |
| 76 | searchPanel.add(usernameBox, GBC.eol().fill(GBC.HORIZONTAL)); |
| 77 | |
| 78 | searchPanel.add(new JLabel(tr("User id:")), GBC.eol()); |
| 79 | useridBox = new HistoryComboBox(); |
| 80 | searchPanel.add(useridBox, GBC.eol().fill(GBC.HORIZONTAL)); |
| 81 | |
| 82 | searchPanel.add(new JLabel(tr("From date:")), GBC.eol()); |
| 83 | fromBox = new HistoryComboBox(); |
| 84 | searchPanel.add(fromBox, GBC.eol().fill(GBC.HORIZONTAL)); |
| 85 | |
| 86 | searchPanel.add(new JLabel(tr("To date:")), GBC.eol()); |
| 87 | toBox = new HistoryComboBox(); |
| 88 | searchPanel.add(toBox, GBC.eol().fill(GBC.HORIZONTAL)); |
| 89 | |
| 90 | //searchPanel.add(new JLabel(tr("BBox:"))); |
| 91 | searchPanel.add(GBC.glue(0, 1), GBC.eol().fill(GBC.VERTICAL)); |
| 92 | |
| 93 | searchPanel.add(new JLabel(tr("Result limit:"))); |
| 94 | int limitPref = Config.getPref().getInt("osm.notes.downloadLimit", 1000); |
| 95 | SpinnerNumberModel limitModel = new SpinnerNumberModel(limitPref, 1, 10000, 1); |
| 96 | limitSpinner = new JSpinner(limitModel); |
| 97 | searchPanel.add(limitSpinner); |
| 98 | |
| 99 | searchPanel.add(new JLabel(tr("Days closed:"))); |
| 100 | int closedPref = Config.getPref().getInt("osm.notes.daysClosed", 7); |
| 101 | SpinnerNumberModel closedModel = new SpinnerNumberModel(closedPref, -1, 10000, 1); |
| 102 | closedSpinner = new JSpinner(closedModel); |
| 103 | searchPanel.add(closedSpinner, GBC.eol()); |
| 104 | |
| 105 | searchPanel.add(new JLabel(tr("Sort by:"))); |
| 106 | String[] sortList = {tr("Modified date"), tr("Created date")}; |
| 107 | sortBox = new JComboBox<>(sortList); |
| 108 | searchPanel.add(sortBox); |
| 109 | |
| 110 | searchPanel.add(new JLabel(tr("Order:"))); |
| 111 | String[] orderList = {tr("Newest"), tr("Oldest")}; |
| 112 | orderBox = new JComboBox<>(orderList); |
| 113 | searchPanel.add(orderBox, GBC.eol()); |
| 114 | |
| 115 | JPanel idPanel = new JPanel(); |
| 116 | idPanel.setLayout(new GridBagLayout()); |
| 117 | |
| 118 | idPanel.add(new JLabel(tr("Enter one id per row:")), GBC.eol()); |
| 119 | idText = new JosmTextArea(); |
| 120 | idPanel.add(idText, GBC.eol().fill()); |
| 121 | |
| 122 | pane = new JTabbedPane(); |
| 123 | pane.addTab(tr("Search"), searchPanel); |
| 124 | pane.addTab(tr("By id"), idPanel); |
| 125 | |
| 126 | add(pane, BorderLayout.CENTER); |
| 127 | |
| 128 | JPanel buttonPanel = new JPanel(); |
| 129 | |
| 130 | DownloadAction downloadAction = new DownloadAction(); |
| 131 | buttonPanel.add(new JButton(downloadAction)); |
| 132 | |
| 133 | CancelAction cancelAction = new CancelAction(); |
| 134 | buttonPanel.add(new JButton(cancelAction)); |
| 135 | InputMapUtils.addEscapeAction(getRootPane(), cancelAction); |
| 136 | |
| 137 | buttonPanel.add(new JButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Dialog/DownloadNotes")))); |
| 138 | |
| 139 | add(buttonPanel, BorderLayout.SOUTH); |
| 140 | } |
| 141 | |
| 142 | class CancelAction extends AbstractAction { |
| 143 | CancelAction() { |
| 144 | putValue(NAME, tr("Cancel")); |
| 145 | putValue(SHORT_DESCRIPTION, tr("Cancel")); |
| 146 | new ImageProvider("cancel").getResource().attachImageIcon(this); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void actionPerformed(ActionEvent e) { |
| 151 | dispose(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | class DownloadAction extends AbstractAction { |
| 156 | DownloadAction() { |
| 157 | putValue(NAME, tr("Download")); |
| 158 | putValue(SHORT_DESCRIPTION, tr("Download")); |
| 159 | new ImageProvider("download").getResource().attachImageIcon(this); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void actionPerformed(ActionEvent e) { |
| 164 | |
| 165 | // By ID |
| 166 | if (pane.getSelectedIndex() == 1) { |
| 167 | for (String id : idText.getText().split("\n", -1)) { |
| 168 | try { |
| 169 | new DownloadNotesTask().download(Long.parseLong(id), null); |
| 170 | } catch (NumberFormatException exception) { |
| 171 | Logging.trace(exception); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | dispose(); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | // Search |
| 180 | StringBuilder sb = new StringBuilder(128); |
| 181 | sb.append(OsmApi.getOsmApi().getBaseUrl()) |
| 182 | .append("notes/search?") |
| 183 | .append("limit=") |
| 184 | .append(limitSpinner.getValue()) |
| 185 | .append("&closed=") |
| 186 | .append(closedSpinner.getValue()); |
| 187 | |
| 188 | String query = queryBox.getText(); |
| 189 | if (query.length() > 0) { |
| 190 | sb.append("&q="); |
| 191 | sb.append(Utils.encodeUrl(query)); |
| 192 | } |
| 193 | |
| 194 | String username = usernameBox.getText(); |
| 195 | if (username.length() > 0) { |
| 196 | sb.append("&display_name="); |
| 197 | sb.append(Utils.encodeUrl(username)); |
| 198 | } |
| 199 | |
| 200 | String userid = useridBox.getText(); |
| 201 | if (userid.length() > 0) { |
| 202 | sb.append("&user="); |
| 203 | sb.append(userid); |
| 204 | } |
| 205 | |
| 206 | String from = fromBox.getText(); |
| 207 | if (from.length() > 0) { |
| 208 | sb.append("&from="); |
| 209 | sb.append(from); |
| 210 | } |
| 211 | |
| 212 | String to = toBox.getText(); |
| 213 | if (to.length() > 0) { |
| 214 | sb.append("&to="); |
| 215 | sb.append(to); |
| 216 | } |
| 217 | |
| 218 | if (sortBox.getSelectedIndex() == 1) { |
| 219 | sb.append("&sort=created_at"); |
| 220 | } |
| 221 | |
| 222 | if (orderBox.getSelectedIndex() == 1) { |
| 223 | sb.append("&order=oldest"); |
| 224 | } |
| 225 | |
| 226 | new DownloadNotesTask().loadUrl(new DownloadParams(), sb.toString(), null); |
| 227 | |
| 228 | dispose(); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |