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