source: josm/trunk/src/org/openstreetmap/josm/actions/DownloadAction.java@ 12620

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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