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.Optional;
|
---|
12 | import java.util.concurrent.ExecutionException;
|
---|
13 | import java.util.concurrent.Future;
|
---|
14 |
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.actions.downloadtasks.AbstractDownloadTask;
|
---|
19 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
20 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
|
---|
21 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
22 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
23 | import org.openstreetmap.josm.data.Bounds;
|
---|
24 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
25 | import org.openstreetmap.josm.data.ViewportData;
|
---|
26 | import org.openstreetmap.josm.gui.download.DownloadDialog;
|
---|
27 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
28 | import org.openstreetmap.josm.tools.Pair;
|
---|
29 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Action that opens a connection to the osm server and downloads map data.
|
---|
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 {
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Constructs a new {@code DownloadAction}.
|
---|
43 | */
|
---|
44 | public DownloadAction() {
|
---|
45 | super(tr("Download from OSM..."), "download", tr("Download map data from the OSM server."),
|
---|
46 | Shortcut.registerShortcut("file:download", tr("File: {0}", tr("Download from OSM...")), KeyEvent.VK_DOWN, Shortcut.CTRL_SHIFT),
|
---|
47 | true);
|
---|
48 | putValue("help", ht("/Action/Download"));
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public void actionPerformed(ActionEvent e) {
|
---|
53 | DownloadDialog dialog = DownloadDialog.getInstance();
|
---|
54 | dialog.restoreSettings();
|
---|
55 | dialog.setVisible(true);
|
---|
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));
|
---|
85 | }
|
---|
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));
|
---|
95 | }
|
---|
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));
|
---|
105 | }
|
---|
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);
|
---|
120 | }
|
---|
121 | } catch (InterruptedException | ExecutionException ex) {
|
---|
122 | Main.warn(ex);
|
---|
123 | }
|
---|
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 | });
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|