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