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