[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[1146] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[2323] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[1146] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
| 7 | import java.awt.GridBagLayout;
|
---|
[8195] | 8 | import java.awt.GridLayout;
|
---|
[1146] | 9 | import java.awt.event.ActionEvent;
|
---|
| 10 | import java.awt.event.KeyEvent;
|
---|
[4521] | 11 | import java.util.ArrayList;
|
---|
[5691] | 12 | import java.util.Collection;
|
---|
[2285] | 13 | import java.util.Collections;
|
---|
| 14 | import java.util.LinkedList;
|
---|
| 15 | import java.util.List;
|
---|
[11279] | 16 | import java.util.Objects;
|
---|
[2322] | 17 | import java.util.concurrent.Future;
|
---|
[11279] | 18 | import java.util.stream.Collectors;
|
---|
[1146] | 19 |
|
---|
| 20 | import javax.swing.JCheckBox;
|
---|
| 21 | import javax.swing.JLabel;
|
---|
[8195] | 22 | import javax.swing.JList;
|
---|
[4521] | 23 | import javax.swing.JOptionPane;
|
---|
[1146] | 24 | import javax.swing.JPanel;
|
---|
| 25 |
|
---|
| 26 | import org.openstreetmap.josm.Main;
|
---|
[4521] | 27 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
[7531] | 28 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
|
---|
[8195] | 29 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesUrlBoundsTask;
|
---|
| 30 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesUrlIdTask;
|
---|
[5361] | 31 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeCompressedTask;
|
---|
[6032] | 32 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeTask;
|
---|
[5361] | 33 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmCompressedTask;
|
---|
[8240] | 34 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmIdTask;
|
---|
[1146] | 35 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
[4996] | 36 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmUrlTask;
|
---|
[6245] | 37 | import org.openstreetmap.josm.actions.downloadtasks.DownloadSessionTask;
|
---|
[13927] | 38 | import org.openstreetmap.josm.actions.downloadtasks.DownloadParams;
|
---|
[4521] | 39 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
|
---|
[2322] | 40 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
[11279] | 41 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
[1397] | 42 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
[6032] | 43 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
[12634] | 44 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[12675] | 45 | import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
|
---|
[13261] | 46 | import org.openstreetmap.josm.gui.util.WindowGeometry;
|
---|
[2285] | 47 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
[12846] | 48 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[11279] | 49 | import org.openstreetmap.josm.tools.GBC;
|
---|
[12620] | 50 | import org.openstreetmap.josm.tools.Logging;
|
---|
[1146] | 51 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[5772] | 52 | import org.openstreetmap.josm.tools.Utils;
|
---|
[1146] | 53 |
|
---|
| 54 | /**
|
---|
| 55 | * Open an URL input dialog and load data from the given URL.
|
---|
| 56 | *
|
---|
| 57 | * @author imi
|
---|
| 58 | */
|
---|
| 59 | public class OpenLocationAction extends JosmAction {
|
---|
[11279] | 60 | /**
|
---|
| 61 | * true if the URL needs to be opened in a new layer, false otherwise
|
---|
| 62 | */
|
---|
[13261] | 63 | private static final BooleanProperty USE_NEW_LAYER = new BooleanProperty("download.location.newlayer", false);
|
---|
[11986] | 64 | /**
|
---|
[13261] | 65 | * true to zoom to entire newly downloaded data, false otherwise
|
---|
| 66 | */
|
---|
| 67 | private static final BooleanProperty DOWNLOAD_ZOOMTODATA = new BooleanProperty("download.location.zoomtodata", true);
|
---|
| 68 | /**
|
---|
[11986] | 69 | * the list of download tasks
|
---|
| 70 | */
|
---|
[8308] | 71 | protected final transient List<Class<? extends DownloadTask>> downloadTasks;
|
---|
[6069] | 72 |
|
---|
[11343] | 73 | static class WhichTasksToPerformDialog extends ExtendedDialog {
|
---|
| 74 | WhichTasksToPerformDialog(JList<DownloadTask> list) {
|
---|
| 75 | super(Main.parent, tr("Which tasks to perform?"), new String[]{tr("Ok"), tr("Cancel")}, true);
|
---|
[12279] | 76 | setButtonIcons("ok", "cancel");
|
---|
[11343] | 77 | final JPanel pane = new JPanel(new GridLayout(2, 1));
|
---|
| 78 | pane.add(new JLabel(tr("Which tasks to perform?")));
|
---|
| 79 | pane.add(list);
|
---|
| 80 | setContent(pane);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[1169] | 84 | /**
|
---|
| 85 | * Create an open action. The name is "Open a file".
|
---|
| 86 | */
|
---|
| 87 | public OpenLocationAction() {
|
---|
[4045] | 88 | /* I18N: Command to download a specific location/URL */
|
---|
[1195] | 89 | super(tr("Open Location..."), "openlocation", tr("Open an URL."),
|
---|
[7859] | 90 | Shortcut.registerShortcut("system:open_location", tr("File: {0}", tr("Open Location...")),
|
---|
| 91 | KeyEvent.VK_L, Shortcut.CTRL), true);
|
---|
[2323] | 92 | putValue("help", ht("/Action/OpenLocation"));
|
---|
[7005] | 93 | this.downloadTasks = new ArrayList<>();
|
---|
[4521] | 94 | addDownloadTaskClass(DownloadOsmTask.class);
|
---|
| 95 | addDownloadTaskClass(DownloadGpsTask.class);
|
---|
[7531] | 96 | addDownloadTaskClass(DownloadNotesTask.class);
|
---|
[4530] | 97 | addDownloadTaskClass(DownloadOsmChangeTask.class);
|
---|
[4996] | 98 | addDownloadTaskClass(DownloadOsmUrlTask.class);
|
---|
[8240] | 99 | addDownloadTaskClass(DownloadOsmIdTask.class);
|
---|
[5361] | 100 | addDownloadTaskClass(DownloadOsmCompressedTask.class);
|
---|
| 101 | addDownloadTaskClass(DownloadOsmChangeCompressedTask.class);
|
---|
[6245] | 102 | addDownloadTaskClass(DownloadSessionTask.class);
|
---|
[8195] | 103 | addDownloadTaskClass(DownloadNotesUrlBoundsTask.class);
|
---|
| 104 | addDownloadTaskClass(DownloadNotesUrlIdTask.class);
|
---|
[1169] | 105 | }
|
---|
[1146] | 106 |
|
---|
[2285] | 107 | /**
|
---|
| 108 | * Restore the current history from the preferences
|
---|
[2512] | 109 | *
|
---|
[8459] | 110 | * @param cbHistory the history combo box
|
---|
[2285] | 111 | */
|
---|
| 112 | protected void restoreUploadAddressHistory(HistoryComboBox cbHistory) {
|
---|
[12846] | 113 | List<String> cmtHistory = new LinkedList<>(Config.getPref().getList(getClass().getName() + ".uploadAddressHistory",
|
---|
[7859] | 114 | new LinkedList<String>()));
|
---|
[7434] | 115 | // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement()
|
---|
[2285] | 116 | //
|
---|
| 117 | Collections.reverse(cmtHistory);
|
---|
| 118 | cbHistory.setPossibleItems(cmtHistory);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /**
|
---|
| 122 | * Remind the current history in the preferences
|
---|
[8459] | 123 | * @param cbHistory the history combo box
|
---|
[2285] | 124 | */
|
---|
| 125 | protected void remindUploadAddressHistory(HistoryComboBox cbHistory) {
|
---|
| 126 | cbHistory.addCurrentItemToHistory();
|
---|
[12846] | 127 | Config.getPref().putList(getClass().getName() + ".uploadAddressHistory", cbHistory.getHistory());
|
---|
[2285] | 128 | }
|
---|
| 129 |
|
---|
[6084] | 130 | @Override
|
---|
[1169] | 131 | public void actionPerformed(ActionEvent e) {
|
---|
[11279] | 132 | JPanel all = new JPanel(new GridBagLayout());
|
---|
[1146] | 133 |
|
---|
[11279] | 134 | // download URL selection
|
---|
| 135 | all.add(new JLabel(tr("Enter URL to download:")), GBC.eol());
|
---|
[4310] | 136 | HistoryComboBox uploadAddresses = new HistoryComboBox();
|
---|
| 137 | uploadAddresses.setToolTipText(tr("Enter an URL from where data should be downloaded"));
|
---|
| 138 | restoreUploadAddressHistory(uploadAddresses);
|
---|
[11279] | 139 | all.add(uploadAddresses, GBC.eop().fill(GBC.BOTH));
|
---|
| 140 |
|
---|
| 141 | // use separate layer
|
---|
[13288] | 142 | JCheckBox layer = new JCheckBox(tr("Download as new layer"));
|
---|
[11279] | 143 | layer.setToolTipText(tr("Select if the data should be downloaded into a new layer"));
|
---|
| 144 | layer.setSelected(USE_NEW_LAYER.get());
|
---|
| 145 | all.add(layer, GBC.eop().fill(GBC.BOTH));
|
---|
| 146 |
|
---|
[13261] | 147 | // zoom to downloaded data
|
---|
| 148 | JCheckBox zoom = new JCheckBox(tr("Zoom to downloaded data"));
|
---|
| 149 | zoom.setToolTipText(tr("Select to zoom to entire newly downloaded data."));
|
---|
| 150 | zoom.setSelected(DOWNLOAD_ZOOMTODATA.get());
|
---|
| 151 | all.add(zoom, GBC.eop().fill(GBC.BOTH));
|
---|
| 152 |
|
---|
| 153 | ExpertToggleAction.addVisibilitySwitcher(zoom);
|
---|
| 154 |
|
---|
[2070] | 155 | ExtendedDialog dialog = new ExtendedDialog(Main.parent,
|
---|
| 156 | tr("Download Location"),
|
---|
[12279] | 157 | tr("Download URL"), tr("Cancel"))
|
---|
| 158 | .setContent(all, false /* don't embedded content in JScrollpane */)
|
---|
| 159 | .setButtonIcons("download", "cancel")
|
---|
| 160 | .setToolTipTexts(
|
---|
[2285] | 161 | tr("Start downloading data"),
|
---|
[12279] | 162 | tr("Close dialog and cancel downloading"))
|
---|
| 163 | .configureContextsensitiveHelp("/Action/OpenLocation", true /* show help button */);
|
---|
[13261] | 164 | dialog.setupDialog();
|
---|
| 165 | dialog.pack();
|
---|
| 166 | dialog.setRememberWindowGeometry(getClass().getName() + ".geometry",
|
---|
| 167 | WindowGeometry.centerInWindow(Main.parent, dialog.getPreferredSize()));
|
---|
[12279] | 168 | if (dialog.showDialog().getValue() == 1) {
|
---|
[11279] | 169 | USE_NEW_LAYER.put(layer.isSelected());
|
---|
[13261] | 170 | DOWNLOAD_ZOOMTODATA.put(zoom.isSelected());
|
---|
[11279] | 171 | remindUploadAddressHistory(uploadAddresses);
|
---|
| 172 | openUrl(Utils.strip(uploadAddresses.getText()));
|
---|
| 173 | }
|
---|
[1169] | 174 | }
|
---|
[6069] | 175 |
|
---|
[1169] | 176 | /**
|
---|
[5691] | 177 | * Replies the list of download tasks accepting the given url.
|
---|
| 178 | * @param url The URL to open
|
---|
[7749] | 179 | * @param isRemotecontrol True if download request comes from remotecontrol.
|
---|
[5691] | 180 | * @return The list of download tasks accepting the given url.
|
---|
| 181 | * @since 5691
|
---|
[1169] | 182 | */
|
---|
[7749] | 183 | public Collection<DownloadTask> findDownloadTasks(final String url, boolean isRemotecontrol) {
|
---|
[11279] | 184 | return downloadTasks.stream()
|
---|
| 185 | .filter(Objects::nonNull)
|
---|
| 186 | .map(taskClass -> {
|
---|
| 187 | try {
|
---|
| 188 | return taskClass.getConstructor().newInstance();
|
---|
| 189 | } catch (ReflectiveOperationException e) {
|
---|
[12620] | 190 | Logging.error(e);
|
---|
[11279] | 191 | return null;
|
---|
[4521] | 192 | }
|
---|
[11279] | 193 | })
|
---|
| 194 | .filter(Objects::nonNull)
|
---|
| 195 | .filter(task -> task.acceptsUrl(url, isRemotecontrol))
|
---|
| 196 | .collect(Collectors.toList());
|
---|
[5691] | 197 | }
|
---|
| 198 |
|
---|
| 199 | /**
|
---|
[6031] | 200 | * Summarizes acceptable urls for error message purposes.
|
---|
[6032] | 201 | * @return The HTML message to be displayed
|
---|
| 202 | * @since 6031
|
---|
[6031] | 203 | */
|
---|
| 204 | public String findSummaryDocumentation() {
|
---|
[6264] | 205 | StringBuilder result = new StringBuilder("<table>");
|
---|
[6040] | 206 | for (Class<? extends DownloadTask> taskClass : downloadTasks) {
|
---|
[6031] | 207 | if (taskClass != null) {
|
---|
| 208 | try {
|
---|
| 209 | DownloadTask task = taskClass.getConstructor().newInstance();
|
---|
[6264] | 210 | result.append(task.acceptsDocumentationSummary());
|
---|
[10212] | 211 | } catch (ReflectiveOperationException e) {
|
---|
[12620] | 212 | Logging.error(e);
|
---|
[6031] | 213 | }
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
[6264] | 216 | result.append("</table>");
|
---|
| 217 | return result.toString();
|
---|
[6031] | 218 | }
|
---|
| 219 |
|
---|
| 220 | /**
|
---|
[5691] | 221 | * Open the given URL.
|
---|
[7859] | 222 | * @param newLayer true if the URL needs to be opened in a new layer, false otherwise
|
---|
[5691] | 223 | * @param url The URL to open
|
---|
[11986] | 224 | * @return the list of tasks that have been started successfully (can be empty).
|
---|
| 225 | * @since 11986 (return type)
|
---|
[5691] | 226 | */
|
---|
[11986] | 227 | public List<Future<?>> openUrl(boolean newLayer, String url) {
|
---|
[13927] | 228 | return openUrl(new DownloadParams().withNewLayer(newLayer), url);
|
---|
[11279] | 229 | }
|
---|
| 230 |
|
---|
| 231 | /**
|
---|
[13927] | 232 | * Open the given URL.
|
---|
| 233 | * @param settings download settings
|
---|
| 234 | * @param url The URL to open
|
---|
| 235 | * @return the list of tasks that have been started successfully (can be empty).
|
---|
| 236 | * @since 13927
|
---|
| 237 | */
|
---|
| 238 | public List<Future<?>> openUrl(DownloadParams settings, String url) {
|
---|
| 239 | return openUrl(settings, DOWNLOAD_ZOOMTODATA.get(), url);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | /**
|
---|
[11279] | 243 | * Open the given URL. This class checks the {@link #USE_NEW_LAYER} preference to check if a new layer should be used.
|
---|
| 244 | * @param url The URL to open
|
---|
[11986] | 245 | * @return the list of tasks that have been started successfully (can be empty).
|
---|
| 246 | * @since 11986 (return type)
|
---|
[11279] | 247 | */
|
---|
[11986] | 248 | public List<Future<?>> openUrl(String url) {
|
---|
[13261] | 249 | return openUrl(USE_NEW_LAYER.get(), DOWNLOAD_ZOOMTODATA.get(), url);
|
---|
[11279] | 250 | }
|
---|
| 251 |
|
---|
[13261] | 252 | /**
|
---|
| 253 | * Open the given URL.
|
---|
| 254 | * @param newLayer true if the URL needs to be opened in a new layer, false otherwise
|
---|
| 255 | * @param zoomToData true to zoom to entire newly downloaded data, false otherwise
|
---|
| 256 | * @param url The URL to open
|
---|
| 257 | * @return the list of tasks that have been started successfully (can be empty).
|
---|
| 258 | * @since 13261
|
---|
| 259 | */
|
---|
| 260 | public List<Future<?>> openUrl(boolean newLayer, boolean zoomToData, String url) {
|
---|
[13927] | 261 | return openUrl(new DownloadParams().withNewLayer(newLayer), zoomToData, url);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | /**
|
---|
| 265 | * Open the given URL.
|
---|
| 266 | * @param settings download settings
|
---|
| 267 | * @param zoomToData true to zoom to entire newly downloaded data, false otherwise
|
---|
| 268 | * @param url The URL to open
|
---|
| 269 | * @return the list of tasks that have been started successfully (can be empty).
|
---|
| 270 | * @since 13927
|
---|
| 271 | */
|
---|
| 272 | public List<Future<?>> openUrl(DownloadParams settings, boolean zoomToData, String url) {
|
---|
[7749] | 273 | Collection<DownloadTask> tasks = findDownloadTasks(url, false);
|
---|
[8195] | 274 |
|
---|
| 275 | if (tasks.size() > 1) {
|
---|
| 276 | tasks = askWhichTasksToLoad(tasks);
|
---|
| 277 | } else if (tasks.isEmpty()) {
|
---|
| 278 | warnNoSuitableTasks(url);
|
---|
[11986] | 279 | return Collections.emptyList();
|
---|
[8195] | 280 | }
|
---|
| 281 |
|
---|
[13607] | 282 | PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
|
---|
[9062] | 283 |
|
---|
[11986] | 284 | List<Future<?>> result = new ArrayList<>();
|
---|
[8195] | 285 | for (final DownloadTask task : tasks) {
|
---|
[6474] | 286 | try {
|
---|
[13261] | 287 | task.setZoomAfterDownload(zoomToData);
|
---|
[13927] | 288 | result.add(MainApplication.worker.submit(new PostDownloadHandler(task, task.loadUrl(settings, url, monitor))));
|
---|
[6474] | 289 | } catch (IllegalArgumentException e) {
|
---|
[12620] | 290 | Logging.error(e);
|
---|
[6474] | 291 | }
|
---|
[5691] | 292 | }
|
---|
[11986] | 293 | return result;
|
---|
[1169] | 294 | }
|
---|
[6069] | 295 |
|
---|
[6032] | 296 | /**
|
---|
[8195] | 297 | * Asks the user which of the possible tasks to perform.
|
---|
| 298 | * @param tasks a list of possible tasks
|
---|
| 299 | * @return the selected tasks from the user or an empty list if the dialog has been canceled
|
---|
| 300 | */
|
---|
| 301 | Collection<DownloadTask> askWhichTasksToLoad(final Collection<DownloadTask> tasks) {
|
---|
[13206] | 302 | final JList<DownloadTask> list = new JList<>(tasks.toArray(new DownloadTask[0]));
|
---|
[8195] | 303 | list.addSelectionInterval(0, tasks.size() - 1);
|
---|
[11343] | 304 | final ExtendedDialog dialog = new WhichTasksToPerformDialog(list);
|
---|
[8195] | 305 | dialog.showDialog();
|
---|
| 306 | return dialog.getValue() == 1 ? list.getSelectedValuesList() : Collections.<DownloadTask>emptyList();
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | /**
|
---|
| 310 | * Displays an error message dialog that no suitable tasks have been found for the given url.
|
---|
| 311 | * @param url the given url
|
---|
| 312 | */
|
---|
[11279] | 313 | protected void warnNoSuitableTasks(final String url) {
|
---|
[8195] | 314 | final String details = findSummaryDocumentation(); // Explain what patterns are supported
|
---|
| 315 | HelpAwareOptionPane.showMessageDialogInEDT(Main.parent, "<html><p>" + tr(
|
---|
| 316 | "Cannot open URL ''{0}''<br>The following download tasks accept the URL patterns shown:<br>{1}",
|
---|
[9972] | 317 | url, details) + "</p></html>", tr("Download Location"), JOptionPane.ERROR_MESSAGE, ht("/Action/OpenLocation"));
|
---|
[8195] | 318 | }
|
---|
| 319 |
|
---|
| 320 | /**
|
---|
[6032] | 321 | * Adds a new download task to the supported ones.
|
---|
| 322 | * @param taskClass The new download task to add
|
---|
[13493] | 323 | * @return <code>true</code> (as specified by {@link Collection#add})
|
---|
[6032] | 324 | */
|
---|
[6890] | 325 | public final boolean addDownloadTaskClass(Class<? extends DownloadTask> taskClass) {
|
---|
[4521] | 326 | return this.downloadTasks.add(taskClass);
|
---|
| 327 | }
|
---|
[1146] | 328 | }
|
---|