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