source: josm/trunk/src/org/openstreetmap/josm/actions/UpdateDataAction.java@ 13751

Last change on this file since 13751 was 13486, checked in by Don-vip, 6 years ago

fix #8039, see #10456 - fix bugs with non-downloadable layers

  • Property svn:eol-style set to native
File size: 3.6 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.awt.geom.Area;
10import java.util.ArrayList;
11import java.util.List;
12import java.util.concurrent.Future;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList;
16import org.openstreetmap.josm.data.DataSource;
17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
18import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
19import org.openstreetmap.josm.io.OnlineResource;
20import 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 */
28public 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 OsmDataLayer editLayer = getLayerManager().getEditLayer();
50 setEnabled(editLayer != null && editLayer.isDownloadable() && !Main.isOffline(OnlineResource.OSM_API));
51 }
52
53 @Override
54 public void actionPerformed(ActionEvent e) {
55 OsmDataLayer editLayer = getLayerManager().getEditLayer();
56 if (!isEnabled() || editLayer == null || !editLayer.isDownloadable())
57 return;
58
59 List<Area> areas = new ArrayList<>();
60 for (DataSource ds : editLayer.data.getDataSources()) {
61 areas.add(new Area(ds.bounds.asRect()));
62 }
63
64 // The next two blocks removes every intersection from every DataSource Area
65 // This prevents downloading the same data numerous times at intersections
66 // and also skips smaller bounding boxes that are contained within larger ones entirely.
67 for (int i = 0; i < areas.size(); i++) {
68 for (int j = i+1; j < areas.size(); j++) {
69 areas.get(i).subtract(areas.get(j));
70 }
71 }
72
73 for (int i = areas.size()-1; i > 0; i--) {
74 for (int j = i-1; j > 0; j--) {
75 areas.get(i).subtract(areas.get(j));
76 }
77 }
78
79 List<Area> areasToDownload = new ArrayList<>();
80 for (Area a : areas) {
81 if (a.isEmpty()) {
82 continue;
83 }
84 areasToDownload.add(a);
85 }
86
87 if (areasToDownload.isEmpty()) {
88 // no bounds defined in the dataset? we update all primitives in the data set using a series of multi fetch requests
89 UpdateSelectionAction.updatePrimitives(editLayer.data.allPrimitives());
90 } else {
91 // bounds defined? => use the bbox downloader
92 final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
93 final Future<?> future = new DownloadTaskList().download(false /* no new layer */, areasToDownload, true, false, monitor);
94 waitFuture(future, monitor);
95 }
96 }
97}
Note: See TracBrowser for help on using the repository browser.