[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[612] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[2327] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[612] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
| 7 | import java.awt.event.ActionEvent;
|
---|
| 8 | import java.awt.event.KeyEvent;
|
---|
[11774] | 9 | import java.util.ArrayList;
|
---|
| 10 | import java.util.List;
|
---|
[12574] | 11 | import java.util.Optional;
|
---|
[11774] | 12 | import java.util.concurrent.ExecutionException;
|
---|
[2322] | 13 | import java.util.concurrent.Future;
|
---|
[612] | 14 |
|
---|
[12574] | 15 | import javax.swing.JOptionPane;
|
---|
| 16 |
|
---|
[612] | 17 | import org.openstreetmap.josm.Main;
|
---|
[11774] | 18 | import org.openstreetmap.josm.actions.downloadtasks.AbstractDownloadTask;
|
---|
[2327] | 19 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
[7531] | 20 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
|
---|
[2327] | 21 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
[2322] | 22 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
[2330] | 23 | import org.openstreetmap.josm.data.Bounds;
|
---|
[11774] | 24 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
| 25 | import org.openstreetmap.josm.data.ViewportData;
|
---|
[612] | 26 | import org.openstreetmap.josm.gui.download.DownloadDialog;
|
---|
[11774] | 27 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
| 28 | import org.openstreetmap.josm.tools.Pair;
|
---|
[1084] | 29 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[612] | 30 |
|
---|
| 31 | /**
|
---|
[655] | 32 | * Action that opens a connection to the osm server and downloads map data.
|
---|
[612] | 33 | *
|
---|
| 34 | * An dialog is displayed asking the user to specify a rectangle to grab.
|
---|
| 35 | * The url and account settings from the preferences are used.
|
---|
| 36 | *
|
---|
| 37 | * @author imi
|
---|
| 38 | */
|
---|
| 39 | public class DownloadAction extends JosmAction {
|
---|
[7509] | 40 |
|
---|
[6509] | 41 | /**
|
---|
| 42 | * Constructs a new {@code DownloadAction}.
|
---|
| 43 | */
|
---|
[1169] | 44 | public DownloadAction() {
|
---|
[1212] | 45 | super(tr("Download from OSM..."), "download", tr("Download map data from the OSM server."),
|
---|
[11621] | 46 | Shortcut.registerShortcut("file:download", tr("File: {0}", tr("Download from OSM...")), KeyEvent.VK_DOWN, Shortcut.CTRL_SHIFT),
|
---|
| 47 | true);
|
---|
[2323] | 48 | putValue("help", ht("/Action/Download"));
|
---|
[1169] | 49 | }
|
---|
[612] | 50 |
|
---|
[6084] | 51 | @Override
|
---|
[2132] | 52 | public void actionPerformed(ActionEvent e) {
|
---|
[2331] | 53 | DownloadDialog dialog = DownloadDialog.getInstance();
|
---|
[2335] | 54 | dialog.restoreSettings();
|
---|
[2331] | 55 | dialog.setVisible(true);
|
---|
[12574] | 56 |
|
---|
| 57 | if (dialog.isCanceled()) {
|
---|
| 58 | return;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | dialog.rememberSettings();
|
---|
| 62 |
|
---|
| 63 | Optional<Bounds> selectedArea = dialog.getSelectedDownloadArea();
|
---|
| 64 | if (!selectedArea.isPresent()) {
|
---|
| 65 | JOptionPane.showMessageDialog(
|
---|
| 66 | dialog,
|
---|
| 67 | tr("Please select a download area first."),
|
---|
| 68 | tr("Error"),
|
---|
| 69 | JOptionPane.ERROR_MESSAGE
|
---|
| 70 | );
|
---|
| 71 | return;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | final Bounds area = selectedArea.get();
|
---|
| 75 | final boolean zoom = dialog.isZoomToDownloadedDataRequired();
|
---|
| 76 | final List<Pair<AbstractDownloadTask<?>, Future<?>>> tasks = new ArrayList<>();
|
---|
| 77 |
|
---|
| 78 | if (dialog.isDownloadOsmData()) {
|
---|
| 79 | DownloadOsmTask task = new DownloadOsmTask();
|
---|
| 80 | task.setZoomAfterDownload(zoom && !dialog.isDownloadGpxData() && !dialog.isDownloadNotes());
|
---|
| 81 | Future<?> future = task.download(dialog.isNewLayerRequired(), area, null);
|
---|
| 82 | Main.worker.submit(new PostDownloadHandler(task, future));
|
---|
| 83 | if (zoom) {
|
---|
| 84 | tasks.add(new Pair<>(task, future));
|
---|
[1847] | 85 | }
|
---|
[12574] | 86 | }
|
---|
| 87 |
|
---|
| 88 | if (dialog.isDownloadGpxData()) {
|
---|
| 89 | DownloadGpsTask task = new DownloadGpsTask();
|
---|
| 90 | task.setZoomAfterDownload(zoom && !dialog.isDownloadOsmData() && !dialog.isDownloadNotes());
|
---|
| 91 | Future<?> future = task.download(dialog.isNewLayerRequired(), area, null);
|
---|
| 92 | Main.worker.submit(new PostDownloadHandler(task, future));
|
---|
| 93 | if (zoom) {
|
---|
| 94 | tasks.add(new Pair<>(task, future));
|
---|
[1847] | 95 | }
|
---|
[12574] | 96 | }
|
---|
| 97 |
|
---|
| 98 | if (dialog.isDownloadNotes()) {
|
---|
| 99 | DownloadNotesTask task = new DownloadNotesTask();
|
---|
| 100 | task.setZoomAfterDownload(zoom && !dialog.isDownloadOsmData() && !dialog.isDownloadGpxData());
|
---|
| 101 | Future<?> future = task.download(false, area, null);
|
---|
| 102 | Main.worker.submit(new PostDownloadHandler(task, future));
|
---|
| 103 | if (zoom) {
|
---|
| 104 | tasks.add(new Pair<>(task, future));
|
---|
[7531] | 105 | }
|
---|
[12574] | 106 | }
|
---|
| 107 |
|
---|
| 108 | if (zoom && tasks.size() > 1) {
|
---|
| 109 | Main.worker.submit(() -> {
|
---|
| 110 | ProjectionBounds bounds = null;
|
---|
| 111 | // Wait for completion of download jobs
|
---|
| 112 | for (Pair<AbstractDownloadTask<?>, Future<?>> p : tasks) {
|
---|
| 113 | try {
|
---|
| 114 | p.b.get();
|
---|
| 115 | ProjectionBounds b = p.a.getDownloadProjectionBounds();
|
---|
| 116 | if (bounds == null) {
|
---|
| 117 | bounds = b;
|
---|
| 118 | } else if (b != null) {
|
---|
| 119 | bounds.extend(b);
|
---|
[11774] | 120 | }
|
---|
[12574] | 121 | } catch (InterruptedException | ExecutionException ex) {
|
---|
| 122 | Main.warn(ex);
|
---|
[11774] | 123 | }
|
---|
[12574] | 124 | }
|
---|
| 125 | // Zoom to the larger download bounds
|
---|
| 126 | if (Main.map != null && bounds != null) {
|
---|
| 127 | final ProjectionBounds pb = bounds;
|
---|
| 128 | GuiHelper.runInEDTAndWait(() -> Main.map.mapView.zoomTo(new ViewportData(pb)));
|
---|
| 129 | }
|
---|
| 130 | });
|
---|
[2512] | 131 | }
|
---|
| 132 | }
|
---|
[612] | 133 | }
|
---|