| 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.List;
|
|---|
| 11 | import java.util.concurrent.Future;
|
|---|
| 12 | import java.util.stream.Collectors;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList;
|
|---|
| 15 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 16 | import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
|
|---|
| 17 | import org.openstreetmap.josm.io.NetworkManager;
|
|---|
| 18 | import org.openstreetmap.josm.io.OnlineResource;
|
|---|
| 19 | import org.openstreetmap.josm.spi.preferences.Config;
|
|---|
| 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 | setHelpId(ht("/Action/UpdateData"));
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | @Override
|
|---|
| 45 | protected boolean listenToSelectionChange() {
|
|---|
| 46 | return false;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Refreshes the enabled state
|
|---|
| 51 | */
|
|---|
| 52 | @Override
|
|---|
| 53 | protected void updateEnabledState() {
|
|---|
| 54 | OsmDataLayer editLayer = getLayerManager().getEditLayer();
|
|---|
| 55 | setEnabled(editLayer != null && editLayer.isDownloadable() && !NetworkManager.isOffline(OnlineResource.OSM_API));
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | @Override
|
|---|
| 59 | public void actionPerformed(ActionEvent e) {
|
|---|
| 60 | OsmDataLayer editLayer = getLayerManager().getEditLayer();
|
|---|
| 61 | if (!isEnabled() || editLayer == null || !editLayer.isDownloadable())
|
|---|
| 62 | return;
|
|---|
| 63 |
|
|---|
| 64 | List<Area> areas = editLayer.data.getDataSources().stream()
|
|---|
| 65 | .map(ds -> new Area(ds.bounds.asRect()))
|
|---|
| 66 | .collect(Collectors.toList());
|
|---|
| 67 |
|
|---|
| 68 | // The next two blocks removes every intersection from every DataSource Area
|
|---|
| 69 | // This prevents downloading the same data numerous times at intersections
|
|---|
| 70 | // and also skips smaller bounding boxes that are contained within larger ones entirely.
|
|---|
| 71 | for (int i = 0; i < areas.size(); i++) {
|
|---|
| 72 | for (int j = i+1; j < areas.size(); j++) {
|
|---|
| 73 | areas.get(i).subtract(areas.get(j));
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | for (int i = areas.size()-1; i > 0; i--) {
|
|---|
| 78 | for (int j = i-1; j > 0; j--) {
|
|---|
| 79 | areas.get(i).subtract(areas.get(j));
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | List<Area> areasToDownload = areas.stream()
|
|---|
| 84 | .filter(a -> !a.isEmpty())
|
|---|
| 85 | .collect(Collectors.toList());
|
|---|
| 86 |
|
|---|
| 87 | if (areasToDownload.isEmpty()) {
|
|---|
| 88 | // no bounds defined in the dataset? We update all but the incomplete primitives in the data set
|
|---|
| 89 | UpdateSelectionAction.updatePrimitives(editLayer.data.allPrimitives().stream()
|
|---|
| 90 | .filter(p -> !p.isNew() && !p.isIncomplete()).collect(Collectors.toList()));
|
|---|
| 91 | } else {
|
|---|
| 92 | // bounds defined? => use the bbox downloader
|
|---|
| 93 | final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
|
|---|
| 94 | final Future<?> future = new DownloadTaskList(Config.getPref().getBoolean("update.data.zoom-after-download"))
|
|---|
| 95 | .download(false /* no new layer */, areasToDownload, true, false, monitor);
|
|---|
| 96 | waitFuture(future, monitor);
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|