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.awt.geom.Area;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.concurrent.Future;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList;
|
---|
16 | import org.openstreetmap.josm.data.DataSource;
|
---|
17 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
18 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
19 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
20 |
|
---|
21 | public class UpdateDataAction extends JosmAction {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Constructs a new {@code UpdateDataAction}.
|
---|
25 | */
|
---|
26 | public UpdateDataAction() {
|
---|
27 | super(tr("Update data"),
|
---|
28 | "updatedata",
|
---|
29 | tr("Updates the objects in the active data layer from the server."),
|
---|
30 | Shortcut.registerShortcut("file:updatedata",
|
---|
31 | tr("File: {0}", tr("Update data")),
|
---|
32 | KeyEvent.VK_U, Shortcut.CTRL),
|
---|
33 | true);
|
---|
34 | putValue("help", ht("/Action/UpdateData"));
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Refreshes the enabled state
|
---|
39 | */
|
---|
40 | @Override
|
---|
41 | protected void updateEnabledState() {
|
---|
42 | setEnabled(getEditLayer() != null && !Main.isOffline(OnlineResource.OSM_API));
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void actionPerformed(ActionEvent e) {
|
---|
47 | if (!isEnabled())
|
---|
48 | return;
|
---|
49 | if (getEditLayer() == null)
|
---|
50 | return;
|
---|
51 |
|
---|
52 | List<Area> areas = new ArrayList<>();
|
---|
53 | for (DataSource ds : getEditLayer().data.dataSources) {
|
---|
54 | areas.add(new Area(ds.bounds.asRect()));
|
---|
55 | }
|
---|
56 |
|
---|
57 | // The next two blocks removes every intersection from every DataSource Area
|
---|
58 | // This prevents downloading the same data numerous times at intersections
|
---|
59 | // and also skips smaller bounding boxes that are contained within larger ones
|
---|
60 | // entirely.
|
---|
61 | for (int i = 0; i < areas.size(); i++) {
|
---|
62 | for (int j = i+1; j < areas.size(); j++) {
|
---|
63 | areas.get(i).subtract(areas.get(j));
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | for (int i = areas.size()-1; i > 0; i--) {
|
---|
68 | for (int j = i-1; j > 0; j--) {
|
---|
69 | areas.get(i).subtract(areas.get(j));
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | List<Area> areasToDownload = new ArrayList<>();
|
---|
74 | for (Area a : areas) {
|
---|
75 | if (a.isEmpty()) {
|
---|
76 | continue;
|
---|
77 | }
|
---|
78 | areasToDownload.add(a);
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (areasToDownload.isEmpty()) {
|
---|
82 | // no bounds defined in the dataset? we update all primitives in the data set
|
---|
83 | // using a series of multi fetch requests
|
---|
84 | //
|
---|
85 | UpdateSelectionAction.updatePrimitives(getEditLayer().data.allPrimitives());
|
---|
86 | } else {
|
---|
87 | // bounds defined? => use the bbox downloader
|
---|
88 | //
|
---|
89 | final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
|
---|
90 | final Future<?> future = new DownloadTaskList().download(false /* no new layer */, areasToDownload, true, false, monitor);
|
---|
91 | waitFuture(future, monitor);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|