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